⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idfactoryimpl.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package planet.generic.commonapi.factory;

import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.Random;

import planet.commonapi.Id;
import planet.commonapi.exception.InitializationException;
import planet.commonapi.factory.IdFactory;
import planet.util.Utilities;

/**
 * This Factory generate uniques Id from a material.
 * @author <a href="mailto: jordi.pujol@estudiants.urv.es">Jordi Pujol</a>
 * 07-jul-2005
 */
public class IdFactoryImpl implements IdFactory {
	/**
	 * Properties file name whose read properties.
	 */
	protected String propertiesFile = "";
	
	
	/* ********* PROPERTIES INTO THE CONFIGURATION FILE *************/
	
	/**
	 * The default Class for Ids specified in the properties file.
	 */
	protected static final String defaultIdClass = "DEFAULT_IDCLASS";
	/**
	 * The default topology of the target network specified in the properties file.
	 */
	protected static final String defaultTopology = "DEFAULT_TOPOLOGY";
	/**
	 * The default size key for the target network specified in the properties file.
	 */
	protected static final String defaultNetworkSize = "DEFAULT_SIZE";
	
	/* END ********* PROPERTIES INTO THE CONFIGURATION FILE *************/
	
	/* ************************* VALUES OF PROPERTIES ************************/
	/**
	 * Actual instance of Class for new Ids.
	 */
	protected Class idClass = null;
	/**
	 * Id.divide(long) method for the actual Id implementation.
	 */
	protected transient Method idDivideMethod = null;
	/**
	 * Topology of the actual target network. It is necessary to know how
	 * generate the new Ids of Nodes.
	 */
	protected String topology = null;
	/**
	 * Network size for the target network.
	 */
	protected int networkSize;
	
	/* END ************************* VALUES OF PROPERTIES ********************/
	
	/* *********************** ATTRIBUTES TO BUILD DISTRIBUTED ID ************/
	
	/**
	 * Id just generated. 
	 */
	protected Id actualValue = null;
	/**
	 * Actual value to add to the <b>actualValue</b> to obtain the next Id.
	 */
	protected Id chunkValue = null;
	/**
	 * Number of Ids generated actually. Its value is important for the
	 * Circular networks.
	 */
	protected int actualBuildsIds;
	
	/* END ******************* ATTRIBUTES TO BUILD DISTRIBUTED ID ************/
	
	/**
	 * Random generator for random Id's. 
	 */
	protected Random random = null;
	
	
	/**
	 * Builds the IdFactory. Does nothing. Requires the <b>setValues(...)</b> 
     * method invokation.
	 * @throws InitializationException if occurs some error during initialization.
	 */
	public IdFactoryImpl() throws InitializationException {}
    
    /**
     * Sets the specified initial values.
     * @param idClass Class reference for the current Id implementation.
     * @param topology Desired network topology.
     * @param networkSize Desired network size.
     * @return The same instance once it has been updated.
     * @throws InitializationException if any error occurs during the
     * initialization process.
     * @see planet.commonapi.factory.IdFactory#setValues(java.lang.Class, java.lang.String, int)
     */
    public IdFactory setValues(Class idClass,String topology,int networkSize) throws InitializationException
    {
		//get constructors for idClass
		this.idClass = idClass;
		
		try {
			Class types[]  = {int.class};
			idDivideMethod = idClass.getMethod("divide",types);
		} catch (Exception e) {
			throw new InitializationException("Cannot obtain divide(int) method for '"+ idClass.getName() +"'.",e);
		}
		
		//read defaultTopology of network
		this.topology = topology;
		if (!Topology.isValid(topology))
			throw new InitializationException("The topology '"+topology+"' is not a valid topology to build new Ids.");
		
		//initialize Random generator
		this.random = new Random();
		
		//read the defaultSize of network.
		this.networkSize = networkSize;
		if (this.networkSize < 0) {
			throw new InitializationException("The network size '"+networkSize+"' are invalid.");
		}
		
		initDistributedAttr();
        return this;
	}
	
	/**
	 * Initializes the protected attributes to permits the uniform
	 * distribution of Ids. 
	 * @throws InitializationException if an error occurs during
	 * their initialization.
	 */
	protected void initDistributedAttr() throws InitializationException {
		if (this.networkSize != 0) {
			//initialize actualValue and chunkValue to generate distributed Ids (for CIRCULARs networks)
		    
			//generate the chunkValue
			try {
			    Object params[]  = {new Integer(this.networkSize)};
				chunkValue = (Id)idDivideMethod.invoke(null,params); //is a statical method
			} catch (Exception e) {
				throw new InitializationException("The chunk value for Ids cannot be initialized.",e);
			}

            //verifying a correct value for the chunk
            if (chunkValue == null)
            {
                throw new InitializationException("The chunk value for Ids cannot be initialize (divide method returns null)");
            }
            
			//generate the actualValue (-chunkValue)
			try{
				actualValue = (Id) idClass.newInstance();
				actualValue = actualValue.subtract(chunkValue);
			} catch (Exception e) {
				throw new InitializationException("The initial value for the first Id cannot be initialized.",e);
			}
		}
		//sets the actual number of Ids generated (0)
		actualBuildsIds = 0;
	}
	
	
	/**
	 * 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.
	 * @return A new Id generated with the actual configuration.
	 */
	public Id buildId() throws InitializationException {
		Id toReturn = null;
		if (this.topology.equalsIgnoreCase(Topology.RANDOM)) {
			toReturn = buildRandomId();
		} else if (this.topology.equalsIgnoreCase(Topology.CIRCULAR)) {
			if (this.actualBuildsIds >= this.networkSize ) {
				throw new InitializationException("Cannot build a new instance of ["+ idClass.getName()
						+"]. The topology network is ["+ Topology.CIRCULAR +"] and just generated all possible Ids ["+
						this.networkSize+"].");
			}
			this.actualValue = this.actualValue.add(this.chunkValue);
			this.actualBuildsIds++;
			toReturn = actualValue;
		} else {
			throw new InitializationException("Cannot build a new Id below the actual network topology ["+
					this.topology +"].");
		}
		return toReturn;
	}
	
	/**
	 * Builds a random Id using the Random constructor of the target Id.
	 * @return A new Id built randomly.
	 * @throws InitializationException if an error occurs during the
	 * initialization of the Id.
	 */
	public Id buildRandomId() throws InitializationException {
	    try {
            return ((Id)idClass.newInstance()).setValues(random);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl.buildRandomId: cannot build a new random Id");
        }
	}
	
	/**
	 * Builds an Id with the double parameter as its internal value.
	 * @see planet.commonapi.factory.IdFactory#buildId(double)
	 * @param material double with the internal value for the new Id.
	 * @return 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.
	 */
	public Id buildId(double material) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(material);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }
	}
	
	/**
	 * Builds an Id with the int parameter as its internal value.
	 * @see planet.commonapi.factory.IdFactory#buildId(int)
	 * @param material Int with the internal value for the new Id.
	 * @return 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.
	 */
	public Id buildId(int material) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(material);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -