Thursday, December 25, 2014

How to create your own Unmodifiable List in Java ?

java.util.Collections class offers numerous utility methods, mostly static ones, to operate collection objects effectively.

This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends. One of the most used utility from this class is unmodifiableCollections or lists.

 So today we will see how we can implement this feature using Java but before we go into more details; let's first understand what unmodifiableList offers to us.

Features:

Like UnmodifiableCollections it returns the wrapper object of the original list which is read-only view of original list. However, important thing to remember here is that it's not immutable list and so state of the object can be changed by modifying original list object.

For ex, any modification made to the original list will be visible to unmodifiable object reference as well.

So now let's see how we can achieve this features by our own custom implementations.

Implementation:

We will need two main classes: MyCollections class to provide the static method which will return our wrapper object and MyUnmodifiableList class which will be the wrapper here to be returned to the caller.

So as it's very much evidant from the API call that unmodifiableList method will be a static method

public class MyCollections {

    public final static  List unmodifiableList(final List list) {
        return (List) new MyUnmodifiableList(list);
    }
}


Since our static method returns the MyUnmodifiableList, we will need concrete implementation of this class. So what do we need from this class ??

1. It should be type of List in order to keep List reference.
2. It should maintain the reference to the original list since we will need to reflect the updates made to the original list.
3. It should not allow any modifications to this object.

So essentially it should behave like the way view does on database table .

To satisfy point #1 here, we will need to implement List interface so class definition will become like this...

public class MyUnmodifiableList implements List {

Now, to get point #2 right,  will need to have List reference in the class;

    List originalList;


Remember, this variable has to keep reference to the originalList and so you might start getting confused now because you kept hearing this whole your life that "java is pass by value and not by reference". Then how on the earth you can keep the reference here to the original List !!

Well, everything you heard is right that Java is pass by value only but in case of objects, it passes the object reference by the value and so any change in original list will still reflect in MyUnmodifiableList reference. Excellent, so we are almost there.....

So last but not least, for point #3, all you need is to keep this class Immutable as the definition says and so what are the methods which can change your object state ??

To name few: add(), addAll(), remove(), removeAll() and few more. Yes, will need to override all of List interface methods here and so you'll need to update it accordingly so that it doesnt support the object state to change.


    public boolean add(E e) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException();
    }


So finally we are done now as we have satisfied all of the above conditions and requirements mentioned above for the unmodifiableList interface.

Why I chose this particular example is because it checks lot of core concepts of Java & OOPS. So if you carefully observe this example, it touched upon OOPS (List interface), Program against Interface, Composition over inheritance, Pass by value or reference , Immutable Objects, Exception Handling etc......

If you could implement or understand this example means, you know all of this concepts and  ready to touch upon some more complex examples.

So keep reading.........................................!!!!



No comments:

Post a Comment