Category: Uncategorized

  • How does OAuth 2.0 work?

    Let’s say you have created an API. You want to protect it. There are so many ways to do so. And one of the most efficient way to do is to protect it using OAuth 2.0. As oauth0.com explains: OAuth 2.0 is a protocol that allows a user to grant limited access to their resources on…

  • The different ways to sort Collections in Java

    Let’s say you have an Employee class with attributes like name , age , rating etc. You want to create a number of employees and sort them all by name. How to do this in Java? You make the Employee class implement Comparable interface which has a method compareTo(). The Employee class needs to implement…

  • How to implement Concurrency in Java – A very minimalist example

    Let’s say you are implementing a batch service and you want to execute a lot of methods. Let’s assume they are independent functionalities and the output of one is not used by any other methods. Executing them one by one will consume a lot of time. The best strategy is to execute them in parallel.…

  • How to enable virtualization in Windows ?

    I have Windows 10 Home Single Language edition of Windows Operation System installed in my laptop. I tried to install Docker for Desktop in my system and it didn’t support. Instead Docker suggested installing Docker Toolbox . But for installing one of the prerequisites is that Virtualization should be enabled in Windows. I checked mine…

  • How to make sure resources are auto-closed in Java?

    Prior to Java 7 , if we wanted to close a resource like a FileOutputStream we had to do it inside a finally block. And the code looked something like this : We had to check if the resources is null in the finally block and again add a try-catch block as closing the resource…

  • How to create a custom JRE using Java Modules?

    Starting Java 9 , Java introduced Modules. It allowed us to organize our code into modules. Also Java organised its own internal libraries into modules. Here is the list of modules in Java by default(you can get this by running the command : java –list-modules from command prompt starting Java 9): G:\softwares\java14\jdk-14\bin>java –list-modules java.base@14 java.compiler@14…

  • How to ignore Code Coverage for Lombok generated code?

    Recently I started using lombok code in one of my projects. It reduced verbosity to a great extent . But one issue kept popping up. The Code Coverage tool which I was using (JaCoCo) was showing zero percentage code coverage for the domain classes annotated with @Data annotation provided by lombok. I tried writing junit…

  • How to implement State Pattern in Java?

    Let’s say you run an eCommerce store. You want to develop an application to run your eCommerce. You are specifically concerned about the delivery of order items. When a customer places an order it goes through many different states: Order is received Order is packed Order is shipped Order is in transit Order is delivered…

  • How to implement Command pattern in Java?

    Let’s say you run a company. You want to give commands to your manager to instruct developers to write code for an application and testers to test it. Also you want to tell the manager what to do dynamically. You give him only the command but the manager neither knows who is going to execute…

  • How to implement AbstractFactory pattern in Java?

    Let’s say you run a dress factory. And you want to create an application to manage manufacturing dress. You have a separate factory for manufacturing shirts and another factory for manufacturing pants. These are the factories which are going to do the actual manufacturing. But you create two more virtual factories (which don’t physically exist)…