Category Archives: Java

Java 17: Pattern Matching for Switch


The Java 17 release introduces  Pattern Matching for switch as a preview language feature. Pattern matching  allows case labels with patterns rather than just constants, which gives us more flexibility when defining conditions for switch cases.  

  • Type pattern

How to perform a different action for each type of object, without using instance of?

Now the switch selector expression can be of any type.


  • Sealed class

Using pattern matching and sealed class, the compiler can check that all allowed subclasses are covered and there is no longer a need for a default case.



  • Guarded pattern

Guarded pattern allow us to perform additional checks.



The full implementation of this tutorial can be found over on GitHub.

Non-instantiable class

Sometimes we need to develop a class Helper, also known as Utility class, contain a group of reusable methods across the application. As an example consider Apache StringUtils or IOUtils. It is stateless frequently with methods as static and cannot be instantiated.

We can eliminate the instantiation of default constructor with an explicit private constructor and an Exception to indicate that the requested operation is not supported, it guarantees the class will never be instantiated under any circumstances:

By marking your class with @UtilityClass, lombok will automatically generate a private constructor that throws an exception:

Vanilla:

The full implementation of this tutorial can be found over on GitHub.

Deploying a containerized Java web application

Install Docker CE

1) Set up the docker repository:

sudo apt-get update

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

2) Install Docker CE

sudo apt-get update

sudo apt-get install docker-ce

3) Verify the installation

$ sudo docker run hello-world

Deployment with the jboss/wildfly Docker image

AdminFaces is an open source project which brings Bootstrap and AdminLTE to your application via a PrimeFaces theme and a JSF responsive template.

The sample files can be downloaded here

1) Create docker file Dockerfile with following content:

 FROM jboss/wildfly
 ADD admin-starter.war /opt/jboss/wildfly/standalone/deployments/

 

2) Place your admin-starter.war file in the same directory as your Dockerfile.

3) Run the build with:

sudo docker build --tag=admin-starter-app .

4) Run the container with:

sudo docker run -it -p 8080:8080 admin-starter-app

Application will be deployed on the container boot.

The application is available at http://localhost:8080/admin-starter

Pushing and Pulling to and from Docker Hub

1) Log into the Docker Hub from the command line

sudo docker login

2) Tag your image

sudo docker tag admin-starter-app yourhubusername/admin-starter-app:v0.0.1

3) Push your image to the repository you created

sudo docker push yourhubusername/admin-starter-app:v0.0.1

4) Create your Docker droplet

https://www.digitalocean.com/products/one-click-apps/docker/

5) Login in Digital Ocean

ssh user@yourDigitalOceanMachine

6) Allow Connections

sudo ufw allow 8080

7)  Pull your image

sudo docker pull yourhubusername/admin-starter-app:v0.0.1

8) Run the container with

sudo docker run -it -p 8080:8080 yourhubusername/admin-starter-app:v0.0.1

Application will be deployed on the container boot.

The application is available at http://yourDigitalOceanMachine:8080/admin-starter