Posts

S.O.L.I.D principles

Image
The S.O.L.I.D principles of Object Oriented Design( OOD ) include these five principles. Lets expanded the acronyms: S  - Single-responsibility principle O  - Open-closed principle L  - Liskov substitution principle I  - Interface segregation principle D  - Dependency inversion principle # Single-responsibility  principle ( S.R.P ) A class should have only one job. O ne and only one reason to change. Examples of responsibilities : Persistence Validation Notification Error Handling Logging Formatting Parsing Mapping # Open-closed principle ( O.C.P ) A class should be open for extension, but closed for modification. # Liskov substitution principle ( L.S.P ) Subtypes must be substitutable for their base types.   In mathematics, a  Square  is a  Rectangle . Indeed it is a specialization of a rectangle. The "is a" makes you want to model this with inheritance. However if in code you made  Square  derive from  Rectangle , then a  Square  sh

Getter Template with Optional<> for IntelliJ IDEA

Image
If you are working with Java 8 and Intellij IDEA, good practice to use Optional for getters, of course depending of your logic :) I'll show how to quick configure IDE for generation such getters. Create new template and place this code: #if($field.modifierStatic) static ## #end Optional<$field.type> ## #set($name = $StringUtil.capitalizeWithJavaBeanConvention( $StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))) #if ($field.boolean && $field.primitive) is## #else get## #end ${name}() { return Optional.ofNullable($field.name); }

Hibernate Caches

- The first-level cache - Session Level  - The second-level cache  - SessionFactory Level  - The query cache The first-level cache   The first level cache type is the  session cache .  The scope of cache objects is of session. Once session is closed, cached objects are gone forever.  First level cache is enabled by default and you can not disable it.  When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.  If we query same object again with same session object, it will be loaded from cache and no sql query will be executed.  The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method.  The whole session cache can be removed using clear() method. It will remove all the entities stored in cache. The second-level cache Second level cache  is apart from first level cache

Java Collection Stream

Java Collection Stream was presented in Java 8.   Creation Create stream much the same as getting iterator from collection Stream<> stream = collection.stream(); Stream<> stream = collection.parallelStream(); Stream.filter() You filter a stream using the  filter()  method.  stream.filter( item -> item.startsWith("a") ); Stream.map() It is possible to map the items in a collection to other objects.  items.stream() .map( item -> item.toUpperCase() ) Stream.collect() When this method is invoked, the filtering and mapping will take place and the object resulting from those actions will be collected.  List<String> filtered = items.stream() .filter( item -> item.startsWith("o") ) .collect(Collectors.toList()); Stream.min() and Stream.max() Once these are called, the stream will be iterated, filtering and mapping applied, and the minimum or maximum value in the stream will b

The Garbage-First Garbage Collector

The Garbage-First (G1) garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with high probability, while achieving high throughput. Whole-heap operations, such as global marking, are performed concurrently with the application threads. This prevents interruptions proportional to heap or live-data size. The heap is partitioned into a set of equal-sized heap regions, each a contiguous range of virtual memory. G1 performs a concurrent global marking phase to determine the liveness of objects throughout the heap. After the mark phase completes, G1 knows which regions are mostly empty. It collects in these regions first, which usually yields a large amount of free space. This is why this method of garbage collection is called Garbage-First. Recommended Use Cases for G1 The first focus of G1

JVM Memory and Tuning

Java Heap and Perm Space Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Java memory is separated into two different regions. These regions are called Heap space and Permgen (for Permanent Generation) For the HotSpot Java VM, the memory pools for serial garbage collection are the following. Eden Space (heap): The pool from which memory is initially allocated for most objects. Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space. Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space. Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code. Permanent generation has been completely removed in JDK 8.  JDK8: Metaspace In JDK 8, classes metadata is now stored in the native heap and this space is called Metaspace. There are some ne