Top Posts in Python
Thread Safety in Java Singleton Classes with Example Code
Singleton is one of the most widely used creational design pattern to restrict the object creation by applications. In real world applications, resources like Database connections or Enterprise Information Systems (EIS) are limited and should be used wisely to avoid any resource crunch. To achieve this, we can implement Singleton design pattern to create a wrapper class around the resource and limit the number of object created at runtime to one.
In general we follow below steps to create a singleton class:
1. Override the private constructor to avoid any new object creation with new operator.
2. Declare a private static instance of the same class
3. Provide a public static method that will return the singleton class instance variable. If the variable is not initialized then initialize it or else simply return the instance variable.
Steps to Configure SSL on Tomcat and Setup Auto Redirect from HTTP to HTTPS
Secured Socket Layer (SSL) is the cryptography protocol to provide message security over the internet. It works on the notion of Private and Public keys and messages are encrypted before sending it over the network. To configure SSL on Tomcat, we need a digital certificate that can be created using Java keytool for development environment. For production environment, you should get the digital certificate from SSL certificate providers, for example, Verisign or Entrust.
HashMap implementation with List in Java
HashMap is one of the most widely used implementation of Map to store key-value pairs. It has been introduced in Java 1.2 and here I am trying to implement HashMap with ArrayList. The below code will provide two of the basic HashMap functions i.e get(key) and put(key, value). The code also takes care of checking duplicate values while storing.
Please note that its just the basic implementation and should not be used as a replacement of HashMap. Also while testing the code, make sure that the Object used in the KEY has proper implementation of equals() method.
StringBuffer vs StringBuilder
StringBuffer is the thread safe utility class to perform several operations on Strings. It contains append() and insert() methods that are widely used to perform operation on Strings in a multi-thread environment. If you will check the source code, most of its functions are synchronized for thread safety.
Since most of the String operations, for example concatenation happens in a single thread environment, Java 1.5 introduced another utility class StringBuilder to perform similar operations but doesn’t provide thread safety. If you will look into its source code, all the methods are unsynchronized.
How to Create Immutable Class in java?
Immutable objects are instances whose state doesn’t change after it has been initialized. For example, String is an immutable class and once instantiated its value never changes.
Immutable objects are good for caching purpose because you don’t need to worry about the value changes. Other benefit of immutable class is that it is inherently thread-safe, so you don’t need to worry about thread safety in case of multi-threaded environment.
Here I am providing a way to create immutable class via an example for better understanding.
ConcurrentHashMap in Java
If you are a Java Developer, I am sure that you must be aware of ConcurrentModificationException that comes when you want to modify the Collection object while using iterator to go through with all its element.
Java 1.5 has introduced java.util.concurrent package with Collection classes implementations that allow you to modify your collection object at runtime.
ConcurrentHashMap is the class that is similar to HashMap but works fine when you try to modify your map at runtime.
Shell Script to Change Extension of Multiple Files in Unix
In unix environment, mv is the command to move files and we can use it to change file extension also. But it works with a single file only and it doesn’t take wild characters also.
Here I am providing a simple script that you can use to change extension of multiple files in a directory.
Queue Implementation in Java using Array
Queue is a special type of collection designed to hold elements before processing and order elements in a FIFO (first-in-first-out) manner.
Here I am trying to implement Queue with Object Array and provides basic functionalities like enqueue(obj), dequeue() and isEmpty().
How to implement ArrayList with Array in Java
ArrayList is similar to Object Array but provides the feature of dynamic space allocation when the number of objects in the list grows. In Object Array we need to provide the size at the time of initialization but that is not required for ArrayList. Actually when you initialize ArrayList, it automatically assigns its capacity to 10.
Here I am trying to implement ArrayList with Object Array and provide basic functions i.e get(index), add(object) and remove(index).
JiBX is a very powerful framework for converting XML data to java object and vice versa. It is very useful in applications integration where XML is the format for data transfer, for example, Web Services and Legacy Systems Integration based on Message Oriented Model (MOM).
Converting Java Object in to XML is called Marshalling and creating java object from XML is called un-marshalling. There are many frameworks available for XML transformation such as JAXB and XMLBeans but JiBX differs in the approach for XML binding and transformation process. JiBX performs these tasks via utility classes generated at compile time via ant scripts. This approach reduces the processing time by moving away from the traditional two-step process with other parsers to a single step.
Benchmarks done on various XML binding tools have shown JiBX as the fastest and the most memory efficient parsing framework till date.
Here I am providing a sample application for XML transformation using JiBX parser.