Creating a zip file of a folder with CRC32 checksum

This post explains how to create a zip file of a folder (also containing sub folders) with CRC32 checksum. The below code is similar to the previous post “Creating zip file of a folder” with minor change in main method. The main method code snippet from the previous post as shown below File folder =…… Continue reading Creating a zip file of a folder with CRC32 checksum

Printing json data in a formatted way

This post explains how to print json data in a properly formatted way, whether the destination is console, file etc. For example when the below code prints the json data to a file, the output will be as shown below Initial Code package package11; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; public…… Continue reading Printing json data in a formatted way

Handling json deserialization error

This post explains how to handle errors generated while deserializing the json file. This can be achieved by implementing an interface “DeserializationProblemHandler” provided by jackson framework as shown below UnMarshallingErrorHandler package package1; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; public class UnMarshallingErrorHandler extends DeserializationProblemHandler { @Override public boolean handleUnknownProperty(DeserializationContext ctxt,…… Continue reading Handling json deserialization error

Executing compiled script

In the previous posts under “Java Scripting”, we used ScriptEngine eval method to execute scripts stored in a string variable or file. The ScriptEngine’s eval methods takes the script compiles to intermediate code and execute it immediately. If we have a script which has to be executed repeatedly, then instead of compiling and executing it…… Continue reading Executing compiled script

Combining Predicates

This post explains how we can combine two or more predicates using logical operations as mentioned below 1) and 2) or 3) negate Below is an example Person package Function; public class Person { private String ssn; private String fname; private String lname; private String description; private int age; public String getSsn() { return ssn;…… Continue reading Combining Predicates

Getting JsonGenerator settings

This post explains how to get JsonGenerator settings. JsonGenerator class obtained from JsonFactory is responsible for writing json data as a stream instead of constructing an entire object model in memory and then writing to destination. The list of settings that can be turned on or off are present in an enumeration JsonGenerator.Feature. The below…… Continue reading Getting JsonGenerator settings

Getting JsonFactory settings

This post explains how to get JsonFactory settings. The JsonFactory class is thread safe and responsible for creating instances of writer and reader. Creating an instance of JsonFactory is an light weight operation. The list of settings that can be turned on or off are present in an enumeration JsonFactory.Feature. The below code shows how…… Continue reading Getting JsonFactory settings

Executing sql statements as a batch through hibernate

They are two ways to execute sql statements as batch in hibernate, which are 1) Work 2) ReturningWork This approach is useful when we want to perform batch operations directly on the database instead of indirectly through hibernate. First we will implement the Work interface as shown below JdbcWork class JdbcWork implements Work { private…… Continue reading Executing sql statements as a batch through hibernate

Mocking Static class methods

In Mockito we cannot mock static class methods, so we can use PowerMock which extends the Mockito features and provides its own features. One of which is mocking static class methods. Below code will give an example how to mock static class methods Calculator package package1; public class Calculator { public static int add(int value1,…… Continue reading Mocking Static class methods