messif.objects
Class LocalAbstractObjectAutoImpl

java.lang.Object
  extended by messif.objects.UniqueID
      extended by messif.objects.AbstractObject
          extended by messif.objects.LocalAbstractObject
              extended by messif.objects.LocalAbstractObjectAutoImpl
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<UniqueID>, Clearable

public abstract class LocalAbstractObjectAutoImpl
extends LocalAbstractObject

This class eases the task of implementing data read/write methods. The data fields of the subclass needs to be marked by overriding the getDataFields() method. If the newly created object cannot be descendant of this class, the static methods for reading/writing can be used.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class messif.objects.LocalAbstractObject
LocalAbstractObject.DataEqualObject
 
Field Summary
 
Fields inherited from class messif.objects.LocalAbstractObject
counterDistanceComputations, counterLowerBoundDistanceComputations, counterUpperBoundDistanceComputations, MAX_DISTANCE, MIN_DISTANCE, suppData, UNKNOWN_DISTANCE
 
Constructor Summary
protected LocalAbstractObjectAutoImpl()
          Creates a new instance of LocalAbstractObjectAutoImpl.
protected LocalAbstractObjectAutoImpl(java.io.BufferedReader stream)
          Creates a new instance of object from a text stream.
 
Method Summary
 LocalAbstractObject cloneRandomlyModify(java.lang.Object... args)
          Creates and returns a randomly modified copy of this object.
 boolean dataEquals(java.lang.Object obj)
          Indicates whether some other object has the same data as this one.
 int dataHashCode()
          Returns a hash code value for the data of this object.
protected abstract  java.lang.reflect.Field[] getDataFields()
          Returns the list of fields that should be automatically managed by AutoImpl class as data.
protected static java.lang.reflect.Field[] getFieldsForNames(java.lang.Class<? extends LocalAbstractObject> forClass, java.lang.String... fieldName)
          Returns a list of fields of the specified class that match the provided names.
 int getSize()
          Returns the size of this object in bytes.
static void readAttributesFromStream(java.lang.String line, char attributesSeparator, char arrayItemsSeparator, LocalAbstractObject dataObject, java.lang.reflect.Field... dataFields)
          Helper method for reading object primitive/array attributes from a text stream.
static void writeAttributesToStream(java.io.OutputStream stream, char attributesSeparator, char arrayItemsSeparator, LocalAbstractObject dataObject, java.lang.reflect.Field... dataFields)
          Helper method for writing object primitive/array attributes to a text stream.
protected  void writeData(java.io.OutputStream stream)
          Store this object's data to a text stream.
 
Methods inherited from class messif.objects.LocalAbstractObject
binarySerialize, clearSurplusData, clone, clone, create, create, createMetaDistancesHolder, excludeUsingPrecompDist, getBinarySize, getDistance, getDistance, getDistance, getDistanceFilter, getDistanceFilter, getDistanceFilter, getDistanceImpl, getDistanceLowerBound, getDistanceLowerBoundImpl, getDistanceUpperBound, getDistanceUpperBoundImpl, getMaxDistance, getNormDistance, getRandomChar, getRandomNormal, chainDestroy, chainFilter, includeUsingPrecompDist, readObjectComments, unchainFilter, write, write
 
Methods inherited from class messif.objects.AbstractObject
clone, getLocatorURI, getNoDataObject, getObjectID, getObjectKey, setObjectKey, toString
 
Methods inherited from class messif.objects.UniqueID
compareTo, equals, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LocalAbstractObjectAutoImpl

protected LocalAbstractObjectAutoImpl()
Creates a new instance of LocalAbstractObjectAutoImpl. New object ID is automatically created.


LocalAbstractObjectAutoImpl

protected LocalAbstractObjectAutoImpl(java.io.BufferedReader stream)
                               throws java.io.EOFException,
                                      java.io.IOException,
                                      java.lang.IllegalArgumentException
Creates a new instance of object from a text stream.

Parameters:
stream - the text stream to read one object from
Throws:
java.io.EOFException - is thrown when the end-of-file is reached
java.io.IOException - if there is an error during reading from the given stream;
java.lang.IllegalArgumentException - if the text stream contains invalid values for this object
Method Detail

writeData

protected void writeData(java.io.OutputStream stream)
                  throws java.io.IOException
Description copied from class: LocalAbstractObject
Store this object's data to a text stream. This method should have the opposite deserialization in constructor of a given object class.

Specified by:
writeData in class LocalAbstractObject
Parameters:
stream - the stream to store this object to
Throws:
java.io.IOException - if there was an error while writing to stream

getSize

public int getSize()
            throws java.lang.IllegalArgumentException
Description copied from class: LocalAbstractObject
Returns the size of this object in bytes.

Specified by:
getSize in class LocalAbstractObject
Returns:
the size of this object in bytes
Throws:
java.lang.IllegalArgumentException

dataEquals

public boolean dataEquals(java.lang.Object obj)
Description copied from class: LocalAbstractObject
Indicates whether some other object has the same data as this one.

Specified by:
dataEquals in class LocalAbstractObject
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.

dataHashCode

public int dataHashCode()
                 throws java.lang.IllegalArgumentException
Description copied from class: LocalAbstractObject
Returns a hash code value for the data of this object.

Specified by:
dataHashCode in class LocalAbstractObject
Returns:
a hash code value for the data of this object
Throws:
java.lang.IllegalArgumentException

cloneRandomlyModify

public LocalAbstractObject cloneRandomlyModify(java.lang.Object... args)
                                        throws java.lang.CloneNotSupportedException
Creates and returns a randomly modified copy of this object. One randomly selected field is modified. If the selected field is array, one randomly selected item of the array is modified.

Overrides:
cloneRandomlyModify in class LocalAbstractObject
Parameters:
args - two objects with the miminal and the maximal possible values and optional third integer parameter with index of the field to modify
Returns:
a randomly modified clone of this instance.
Throws:
java.lang.CloneNotSupportedException - if the object's class does not support clonning or there was an error

writeAttributesToStream

public static void writeAttributesToStream(java.io.OutputStream stream,
                                           char attributesSeparator,
                                           char arrayItemsSeparator,
                                           LocalAbstractObject dataObject,
                                           java.lang.reflect.Field... dataFields)
                                    throws java.io.IOException,
                                           java.lang.IllegalArgumentException
Helper method for writing object primitive/array attributes to a text stream. If the attribute is a primitive type, the value of that attribute is written. If the attribute is an instance of some class, toString method result is written. If the attribute is an array, a arrayItemsSeparator-separated list of items is written.

Parameters:
stream - the stream to output the text to
attributesSeparator - the character written to separate attributes
arrayItemsSeparator - the character written to separate items of an array attribute
dataObject - the object whose data are written
dataFields - the list of dataObject attribute fields to write to the stream
Throws:
java.io.IOException - if there was an error writing to the stream
java.lang.IllegalArgumentException - if one of the specified fields is invalid

readAttributesFromStream

public static void readAttributesFromStream(java.lang.String line,
                                            char attributesSeparator,
                                            char arrayItemsSeparator,
                                            LocalAbstractObject dataObject,
                                            java.lang.reflect.Field... dataFields)
                                     throws java.io.IOException,
                                            java.lang.IllegalArgumentException
Helper method for reading object primitive/array attributes from a text stream. If the field is a primitive type, the value of that attribute is read into respective attribute. If the field is an instance of some class, a static Object valueOf(String value) method is used to create a respective attribute object. If the field is an array, a arrayItemsSeparator-separated list of items is read into respective attribute array.

Parameters:
line - a line of text representing the data
attributesSeparator - the character that separates attributes
arrayItemsSeparator - the character that separates items of arrays
dataObject - the object whose data are read
dataFields - the list of dataObject attribute fields to read from the stream
Throws:
java.io.IOException - if there was an error reading from the stream
java.lang.IllegalArgumentException - if one of the specified fields is invalid or the value specified for a field can't be converted to correct type

getDataFields

protected abstract java.lang.reflect.Field[] getDataFields()
Returns the list of fields that should be automatically managed by AutoImpl class as data. Recommended usage in a subclass: private final static Field[] fields = getFieldsFor????(.class, ...); protected Field[] getDataFields() { return fields; } See getFieldsForNames(java.lang.Class, java.lang.String...) for explanation how to get field lists.

Returns:
the list of fields that hold data

getFieldsForNames

protected static java.lang.reflect.Field[] getFieldsForNames(java.lang.Class<? extends LocalAbstractObject> forClass,
                                                             java.lang.String... fieldName)
                                                      throws java.lang.IllegalArgumentException
Returns a list of fields of the specified class that match the provided names.

Parameters:
forClass - the class to get fields for
fieldName - the list of field names
Returns:
a list of fields of the specified class that match the provided names
Throws:
java.lang.IllegalArgumentException - if there is no attribute field with the specified name