planet.generic.commonapi.factory
Class IdFactoryImpl

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

public class IdFactoryImpl
extends java.lang.Object
implements IdFactory

This Factory generate uniques Id from a material.

Author:
Jordi Pujol 07-jul-2005
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.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()
          Builds the IdFactory.
 
Method Summary
 java.util.Iterator buildDistributedIds(int 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.
 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 initDistributedAttr()
          Initializes the protected attributes to permits the uniform distribution of Ids.
 IdFactory setValues(java.lang.Class idClass, java.lang.String topology, int networkSize)
          Sets the specified initial values.
 
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

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()
              throws InitializationException
Builds the IdFactory. Does nothing. Requires the setValues(...) method invokation.

Throws:
InitializationException - if occurs some error during initialization.
Method Detail

setValues

public IdFactory setValues(java.lang.Class idClass,
                           java.lang.String topology,
                           int networkSize)
                    throws InitializationException
Sets the specified initial values.

Specified by:
setValues in interface IdFactory
Parameters:
idClass - Class reference for the current Id implementation.
topology - Desired network topology.
networkSize - Desired network size.
Returns:
The same instance once it has been updated.
Throws:
InitializationException - if any error occurs during the initialization process.
See Also:
IdFactory.setValues(java.lang.Class, java.lang.String, int)

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

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(int 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.