planet.generic.commonapi.factory
Class IdFactoryImpl

java.lang.Object
  extended by planet.generic.commonapi.factory.IdFactoryImpl
All Implemented Interfaces:
java.io.Serializable, IdFactory

public class IdFactoryImpl
extends java.lang.Object
implements IdFactory

This Factory generate uniques Id from a material.

Author:
Jordi Pujol
See Also:
Serialized Form

Nested Class Summary
 class IdFactoryImpl.DistributedIdIterator
          Is an Id iterator, for building up to networkSize Id.
 
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 idClassConstructorHashSpecific
          Constructor of actual idClass with two parameters: the first String with the material for the hash algorithm, and the second String with the algorithm name.
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 idClassConstructorRandom
          Constructor of actual idClass with only one parameter: a java.util.Random, for radomly built Id.
protected  java.lang.reflect.Constructor idClassConstructorString
          Constructor of actual idClass with only one parameter: a String.
protected  java.lang.reflect.Method idDivideMethod
          Id.divide(long) method for the actual Id implementation.
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
 java.util.Iterator buildDistributedIds(long desiredNetworkSize)
          The Iterator instance returned permits to build as maximum desiredNetworkSize.
 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.
protected  Id buildId(java.lang.Object[] params, java.lang.reflect.Constructor cons)
          Builds an Id with the arguments params, using the Constructor cons.
 Id buildId(java.lang.String material)
          Generate an Id from a String that contains its internal value.
 Id buildId(java.lang.String material, java.lang.String algorithm)
          Builds a new Id from an arbitray string applying a one-way hashing algorithm, such as SHA-1 or MD5.
 Id buildKey(java.lang.String string)
          Generate an Id from a String with SHA-1 hash function.
 Id buildRandomId()
          Builds a random Id using the Random 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.


idClassConstructorRandom

protected transient java.lang.reflect.Constructor idClassConstructorRandom
Constructor of actual idClass with only one parameter: a java.util.Random, for radomly built Id.


idClassConstructorHashSpecific

protected transient java.lang.reflect.Constructor idClassConstructorHashSpecific
Constructor of actual idClass with two parameters: the first String with the material for the hash algorithm, and the second String with the algorithm name.


idClass

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


idDivideMethod

protected transient java.lang.reflect.Method idDivideMethod
Id.divide(long) method for the actual Id implementation.


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.

buildId

protected Id buildId(java.lang.Object[] params,
                     java.lang.reflect.Constructor cons)
              throws InitializationException
Builds an Id with the arguments params, using the Constructor cons.

Parameters:
params - Arguments for the constructor in use.
cons - Constructor in use.
Returns:
A new Id instance.
Throws:
InitializationException - if any error has occurred during the initialization.

buildRandomId

public Id buildRandomId()
                 throws InitializationException
Builds a random Id using the Random constructor of the target Id.

Specified by:
buildRandomId in interface IdFactory
Returns:
A new Id built randomly.
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), 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 - Contains the internal value of Id in String format.
Returns:
A new Id with the value included in the String.
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.lang.String material,
                  java.lang.String algorithm)
           throws InitializationException
Builds a new Id from an arbitray string applying a one-way hashing algorithm, such as SHA-1 or MD5.

Specified by:
buildId in interface IdFactory
Parameters:
material - An arbitray string.
algorithm - One-way hashing algorithm such as "SHA" or "MD5".
Throws:
InitializationException - when an error occurs during the initialization of the Id or when this factory method is nonapplicable.
See Also:
Java Cryptography Architecture API Specification & Reference

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 of 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)

buildDistributedIds

public java.util.Iterator buildDistributedIds(long desiredNetworkSize)
                                       throws InitializationException
The Iterator instance returned permits to build as maximum desiredNetworkSize. This method goal is to obtain desiredNetworkSize equidistant Ids for a network.

Specified by:
buildDistributedIds in interface IdFactory
Parameters:
desiredNetworkSize - Number of nodes Id to obtain.
Returns:
An Iterator instance for getting all desiredNetworkSize. The Iterator.remove() method of this instance is not implemented and always throws a NoSuchMethodError error. null if desiredNetworkSize is zero.
Throws:
InitializationException - if any error has ocurred during the initialization.

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