Thursday 23 July 2015

Default & Static methods in Java 8


Interface: Interface is a business rules specific document/contract among various parties for effective communication between two or more interface systems.
Abstract class: Usually in Java, abstract means – incomplete. Abstract class a template, describing functionality that is common to a group of classes and require unique implementations within each class.

Prior to Java 8, Interfaces contain only “static final constants & abstract methods”. Below are the two new features introduced for Interfaces in Java 8.
Default Methods
Static Methos:

1. Default Methods:
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.

Syntax:
```java

public interface Transaction {
void insertRecord();
void updatedRecord();
void deleteRecord();
default void auditTransaction() {
// Implementation to persist information about transaction for an audit purpose
} } ```


Why do we need Default Methods in Interface?
In Java, interfaces are tightly coupled with their implementation classes. It’s not at all possible to add a new method in interface without implementing the same in its all implementers. So adding a new method in interface will break all its implementers.
So for backward compatibility, Java 8 provides a feasibility to add a new method definition an to interface.
One of the default method available in Java 8 java.util Collections’s is “forEach()”. This method is neither implemented in List nor in Collection. But, we can use this method across all List implementers.
Hierarchy:
List -> Collection -> Iterable.
So, forEach() method definition available in Iterable interface as follows:
default void forEach(Consumer action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}


Conflicts with multiple interface implementations:
When a class implements two or more interfaces and a default method with the same signature available in more than one interface, then implementer class will gets ambiguity to reuse which interface’s default method definition.
Error details:
Duplicate default methods named with the are inherited from the types and .


In this scenario, we need to override method definition in implementer class as required.
package com.sample;

public interface MetropolitonCity {

default public Integer getPropertyTaxPercent() {
return new Integer(10);
}
}
package com.sample;


public interface CapitalCity {

default public Integer getPropertyTaxPercent() {
return new Integer(8);
}
}
package com.sample;


public class Bangalore implements MetropolitonCity, CapitalCity {

public Integer getPropertyTaxPercent() {
Integer taxpercent;
// Can reuse from any one the interfaces
taxpercent = MetropolitonCity.super.getPropertyTaxPercent();
// or
taxpercent = CapitalCity.super.getPropertyTaxPercent();
// or we can add its own definition
taxpercent = new Integer(12);
return taxpercent;
}
}


Abstract class Vs Interfaces:
Once default methods introduced, both Abstract class & Interfaces looks similar. But still there is significant difference between them.

Abstract class can have constructors and member variables. Hence it can hold state of an object, whereas interfaces cannot hold the state of an object.
Methods in abstract classes can use and modify method arguments as well as the fields of their class. But default method can only access its arguments as interfaces do not have any state.
Note:
The default method cannot be final, so as to prevent modifying the default implementation of the method in the inherited classes.
The default method cannot be synchronized, while the synchronized block can be added inside the default method.
The default method cannot be used to override any non-final method of java.lang.Object class. Since Object is a super class for all classes in java, can cause confusion about its actual implementation in the sub classes.

2. Static Methods:
In general, a static method is a method that is associated with the class in which it is defined rather than with any object.
In addition to default methods, we can define static methods in Interfaces. It makes easier to organize helper methods in our library rather than keeping them in a separate utility class.
package com.sample;


public interface StaticMethodDemo {

static void staticMethod() {
System.out.println("Sample static method");
}
}
3. Functional Interfaces:
Marker Interfaces: The interfaces which contains no members in it called Marker Interfaces. Marker Interfaces are used to indicate something to Compiler, JVM.
Examples:
Java.util.Serializable : If JVM finds a class is implementing Serializable interface, it allows to persist the state of an object.
Java.lang.Clonebale: If JVM finds as a Class is implementing Clonebale interface, it performs some operation to support cloning.
The interface which contains a single abstract method is called “Functional Interface”. In Java 8, we can annotate functional interfaces with the annotation @FunctionalInterface. This annotation performs the violations at compilation time.
If an interface annotated with @FunctionalInterface and contains more/less than one abstract method, compiler throws an error as “Invalid '@FunctionalInterface' annotation; is not a functional interface”.
package com.sample;

@FunctionalInterface
public interface CapitalCity {

public abstract Integer getPropertyTaxPercent();

}
Popular Functional Interfaces in Java:
public interface Runnable { void run(); }
public interface Callable { V call() throws Exception; }
public interface ActionListener { void actionPerformed(ActionEvent e); }
public interface Comparator { int compare(T o1, T o2); boolean equals(Object obj); }
Functional interface can:
Have many number of default methods along with single abstract class. Since, default methods have already method definition.
Override public methods of java.lang.Object class and mark them as abstract. The overridden methods from Object class will not be considered as its own abstract methods.
Ex: java.util.Comporator has default, static methods and along with overridden methods from Object class. Functional interface implementation with inner class:
```java
Thread thread = new Thread(new Runnable() {

@Override
public void run() {
System.out.println(“This is a thread created with Runnable interface”);
}
});
The functional interfaces can be converted as Lambda Expressions with the following syntax.
(method arguments) -> {method body}
```
Ex:
Thread thread = new Thread(() -> System.out.println("This is a thread created with Lambda Expression"));
Comparator Interface:
public class Employee {
private Integer id;
private String name;
private Double salary;

public Employee(Integer id, String name, Double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getSalary() {
return salary;
}

public void setSalary(Double salary) {
this.salary = salary;
}
}

public class EmpSorting {

public void main(String[] args) {
List employees =
Arrays.asList(new Employee(1, "abc", new Double("10.00")),
new Employee(3, "def", new Double("15.00")),
new Employee(2, "ghi", new Double("5.00")));

// Sort employees list with prior to Java 8 approach
Collections.sort(employees, new Comparator() {

@Override
public int compare(Employee o1, Employee o2) {
return o1.id.compareTo(o2.id);
}
});

// Sorting employees list with Java 8 Lambda Expression
Collections.sort(employees, (Employee o1, Employee o2) -> {return o1.id.compareTo(o2.id);});
// Alos can be written as below. Here parameters datatypes can be auto detect
Collections.sort(employees, (o1, o2) -> {return o1.id.compareTo(o2.id);});
}
}

Saturday 7 February 2015

What is Type Erasure in Java Generics

Introduction:
Generics is one the most important features introduced in JDK 1.5.this feature is provided to perform compile-time checking and removing risk of ClassCastException that occures at runtime generally working with collections.

The whole collection framework re-written to use generics for type-safety. Lets discuss, how we can avoid type-safety in JDK 1.5 through examples.

JDK 1.4 coding style:
List collection = new ArrayList();
colection.add("This is a String object");
collection.add(new Integer(10));
collection.add(new UserdefinedObject());

for(int index = 0; index < collection.size(); index++) {
     Integer int = (Integer) collection.get(index);
}
Here, there is no type-check validation performed at compilation time. Hence, any kind of objected can be able to added the list. But while iterating at run-time there is no guarantee that all the elements are of type Integer. So there are more chances of getting ClasscastException.

To overcome this limitation, generics are introduced in JDK 5.

JDK 1.5 coding style:
List<Integer> collection = new ArrayList<Integer>();
collection.add(new Integer(10));
collection.add(new UserdefinedObject()); // Gives compilation error

for(Integer int : collection) {
    // No explicit casting required, since the collcetion object have only Integer objects.
}

What is Type Erasure in Generics:
To discuss this, lets take an example. We have a requirement to create two similar methods(overloading), one to accept list of Employee objects and another to accept list of Assests objects to perform required business logic.

So, our code looks like as follows:
public Double calculateCost(List<Employee> empList) {
     // business logic 
}
public Double calculateCost(List<Asset> assetList) (
     // business logic 
}
The above gives compilation error, saying "Method has the same easure as another method in type".

The reason for above compilation error is due to Java generics uses type erasure. It means that, Java generics is implemented on the compilation level. The byte code generated from compiler does not contain type information of generic type for the run-time execution.

After compilation, both list of employee and list of assest become List.
If we observe the code by using Java Decompiler, the code looks like below:
public Double calculateCost(List empList) { }
public Double calculateCost(List assetList) { }
Hence, while runtime both methods appears same signature. So JVM gets ambiguity to call which method to perform required operation.

In the case of above situations, we can use Wildcard characters in the methods signature as follows:
public Double calculateCost(List<?> list) { }

Note:
During the type erasure process, the Java compiler erases all type parameters and replaces each with its first bound if the type parameter is bounded or Object if the type parameter is unbounded.

Reference : http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
.

Wednesday 8 October 2014

Examples of Comparable, Comparator, equals() & hashCode()

We can discuss the use of Comparable, Comparator interfaces & equals(), hashCode() methods by using some problem description.

In Java, there is an utility called Collections to perform most of the required operations on collection framework classes.
One of the most frequently used operation is Sorting. We can do the sorting by using Collections.sort() method.
Collections.sort() method has the following two forms.

sort(List list) - Sorts the specified list into ascending order, according to the natural ordering of its elements.
sort(List list, Comparator c) - Sorts the specified list according to the order induced by the specified comparator.

Comparable and Comparator are two interfaces provided by Java Core API. From their names, we can tell they may be used for comparing stuff in some way. But what exactly are they and what is the difference between them?

Comparable:Comparable is implemented by a class in order to be able to comparing object of itself with some other objects. The class itself must implement the interface in order to be able to compare its instance(s). The method required for implementation is compareTo().
Objects which implement Comparable in java can be used as keys in a SortedMap like TreeMap or SortedSet like TreeSet without implementing any other interface.

Comparator:
Class whose objects to be sorted do not need to implement this interface. Some third class can implement this interface to sort.
Using Comparator interface, we can write different sorting based on different attributes of objects to be sorted.You can use anonymous comparator to compare at particular line of code.


Parameter
Comparable
Comparator
Sorting logic
Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects
Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc.
Implementation
Class whose objects to be sorted must implement this interface.e.g Country class needs to implement comparable to collection of country object by id
Class whose objects to be sorted do not need to implement this interface.Some other class can implement this interface. E.g.-CountrySortByIdComparator class can implement Comparator interface to sort collection of country object by id

Sorting method
int compareTo(Object o1)
This method compares this object with o1 object and returns a integer.Its value has following meaning
1. positive – this object is greater than o1
2. zero – this object equals to o1
3. negative – this object is less than o1
int compare(Object o1,Object o2)
This method compares o1 and o2 objects. and returns a integer.Its value has following meaning.
1. positive – o1 is greater than o2
2. zero – o1 equals to o2
3. negative – o1 is less than o1
Calling method
Collections.sort(List)
Here objects will be sorted on the basis of CompareTo method
Collections.sort(List, Comparator)
Here objects will be sorted on the basis of Compare method in Comparator
Package
Java.lang.Comparable

Java.util.Comparator

equals() & hashCode() methods: In Java every object has access to equals() & hashCode() methods, since these are available in java.lang.Object class & Object class is super class by default for all the classes. The main intention of these methods are to compare the given two or more objects and identify their equality.

The default implementation of equals() method in Object class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y. This particular comparison is also known as "shallow comparison".


These two methods have significant relationship with each other. If we want to override any one of the methods, need to override both the methods. We will discuss why do we need to override both, otherwise what is the impact.


The equals() method must exhibit the following properties:
  1. Symmetry: For two references, a and b, a.equals(b) if and only if b.equals(a)
  2. Reflexivity: For all non-null references, a.equals(a)
  3. Transitivity: If a.equals(b) and b.equals(c), then a.equals(c)
  4. Consistency with hashCode(): Two equal objects must have the same hashCode() value

Why do we need to override equals() method: An object might have many number of variables, but we may need to consider only few variables into consideration while comparing the objects.
Suppose, customer landed on some eCommerce application and would like buy some article. Article may have many number of properties such as name, capacity, cost, availability, seller_name, seller_location, delivery_date..etc. But while coming to comparison, customer may consider name, price & delivery_date.
Hence, it require to apply equals() methods to lookup only these properties while determining equality. 

Why to override hashCode() method: hashCode() method is used to get a unique integer for given object. This integer is used for determining the bucket location, when this object needs to be stored in some HashTable, HashMap like data structure. By default, Object’s hashCode() method returns an integer representation of memory address where object is stored.
The hashCode() method of objects is used when we insert them into a HashTable, HashMap or HashSet. More about hastables on Wikipedia.org for reference.
To insert any entry in map data structure, we need both key and value. If both key and values are user define data types, the hashCode() of the key will be determine where to store the object internally. When require to lookup the object from the map also, the hash code of the key will be determine where to search for the object.
The hash code only points to a certain "area" (or list, bucket etc) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The HashTable then iterates this area (all keys with the same hash code) and uses the key's equals() method to find the right key. Once the right key is found, the object stored for that key is returned.
So, as we can see, a combination of the hashCode() and equals() methods are used when storing and when looking up objects in a HashTable.

NOTES:
1.       Always use same attributes of an object to generate hashCode() and equals() both. As in our case, we have used employee id.
2.       equals() must be consistent (if the objects are not modified, then it must keep returning the same value).
3.       Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
4.       If you override one, then you should override the other.

Problem Statement:
There are 2 travel companies who have buses travelling from dest A to dest B. they want to merge their buses timetable and produce it to the customer so that they can find out which bus is effective for them and choose bus accordingly(o/p).

They will provide a text file having the I/P as shown above and we need to give O/P as shown above.

Conditions:
1) If the bus travel time is greater than 1hr then it should not be displayed.
2) If the both the travel agency having same time of dept and arrival then we need to show only volvo travel time and ignore BMW.
3) we need to format O/P as
a) if busA starts at 10 and arrives at 11, if busB starts at 10:05 and arrives at 11, then busB has to come before busA.
b) if busA starts late and arrives before busB,then busA has to come before busB.
c) if busA starts at same time as busB but reaches before,then busA has to come before busB.

Input file:


volvo 10:10 11:05
volvo 10:15 11:05
volvo 11:00 12:15
volvo 11:50 12:50
volvo 12:00 12:45
BMW 10:05 11:05 
BMW 10:10 11:05
BMW 10:50 11:45
volvo 10:50 11:45
BMW 11:05 12:15

Output:
volvo 10:15 11:05
volvo 10:10 11:05
BMW 10:05 11:05
volvo 10:50 11:45
volvo 12:00 12:45
volvo 11:50 12:50

BusInfoVO: Its a Data transfer object which holds the information of a bus.
package com.exer.files.vo;

import java.io.Serializable;
import java.util.Date;

public class BusInfoVO implements Comparable<BusInfoVO>, Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private String busType;

 private Date startTime;

 private Date endTime;

 private transient Long duration;
 
 public String getBusType() {
  return busType;
 }

 public void setBusType(String busType) {
  this.busType = busType;
 }

 public Date getStartTime() {
  return startTime;
 }

 public void setStartTime(Date startTime) {
  this.startTime = startTime;
 }

 public Date getEndTime() {
  return endTime;
 }

 public void setEndTime(Date endTime) {
  this.endTime = endTime;
 }

 public Long getDuration() {
  return duration;
 }

 public void setDuration(Long duration) {
  this.duration = duration;
 }

 @Override
 public int compareTo(BusInfoVO busInfo) {
  
  if((this.getStartTime().equals(busInfo.getStartTime())) && (this.getEndTime().equals(busInfo.getEndTime()))) {
   return 0;
  } if((this.getStartTime().equals(busInfo.getStartTime())) || (this.getEndTime().equals(busInfo.getEndTime()))) {
   return this.getDuration() < busInfo.getDuration() ? -1 : 1;
  } else if((this.getStartTime().after(busInfo.getStartTime())) && (this.getEndTime().before(busInfo.getEndTime()))) {
   return -1;
  } else if(this.getStartTime().before(busInfo.getStartTime())) {
   return -1;
  }
  return 1;
 }
 
 @Override
 public boolean equals(Object busInfo) {
  boolean result = false;
  
  if (busInfo == null || (this.getClass() != busInfo.getClass())) {
   return result;
  }
  
  BusInfoVO other = (BusInfoVO) busInfo;
  if((this.getStartTime().equals(other.getStartTime())) && (this.getEndTime().equals(other.getEndTime()))) {
   return true;
  }
  
  return result;
 }
 
 @Override
 public int hashCode() {
  int hash = 10;
  hash = hash + this.getStartTime().getHours() + this.getStartTime().getMinutes();
  hash = hash + this.getEndTime().getHours() + this.getEndTime().getMinutes();
  return hash;
 }

}


Here BusInfoVO class implementing Comparable interface and overrides compareTo() method.

FilesReader.java: Where data can be read and processed as expected output.
package com.exe.files.reader;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;

import com.exer.files.vo.BusInfoVO;

public class FilesReader {

 private static DateFormat dateFormat = new SimpleDateFormat("HH:mm");
 
 private List<BusInfoVO> busInfoVOList;

 public List<BusInfoVO> getBusInfoVOList(String path) {
  if(path != null) {
   File folder = new File(path);
   File[] listOfFiles = folder.listFiles(); 
   busInfoVOList = new ArrayList<BusInfoVO>();
   for (int i = 0; i < listOfFiles.length; i++) {
    try (Scanner scanner = new Scanner(listOfFiles[i])) {
     while (scanner.hasNextLine()) {
      String[] info = scanner.nextLine().toString().split(" ");
      if (info != null && info.length > 0) {
       BusInfoVO vo = new BusInfoVO();
       vo.setBusType(info[0]);
       Date startTime = dateFormat.parse(info[1]);
       Date endTime = dateFormat.parse(info[2]);
       vo.setStartTime(startTime);
       vo.setEndTime(endTime);
       vo.setDuration((endTime.getTime() - startTime.getTime()) / (60 * 1000)); // In minutes.
       if(vo.getDuration() <= 60) {
        busInfoVOList.add(vo);
       }
      }
     }
    } catch(ParseException | FileNotFoundException exp) {
     exp.printStackTrace();
    }
   }
   Collections.sort(busInfoVOList, new Comparator<BusInfoVO>() {

    @Override
    public int compare(BusInfoVO o1, BusInfoVO o2) {
     return o2.getBusType().compareTo(o1.getBusType());
    }
   });
   return busInfoVOList;
  }
  
  return null;
 }
 
 public static void main(String[] args) throws ParseException, FileNotFoundException {
  String path = "D:\\PARAMESH\\FILES";
  FilesReader reader = new FilesReader();
  List<BusInfoVO> list = reader.getBusInfoVOList(path);
  TreeSet<BusInfoVO> set = new TreeSet<BusInfoVO>();
  for(BusInfoVO vo : list) {
   set.add(vo);
  }

  for(BusInfoVO vo : set) {
   System.out.println(vo.getBusType() + " " + dateFormat.format(vo.getStartTime()) + " " + dateFormat.format(vo.getEndTime()));
  }
 }
}

Here, we used(@ L 50) "sort(List<T> list, Comparator c)" method to sort the list based on the name field of BusInfoVO.

While adding to the set.add(vo) (@L 69), its looks for the equals() method definition available in BusInfoVO class and compares the given object with all existing objects in the set.
If equals returns : true - Will add the given object to the set.
                             false - Will not add the given object to the set.
Also, when we add the elements in TreeSet, it internally calls compareTo() method available in the given for sorting.

Thursday 13 March 2014

Best approach for formatting date in java


The most common approach to format the date as required by using SimpleDateFormat.


This approach causes problems when multiple threads access the same instance of the class variable, due to lack of synchronization.


The below are the most frequent exceptions will occurs:
  • java.lang.NumberFormatException
  • java.lang.ArrayIndexOutOfBoundsException

There are two approaches to handle these exceptions in Multi-threading environment.
  1. By using ThreadLocal<SimpleDateFormat>: ThreadLocal is a best appraoch to implement Per Thread Singleton classes or per thread context information.
  2. FastDateFormat.getInstance(): FastDateFormat is available in commons-lang-version.jar. it has several overloaded methods of getInstance() to provide the date format for Thread-safe environment. Please find the API documents here.

Sample code snippet by using SimpleDateFormat:
package com.samples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {
 
 private static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.format(currentDate));
 }

}



Sample code snippet by using ThreadLocal:
package com.samples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadLocalDateFormatExample {

 private static ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
  protected DateFormat initialValue() {
   return new SimpleDateFormat("MM/dd/yy");
  }
 };
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.get().format(currentDate));
 }

}


Sample code snippet by using FastDateFormat:
package com.samples;

import java.util.Date;

import org.apache.commons.lang.time.FastDateFormat;

public class FastDateFormatExample {

 private static final FastDateFormat dateFormat = FastDateFormat.getInstance("dd-MM-yyyy");
 
 public static void main(String[] args) {
  Date currentDate = new Date();
  System.out.println("Current Time: " + dateFormat.format(currentDate));
 }
}

Hence, the best approach is to format the date isby using FastDateFormat if your project has commons-lang.jar dependency, otherwise can use Threadlocal approach.