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

📄 idfactoryimpl.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	/**
	 * Generate an Id from material in byte[] format as its internal value.
	 * @param material Hash code previously generated.
	 * @see planet.commonapi.factory.IdFactory#buildId(byte[])
	 * @throws InitializationException if an error occurs during the
	 * initialization of the Id or if it is nonapplicable to the 
	 * target Id.
	 */
	public Id buildId(byte[] material) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(material);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }
	}

	/**
	 * Generate an Id from material in int[] format, as its internal value.
	 * @param material Internal value of the new Id in int[] format.
	 * @see planet.commonapi.factory.IdFactory#buildId(int[])
	 * @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");
        }
	}

	/**
	 * Generate an Id from a String with SHA-1 hash function.
	 * This method use the constructor of the implemented Id with 
	 * byte[] argument.
	 * <br><br>
	 * The implementation makes the following operation:
	 * <br>
	 * <center><b>Utilities.generateByteHash(string)</b></center>
	 * <br>
	 * to build a byte[] with its hash value.
	 * @param string String to apply default hash function (SHA-1) to 
	 * generate Id. 
	 * @see planet.commonapi.factory.IdFactory#buildKey(java.lang.String)
	 * @see planet.util.Utilities#generateByteHash(java.lang.String)
	 * @throws InitializationException when an error occurs during the
	 * initialization of the Id or when this factory method is
	 * nonapplicable. 
	 */
	public Id buildKey(String string) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(Utilities.generateByteHash(string));
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }
	}
	
	/**
	 * Generate an Id from a String that contains its internal value.
	 * @param material Contains the internal value of Id in String format. 
	 * @see planet.commonapi.factory.IdFactory#buildId(java.lang.String)
	 * @throws InitializationException when an error occurs during the
	 * initialization of the Id or when this factory method is
	 * nonapplicable. 
	 */
	public Id buildId(String material) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(material);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }
    }
    
	/**
     * Builds a new Id from an arbitray string applying a one-way hashing algorithm,
	 * such as SHA-1 or MD5.
	 * @param material An arbitray string. 
	 * @param 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. 
	 */
	public Id buildId(String material, String algorithm) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(material,algorithm);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }

	}
	
	/**
	 * Generate an Id from the BigInteger as its internal value.
	 * @param bigNumber BigInteger with the internal value of the new Id. 
	 * @see planet.commonapi.factory.IdFactory#buildId(java.math.BigInteger)
	 * @throws InitializationException when an error occurs during the
	 * initialization of the Id or when this factory method is
	 * nonapplicable. 
	 */
	public Id buildId(BigInteger bigNumber) throws InitializationException {
        try {
            return ((Id)idClass.newInstance()).setValues(bigNumber);
        } catch (Exception e) {
            throw new InitializationException("IdFactoryImpl: cannot build a new Id");
        }
	}
	
	/**
	 * The Iterator instance returned permits to build as maximum <b>desiredNetworkSize</b>.
	 * This method goal is to obtain <b>desiredNetworkSize</b> equidistant Ids for a network.   
	 * @param desiredNetworkSize Number of nodes Id to obtain.
	 * @return An Iterator instance for getting all <b>desiredNetworkSize</b>. The 
	 * Iterator.remove() method of this instance is not implemented and always throws
	 * a NoSuchMethodError error. null if <b>desiredNetworkSize</b> is zero.
	 * @throws InitializationException if any error has ocurred during the initialization.
	 */
	public java.util.Iterator buildDistributedIds(int desiredNetworkSize) throws InitializationException
	{
	    if (desiredNetworkSize==0) return null;
	    
	    Id chunkValue = null;
	    Id initialValue = null;
	    // generate the chunkValue
		try {
		    Object params[]  = {new Integer(desiredNetworkSize)};
			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);
		}
		//generate the actualValue (-chunkValue)
		try{
			initialValue = (Id) idClass.newInstance();
			initialValue = initialValue.subtract(chunkValue);
		} catch (Exception e) {
			throw new InitializationException("The initial value for the first Id cannot be initialized.",e);
		}
		//generate the Iterator
	    return new DistributedIdIterator(desiredNetworkSize, initialValue,chunkValue);
	}

	/**
	 * Is an Id iterator, for building up to <b>networkSize</b> Id. All Id are
	 * equidistant between two consecutive Id.
	 * @author <a href="mailto: jordi.pujol@estudiants.urv.es">Jordi Pujol</a>
	 * 28/02/2005
	 */
	public class DistributedIdIterator implements java.util.Iterator, java.io.Serializable
	{
	    /** Number of Id to generate. */
	    private int networkSize;
	    /** Number of built Ids. */
	    private int actualBuiltIds;
	    /** Increment value between two consecutive Ids. */
	    private Id chunkValue;
	    /** Actual value of the Id. */
	    private Id actualValue;

	    /**
	     * Initialize this instance with the specified values. The first returned Id
	     * for the <b>next()</b> method will be:<br><br>
	     * <center><b>initialValue+chunkValue</b></center>
	     * @param networkSize Number of Id to build.
	     * @param initialValue Initial Id value.
	     * @param chunkValue Increment value between two consecutive Ids.
	     */
	    public DistributedIdIterator(int networkSize, Id initialValue, Id chunkValue)
	    {
	        this.networkSize = networkSize;
			this.actualValue = initialValue;
			this.chunkValue = chunkValue;
	    }
	    
        /**
         * Test if all Id have been built.
         * @return true if the number of built Ids is less than the initial <b>networkSize</b>.
         * @see java.util.Iterator#hasNext()
         */
        public boolean hasNext() {
           return actualBuiltIds < networkSize;
        }

        /**
         * The next Id or null if all <b>networkSize</b> Id has been generated.
         * @return The next Id or null if all ones has been generated.
         * @see java.util.Iterator#next()
         */
        public Object next() {
            Id toReturn = actualValue;
            if (actualBuiltIds >= networkSize ) {
				return null;
			}
			toReturn = actualValue.add(chunkValue); //builds a new instance of Id
			actualValue.setValue(toReturn);         //update local instance
			actualBuiltIds++;
			return toReturn;
        }

        /**
         * This method is not implemented and throws a NoSuchMethodError error always. 
         * @see java.util.Iterator#remove()
         * @throws NoSuchMethodError always.
         */
        public void remove() {
            throw new NoSuchMethodError("Method is not implemented");
        }
	    
	}
	
	/* ************************ METHODS FOR SERIALIZATION ****************************/
	/**
	 * Makes nothing special, only invokes to <b>stream.defaultWriteObject()</b>.
	 * @param stream Stream to save the actual instance.
	 * @throws IOException if any error has ocurred.
	 */
	private void writeObject(java.io.ObjectOutputStream stream)
    throws IOException {
		stream.defaultWriteObject();
	}
	
	/**
	 * Makes a first <b>stream.defaultReadObject()</b> invocation to uses
	 * the default read method. And then, builds the constructor for
	 * the loaded Node class.
	 * @param stream Stream to read the actual instance.
	 * @throws IOException if occur any error during 
	 * default read object, or if there are any error during
	 * building of Node constructor.  
	 * @throws ClassNotFoundException
	 */
	private void readObject(java.io.ObjectInputStream stream)
    throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        try {
            Class types[] = {int.class};
            idDivideMethod                 = idClass.getMethod("divide",types);
        } catch (Exception e) {
            throw new IOException("Cannot obtain constructors for '"+ idClass.getName() +"'.");
        }
	}
}

⌨️ 快捷键说明

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