Sunday, May 11, 2014

Difference between Spring Singleton and JVM Singleton

The Java singleton is scoped by the Java class loader, the Spring singleton is scoped by the container context.
Which basically means that, in Java, you can be sure a singleton is a truly a singleton only within the context of the class loader which loaded it. Other class loaders should be capable of creating another instance of it (provided the class loaders are not in the same class loader hierarchy), despite of all your efforts in code to try to prevent it.
In Spring, if you could load your singleton class in two different contexts and then again we can break the singleton concept.
So, in summary, Java considers something a singleton if it cannot create more than one instance of that class within a given class loader, whereas Spring would consider something a singleton if it cannot create more than one instance of a class within a given container/context.
A Java singleton, per the design pattern where instantiation is restricted to one, usually per JVM class loader by the code.

A Spring singleton bean can be any normal class you write, but declaring it's scope as singleton means that Spring will only create one instance and provide its reference to all beans that reference the declared bean. You may have many instances of that class in your application, but only one will be created for that bean. You may even have multiple beans of the same class all declared as singleton. Each bean will create exactly one instance of the class.

Friday, May 9, 2014

Comparator and Comparable in Java

Comparator: Comparator is an interface which is defined in java.util package which means Comparator should be used as a utility to sort objects. Comparator interface in Java has method public int compare (Object o1, Object o2) which is used to compare two different objects.
Let’s see below example.
Student.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

 class Student {
           
            private int marks;
private String name;
            /**
             * @return the marks
             */
            public int getMarks() {
                        return marks;
            }

            /**
             * @return the name
             */
            public String getName() {
                        return name;
            }

            public Student(int marks, String name){
                        this.marks = marks;
                        this.name = name;
                       
            }
            /**
             * @param args
             */
            public static void main(String[] args) {
                        List<Student> studentList = new ArrayList<Student>();
                        studentList.add(new Student(50, "A"));
                        studentList.add(new Student(40, "B"));
                        studentList.add(new Student(60, "C"));
                        studentList.add(new Student(20, "D"));
                        studentList.add(new Student(30, "E"));
                       
                        StudentSortByMarks studentSortByMarks = new StudentSortByMarks();
                        Collections.sort(studentList,studentSortByMarks );
                        for(Student st : studentList) {
                        System.out.println(st.getMarks() + " :::: " + st.getName());
                        }
                        }
            }
           

Let’s create an interface StudentSortByMarks

StudentSortByMarks.java

import java.util.Comparator;
public class StudentSortByMarks implements Comparator<Student> {
            public int compare(Student st1, Student st2) {
                        return st1.getMarks() - st2.getMarks();
                        }          
}
Output:
20 :::: D
30 :::: E
40 :::: B
50 :::: A
60 :::: C
In the above example we have create a class Student. Two instance variables marks and name are declared and values are assigned through a constructor. Two getter methods are declared with which Student marks and name are retrieved later.
The Comparator is implemented by StudentSortByMarks where we are comparing marks of students with method int compare (Student st1, Student st2)
After that when we are running the main () method and After the studentList is sorted, the elements are printed with enhanced for loop and above output is getting generated.

Comparable: Comparable interface in Java is defined in java.lang package. Comparable is implemented by a class in order to be able to comparing object of itself with some other objects. Comparable interface has method public int compareTo (Object o)
Let’s see below example.
Student.java: The Student class implements Comparable interface. Here, Comparable is designed to be generics; that is, Comparable object compares only Student objects.
Two instance variables marks and name are declared and values are assigned through a constructor. Two getter methods are declared with which Student marks and name are retrieved later.
class Student implements Comparable<Student> {

       private int marks;
       private String name;
       /**
        * @return the marks
        */
       public int getMarks() {
              return marks;
       }
       /**
        * @return the name
        */
       public String getName() {
              return name;
       }
        public Student(int marks, String name) {
               this.marks = marks;
               this.name = name;
               }
        
        public int compareTo(Student st1) {
               return this.marks - st1.marks;
               }
        }

StudentSortByMarks.java
Another class StudentSortByMarks is created which creates some Student objects and adds them to ArrayList studentList. The ArrayList object studentList is passed to sort() method of Collections class. The sort() method sorts as per the Comparable interface compareTo() method as Student class implements Comparable interface.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StudentSortByMarks {
                         public static void main(String args[]) {
                                     List<Student> studentList = new ArrayList<Student>();
                                     studentList.add(new Student(50, "A"));
                                     studentList.add(new Student(40, "B"));
                                     studentList.add(new Student(60, "C"));
                                     studentList.add(new Student(20, "D"));
                                     studentList.add(new Student(30, "E"));
                                     Collections.sort(studentList);
                                     for(Student st : studentList) {
                                                 System.out.println(st.getMarks() + " : " + st.getName());
                                                 }
                                     }
                         }
Output:
20 : D
30 : E
40 : B
50 : A
60 : C

Difference between Comparator and Comparable in Java

Parameter
Comparator
Comparable
Definition
Comparator in Java is defined in java.util package which means Comparator should be used as an utility to sort objects
java.util.Comparator
Comparable interface is defined in java.lang package, which should be provided by default
java.lang.Comparable
Sorting logic
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.
Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects
Sorting Strategy
if you want to sort on some other attribute of object then use Comparator
if you want to sort objects based on natural order then use Comparable
Implementation
Class whose objects to be sorted do not need to implement this interface. Some other class can implement this interface.
Ex. StudentSortByMarks class can implement Comparator interface to sort collection of students object by marks
Class whose objects to be sorted must implement this interface.
 Ex. StudentSortByMarks  class needs to implement comparable to collection of students object by marks
Sorting method
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
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
Calling method
Collections.sort(List, Comparator)
Here objects will be sorted on the basis of Compare method in Comparator
Collections.sort(List)
Here objects will be sorted on the basis of CompareTo method

Thursday, May 8, 2014

JSR 286 vs JSR 168

Features
JSR 168 Specification
JSR 286 Specification
Inter Portlet Communication
·  Only supported within the same portlet application using session attributes
· Target portlets will only "see" messages during next render request.

Add additional coordination capabilities
· Limited only to String Values.
· Sharing of session data beyond the current Portlet application.
· Sharing of render parameters across portlets.
Life cycle
Portlets cannot update their state during a render request: "event" handling not really possible
New 3rd life cycle phase before rendering
Portlet Filters
Doesn't Support
· Supports Allow on the fly transformations of information in both the request to and the response from the portlet
·  Defined in portlet.xml
Caching

· Extended Cache support.
·Allow public cached content for multiple users
Common Web Frameworks
· Servlet dispatching not supported from process Action.
· Needs Portals Bridges or similar solutions.
· JSTL support very limited

·  Extended Cache support.
· Allow public cached content for multiple users.
· Improved support for web frameworks (Struts, JSF, Spring) Allow servlet dispatching during all lifecycle calls: processAction, processEvent, render, serverResource.
· Extended JSP tag library <defineObjects/>, support for JSF
Non HTML Resources(pdf, doc, images etc.)
·  A portlet can only render html fragments.
· Have to fallback/delegate to the servlet container.
· Requires coordination between portlet and servlet.