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

📄 addressbook.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:

/* 
 * @<#>AddressBook.java<#> version 0.0.1, 1/1/2001
 *
 * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR
 * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE 
 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.
 *
 * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
 * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE 
 * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS.
 *
 * Copyright (c) 2000 Wayne State University. All Rights Reserved.
 */
package naplet.message;

import java.util.*;
import naplet.*;

/**
 * The <code>AddressBook</code> of a naplet contains all the NapletID of 
 * its sibling naplets due to cloning, as well as the address information
 * inherited from its parent (creator).  
 * All the naplets in the AddressBook forms a communication network.
 */
public class AddressBook implements java.io.Serializable, Cloneable {
	/**
	 * AddressBook is created to associate with each original naplet. 
	 * Naplet clone() inherits AddressBook.
	 */ 
	private NapletID creator;

	/**
	 * Owner of the address book can change with time.
	 * On spawning a naplet, the child naplet becomes a owner of the book while
	 * creator remains the same. 
	 */
	private NapletID owner;			
	private int generation; 	// Descendent of the creator.

	/**
	 * A group of reachable naplets, including the creator itself. 
	 */
	private ArrayList aBook;
	
	/**
	 * Constructor
	 * @param nid Creator of the AddressBook
	 */
	public AddressBook( NapletID nid ) {
		creator = nid;	
		owner = nid;
		aBook = new ArrayList();  // Create an empty address book
		aBook.add( new AddressEntry(nid) ); 
	}

	/**
	 * Clone, deep or shallow??
	 */
	public synchronized Object clone() {
		try {
			AddressBook newBook = (AddressBook)super.clone();

			newBook.creator = creator; 
			newBook.owner = owner;
			newBook.generation++; 	// New generation relative to creator
			
			if (aBook!=null) 
				newBook.aBook = (ArrayList) aBook.clone();
			else
				throw new NapletInternalError("Clone an Empty Address Book");	

			return newBook;
		} catch (CloneNotSupportedException e) {
			throw new NapletInternalError("Clone error for AddressBook");
		}
	}

	public final NapletID creator() { return creator; }
	public final int generation() { return generation; }
	public int size() { return aBook.size(); }

	public final NapletID getOwner() { return owner; }
	public final void setOwner( NapletID nid ) throws InvalidNapletException { 
		if (nid==null)
			throw new InvalidNapletException("Empty AddressBook Owner"); 
		owner = nid; 
	}

	public void add( NapletID nid, URN server ) {
		aBook.add( new AddressEntry(nid, server) );
	}

	public void add( NapletID nid ) {
		add(nid, null);
	}

	public void addAll( NapletID[] nids, URN[] servers ) {
		for (int i=0; i<nids.length; i++) 
			add( nids[i], servers[i] );
	}
	public void addAll( NapletID[] nids ) {
		for (int i=0; i<nids.length; i++) 
			add( nids[i] );
	}

	public URN getAddress( NapletID nid ) throws NoSuchElementException {
		boolean going = true;
		URN server = null;

		if (nid==null)
			throw new NoSuchElementException();
		
		for (int i=0; going && i<aBook.size(); i++) {
			AddressEntry entry = (AddressEntry) aBook.get(i);
			if (nid.equals(entry.getNapletID())) {
				server = entry.getServerURN();
				going = false;
			}
		}

		return server; 
	}

	public void updateAddress( NapletID nid, URN server ) 
					throws NoSuchElementException {

		boolean going = true;
		if (nid==null)
			throw new NoSuchElementException();
		
		for (int i=0; going && i<aBook.size(); i++) {
			AddressEntry entry = (AddressEntry) aBook.get(i);
			if (nid.equals(entry.getNapletID())) {
				entry.setServerURN( server ) ;
				going = false;
			}
		}

	}

	public ListIterator iterator() {
		return aBook.listIterator();
	}

	public String toString() {
		StringBuffer sb = new StringBuffer(creator.toString()+"("); 
		for (int i=1; i<aBook.size(); i++)
			sb.append( aBook.toString() );
		sb.append(")");
		return sb.toString();
	}

}

⌨️ 快捷键说明

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