messif.objects.nio
Class CachingSerializator<T>

java.lang.Object
  extended by messif.objects.nio.BinarySerializator
      extended by messif.objects.nio.MultiClassSerializator<T>
          extended by messif.objects.nio.CachingSerializator<T>
Type Parameters:
T - default class used when reading serialized object
All Implemented Interfaces:
java.io.Serializable

public class CachingSerializator<T>
extends MultiClassSerializator<T>

This class extends the deserializing support of MultiClassSerializator with caching. The constructors/factory methods of all the cachedClasses are cached and thus this serializator is quite fast when deserializing from binary stream. If the class is not predefined, but supports BinarySerializable interface, there will be an additional cost of the class name and constructor/factory method lookup. This serializator checks the serialUIDs of the cached objects for changes.

See Also:
Serialized Form

Field Summary
protected  java.util.Map<java.lang.Class<? extends BinarySerializable>,java.lang.Integer> cachedClasses
          The hash table of cached classes with references to the constructor/factory method lists
protected  java.util.List<java.lang.reflect.Constructor<?>> cachedConstructors
          The list of constructors for the cached classes with the exactly the same order as specified by cachedClasses
protected  java.util.List<java.lang.reflect.Method> cachedFactoryMethods
          The list of factory methods for the cached classes with the exactly the same order as specified by cachedClasses
 
Fields inherited from class messif.objects.nio.MultiClassSerializator
CLASSNAME_SERIALIZATION, constructor, defaultClass, DEFAULTCLASS_SERIALIZATION, factoryMethod, JAVA_SERIALIZATION
 
Fields inherited from class messif.objects.nio.BinarySerializator
log
 
Constructor Summary
CachingSerializator(java.lang.Class<? extends T> defaultClass, java.lang.Class... cachedClasses)
          Creates a new instance of CachingSerializator.
 
Method Summary
protected  int addToCache(java.lang.Class<? extends BinarySerializable> classToAdd)
          Add the specified class to cache.
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this serializator.
protected  int getClassIndex(BinarySerializable object)
          Returns the index of the predefined class for this object.
protected  java.lang.reflect.Constructor<?> getConstructor(byte flag, java.lang.Class<?> objectClass)
          Returns a constructor for the specified flag and object class.
protected  java.lang.reflect.Method getFactoryMethod(byte flag, java.lang.Class<?> objectClass)
          Returns a factory method for the specified flag and object class.
 int hashCode()
          Returns a hash code value for this serializator.
 
Methods inherited from class messif.objects.nio.MultiClassSerializator
getBinarySize, getDefaultClass, readObject, readObjectImpl, write
 
Methods inherited from class messif.objects.nio.BinarySerializator
getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getNativeSerializableConstructor, getNativeSerializableFactoryMethod, getSerialVersionUID, getSerialVersionUIDHash, readBoolean, readBooleanArray, readByte, readByteArray, readDouble, readDoubleArray, readFloat, readFloatArray, readChar, readCharArray, readInt, readIntArray, readLong, readLongArray, readObject, readObject, readObjectSize, readShort, readShortArray, readString, skipObject, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cachedClasses

protected final java.util.Map<java.lang.Class<? extends BinarySerializable>,java.lang.Integer> cachedClasses
The hash table of cached classes with references to the constructor/factory method lists


cachedConstructors

protected transient java.util.List<java.lang.reflect.Constructor<?>> cachedConstructors
The list of constructors for the cached classes with the exactly the same order as specified by cachedClasses


cachedFactoryMethods

protected transient java.util.List<java.lang.reflect.Method> cachedFactoryMethods
The list of factory methods for the cached classes with the exactly the same order as specified by cachedClasses

Constructor Detail

CachingSerializator

public CachingSerializator(java.lang.Class<? extends T> defaultClass,
                           java.lang.Class... cachedClasses)
                    throws java.lang.IllegalArgumentException
Creates a new instance of CachingSerializator. The constructors/factory methods of the cachedClasses are cached.

If any of the items of this the list of predefined classes is replaced or deleted, the deserialization of the older binary stream will fail. Appending the list is safe.

Parameters:
defaultClass - the default class that is used for deserialization when a class is not specified
cachedClasses - the classes that are used frequently and should be cached
Throws:
java.lang.IllegalArgumentException - if there is an invalid value in cachedClasses
Method Detail

addToCache

protected int addToCache(java.lang.Class<? extends BinarySerializable> classToAdd)
                  throws java.lang.IllegalArgumentException
Add the specified class to cache.

Parameters:
classToAdd - the class to add
Returns:
the position in the cache
Throws:
java.lang.IllegalArgumentException - if the class has invalid constructor/factory method or there is already another class on this position

getConstructor

protected java.lang.reflect.Constructor<?> getConstructor(byte flag,
                                                          java.lang.Class<?> objectClass)
                                                   throws java.lang.IllegalArgumentException
Returns a constructor for the specified flag and object class. If the object was serialized as one of the predefined classes, the cached constructor is used. For the DEFAULTCLASS serialization, current constructor is returned. If the stored object implements the BinarySerializable interface, its constructor is looked up. Otherwise, null is returned.

Overrides:
getConstructor in class MultiClassSerializator<T>
Parameters:
flag - the type of deserialization (see constants)
objectClass - the class that is expected to be in the stream
Returns:
a constructor for the specified flag and object class
Throws:
java.lang.IllegalArgumentException - if there was a problem getting the constructor

getFactoryMethod

protected java.lang.reflect.Method getFactoryMethod(byte flag,
                                                    java.lang.Class<?> objectClass)
                                             throws java.lang.IllegalArgumentException
Returns a factory method for the specified flag and object class. If the object was serialized as one of the predefined classes, the cached factory method is used. For the DEFAULTCLASS serialization, current factory method is returned. If the stored object implements the BinarySerializable interface, its factory method is looked up. Otherwise, null is returned.

Overrides:
getFactoryMethod in class MultiClassSerializator<T>
Parameters:
flag - the type of deserialization (see constants)
objectClass - the class that is expected to be in the stream
Returns:
a constructor for the specified flag and object class
Throws:
java.lang.IllegalArgumentException - if there was a problem getting the factory method

getClassIndex

protected int getClassIndex(BinarySerializable object)
Returns the index of the predefined class for this object.

Overrides:
getClassIndex in class MultiClassSerializator<T>
Parameters:
object - the object whose class is looked up
Returns:
the index of the predefined class for this object

hashCode

public int hashCode()
Returns a hash code value for this serializator. It is based on the serialVersionUIDs of the cached classes.

Overrides:
hashCode in class MultiClassSerializator<T>
Returns:
a hash code value for this serializator

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this serializator. In particular, the other object must be CachingSerializator and have the same cached classes.

Overrides:
equals in class MultiClassSerializator<T>
Parameters:
obj - the reference object with which to compare
Returns:
true if this serializator is the same as the obj argument; false otherwise.