planet.generic.commonapi.factory
Class IdFactoryImpl

java.lang.Object
  extended byplanet.generic.commonapi.factory.IdFactoryImpl
All Implemented Interfaces:
IdFactory, java.io.Serializable
Direct Known Subclasses:
IdFactoryDoubleImpl

public class IdFactoryImpl
extends java.lang.Object
implements IdFactory

This Factory generate uniques Id from a material.

Author:
Jordi Pujol
See Also:
Serialized Form

Field Summary
protected  int actualBuildsIds
          Number of Ids generated actually.
protected  Id actualValue
          Id just generated.
protected  Id chunkValue
          Actual value to add to the actualValue to obtain the next Id.
protected static java.lang.String defaultIdClass
          The default Class for Ids specified in the properties file.
protected static java.lang.String defaultNetworkSize
          The default size key for the target network specified in the properties file.
protected static java.lang.String defaultTopology
          The default topology of the target network specified in the properties file.
protected  java.lang.Class idClass
          Actual instance of Class for new Ids.
protected  java.lang.reflect.Constructor idClassConstructorBigInteger
          Constructor of actual idClass with only one parameter: a BigInteger.
protected  java.lang.reflect.Constructor idClassConstructorByteArray
          Constructor of actual idClass with only one parameter: a byte array.
protected  java.lang.reflect.Constructor idClassConstructorDouble
          Constructor of actual idClass with only one parameter: a double.
protected  java.lang.reflect.Constructor idClassConstructorInt
          Constructor of actual idClass with only one parameter: an int.
protected  java.lang.reflect.Constructor idClassConstructorIntArray
          Constructor of actual idClass with only one parameter: an int array.
protected  java.lang.reflect.Constructor idClassConstructorString
          Constructor of actual idClass with only one parameter: a String.
protected  int networkSize
          Network size for the target network.
protected  java.lang.String propertiesFile
          Properties file name whose read properties.
protected  java.util.Random random
          Random generator for random Id's.
protected  java.lang.String topology
          Topology of the actual target network.
 
Constructor Summary
IdFactoryImpl(java.lang.Class idClass, java.lang.String topology, int networkSize)
          Constructor that builds an instance with these parameters
IdFactoryImpl(java.lang.String propertiesFile)
          This constructor reads the properties' file and initializes the IdFactory and Class for new Nodes.
 
Method Summary
 Id buildId()
          Builds an Id with the actual configuration of network topology and size.
 Id buildId(java.math.BigInteger bigNumber)
          Generate an Id from the BigInteger as its internal value.
 Id buildId(byte[] material)
          Generate an Id from material in byte[] format as its internal value.
 Id buildId(double material)
          Builds an Id with the double parameter as its internal value.
 Id buildId(int material)
          Builds an Id with the int parameter as its internal value.
 Id buildId(int[] material)
          Generate an Id from material in int[] format, as its internal value.
 Id buildId(java.lang.String material)
          Generate an Id from a String that contains its internal value.
 Id buildKey(java.lang.String string)
          Generate an Id from a String with SHA-1 hash function.
protected  Id buildRandomId()
          Builds a random Id using the BigInteger constructor of the target Id.
protected  void init(java.lang.Class idClass, java.lang.String topology, int networkSize)
          Initialize the new instance with correct values to generate new Ids.
protected  void initDistributedAttr()
          Initializes the protected attributes to permits the uniform distribution of Ids.
protected  void readIdFactoryObject(java.io.ObjectInputStream stream)
          This method must be invoked by the different IdFactory implementation to recover all Constructors that this class contains.
protected  void writeIdFactoryObject(java.io.ObjectOutputStream stream)
          Makes nothing special, only invokes to stream.defaultWriteObject().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

propertiesFile

protected java.lang.String propertiesFile
Properties file name whose read properties.


defaultIdClass

protected static final java.lang.String defaultIdClass
The default Class for Ids specified in the properties file.

See Also:
Constant Field Values

defaultTopology

protected static final java.lang.String defaultTopology
The default topology of the target network specified in the properties file.

See Also:
Constant Field Values

defaultNetworkSize

protected static final java.lang.String defaultNetworkSize
The default size key for the target network specified in the properties file.

See Also:
Constant Field Values

idClassConstructorInt

protected transient java.lang.reflect.Constructor idClassConstructorInt
Constructor of actual idClass with only one parameter: an int.


idClassConstructorDouble

protected transient java.lang.reflect.Constructor idClassConstructorDouble
Constructor of actual idClass with only one parameter: a double.


idClassConstructorByteArray

protected transient java.lang.reflect.Constructor idClassConstructorByteArray
Constructor of actual idClass with only one parameter: a byte array.


idClassConstructorIntArray

protected transient java.lang.reflect.Constructor idClassConstructorIntArray
Constructor of actual idClass with only one parameter: an int array.


idClassConstructorString

protected transient java.lang.reflect.Constructor idClassConstructorString
Constructor of actual idClass with only one parameter: a String.


idClassConstructorBigInteger

protected transient java.lang.reflect.Constructor idClassConstructorBigInteger
Constructor of actual idClass with only one parameter: a BigInteger.


idClass

protected java.lang.Class idClass
Actual instance of Class for new Ids.


topology

protected java.lang.String topology
Topology of the actual target network. It is necessary to know how generate the new Ids of Nodes.


networkSize

protected int networkSize
Network size for the target network.


actualValue

protected Id actualValue
Id just generated.


chunkValue

protected Id chunkValue
Actual value to add to the actualValue to obtain the next Id.


actualBuildsIds

protected int actualBuildsIds
Number of Ids generated actually. Its value is important for the Circular networks.


random

protected java.util.Random random
Random generator for random Id's.

Constructor Detail

IdFactoryImpl

public IdFactoryImpl(java.lang.String propertiesFile)
              throws InitializationException
This constructor reads the properties' file and initializes the IdFactory and Class for new Nodes. If occur any problem with this initialization, throws a InitializationException exception with short description for the problem.

Throws:
InitializationException - If occur any problem during initialization steps.

IdFactoryImpl

public IdFactoryImpl(java.lang.Class idClass,
                     java.lang.String topology,
                     int networkSize)
              throws InitializationException
Constructor that builds an instance with these parameters

Parameters:
idClass - Id class to build.
topology - Network topology to known how build the new Ids.
networkSize - Size of the network. It is important if is used the topology Topology.CIRCULAR.
Throws:
InitializationException - if occurs some error during initialization of values.
See Also:
Topology
Method Detail

init

protected void init(java.lang.Class idClass,
                    java.lang.String topology,
                    int networkSize)
             throws InitializationException
Initialize the new instance with correct values to generate new Ids.

Parameters:
idClass - Id class to be build with this factory.
topology - Network topology of the target network.
networkSize - Number of nodes to be build to the target network.
Throws:
InitializationException - if occurs some error with the initialization process.

initDistributedAttr

protected void initDistributedAttr()
                            throws InitializationException
Initializes the protected attributes to permits the uniform distribution of Ids.

Throws:
InitializationException - if an error occurs during their initialization.

buildId

public Id buildId()
           throws InitializationException
Builds an Id with the actual configuration of network topology and size. Use the protected method buildRandomId() to build the Id if the specified topology is random.

Specified by:
buildId in interface IdFactory
Returns:
A new Id generated with the actual configuration.
Throws:
InitializationException - when an error occurs during the initialization of the Id or when this factory method is nonapplicable.

buildRandomId

protected Id buildRandomId()
                    throws InitializationException
Builds a random Id using the BigInteger constructor of the target Id.

Returns:
A new Id builded randomly. This method must be reimplemented by the IdFactories that have another requirements to build them.
Throws:
InitializationException - if an error occurs during the initialization of the Id.

buildId

public Id buildId(double material)
           throws InitializationException
Builds an Id with the double parameter as its internal value.

Specified by:
buildId in interface IdFactory
Parameters:
material - double with the internal value for the new Id.
Returns:
New Id with the double internal value.
Throws:
InitializationException - if an error occurs during the initialization of the Id or if it is nonapplicable to the target Id.
See Also:
IdFactory.buildId(double)

buildId

public Id buildId(int material)
           throws InitializationException
Builds an Id with the int parameter as its internal value.

Specified by:
buildId in interface IdFactory
Parameters:
material - Int with the internal value for the new Id.
Returns:
New Id with the int internal value.
Throws:
InitializationException - if an error occurs during the initialization of the Id or if it is nonapplicable to the target Id.
See Also:
IdFactory.buildId(int)

buildId

public Id buildId(byte[] material)
           throws InitializationException
Generate an Id from material in byte[] format as its internal value.

Specified by:
buildId in interface IdFactory
Parameters:
material - Hash code previously generated.
Returns:
A new Id with the byte[] value.
Throws:
InitializationException - if an error occurs during the initialization of the Id or if it is nonapplicable to the target Id.
See Also:
IdFactory.buildId(byte[])

buildId

public Id buildId(int[] material)
           throws InitializationException
Generate an Id from material in int[] format, as its internal value.

Specified by:
buildId in interface IdFactory
Parameters:
material - Internal value of the new Id in int[] format.
Returns:
A new Id with the int[] value.
Throws:
InitializationException - if an error occurs during the initialization of the Id or if it is nonapplicable to the target Id.
See Also:
IdFactory.buildId(int[])

buildKey

public Id buildKey(java.lang.String string)
            throws InitializationException
Generate an Id from a String with SHA-1 hash function. This method use the constructor of the implemented Id with byte[] argument.

The implementation makes the following operation:
Utilities.generateByteHash(string)

to build a byte[] with its hash value.

Specified by:
buildKey in interface IdFactory
Parameters:
string - String to apply default hash function (SHA-1) to generate Id.
Returns:
The built Id as result to apply a hash function to the given String.
Throws:
InitializationException - when an error occurs during the initialization of the Id or when this factory method is nonapplicable.
See Also:
IdFactory.buildKey(java.lang.String), planet.util.Utilities.generateByteHash(java.lang.String)

buildId

public Id buildId(java.lang.String material)
           throws InitializationException
Generate an Id from a String that contains its internal value.

Specified by:
buildId in interface IdFactory
Parameters:
material - With the String representation of the internal value.
Returns:
A new Id with the value included in the StringBuffer.
Throws:
InitializationException - when an error occurs during the initialization of the Id or when this factory method is nonapplicable.
See Also:
IdFactory.buildId(java.lang.String)

buildId

public Id buildId(java.math.BigInteger bigNumber)
           throws InitializationException
Generate an Id from the BigInteger as its internal value.

Specified by:
buildId in interface IdFactory
Parameters:
bigNumber - BigInteger with the internal value for the new Id.
Returns:
A new Id with the value included in the BigInteger.
Throws:
InitializationException - when an error occurs during the initialization of the Id or when this factory method is nonapplicable.
See Also:
IdFactory.buildId(java.math.BigInteger)

writeIdFactoryObject

protected void writeIdFactoryObject(java.io.ObjectOutputStream stream)
                             throws java.io.IOException
Makes nothing special, only invokes to stream.defaultWriteObject(). This must be invoked by the different writeObject() method.

Parameters:
stream - Stream to save the actual instance.
Throws:
java.io.IOException - if any error has ocurred.

readIdFactoryObject

protected void readIdFactoryObject(java.io.ObjectInputStream stream)
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException
This method must be invoked by the different IdFactory implementation to recover all Constructors that this class contains.

Parameters:
stream - Stream to read the actual instance.
Throws:
java.io.IOException - if occur any error during default read object, or if there are any error during building of Node constructor.
java.lang.ClassNotFoundException