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

📄 piynamer.java

📁 PIY(Program It Yourself)是一个基于Java的应用程序开发环境
💻 JAVA
字号:
package piy;

import java.util.*;

/**
* Deals with the managing the names of the values that make up a project (ie. component
* names, user window names and action list names). Actions should <b>never</b> reference 
* a PIYNamer object to retrieve the names of components/user windows/action lists - these 
* should be stored by reference, not by name.
* No UserComponent, ActionList or UserWindow should know its own name - the name
* is used solely in the PIY development environment.
* @author David Vivash
* @version 1.1, 22/11/00
*/
public final class PIYNamer implements java.io.Serializable
{
	private Mapper names;

	/**
	* Constructs a PIYNamer object with the specified initial capacity for name->value
	* mappings. If size<=0, size defaults to 8.
	* @param size the initial capacity allocated for storing names
	*/
	protected PIYNamer(int size) {
		if (size < 1) size = 8;
		names = new Mapper(size);
	}

	/**
	* Gets the names of all of the values being used in the PIY project where the values mapped
	* to are castable to the specified class.
	* @param castabeTo the base class which the value of all returned names can be cast to
	* @return a list of value names whose values can be cast to "castableTo"
	*/
	public List getNamesList(Class castableTo) {
		List values = names.values();
		Object o = null; Class c = null;
		List toReturn = new ArrayList(values.size());
		
		for (int i=0; i<values.size(); i++) {
			c = (o = values.get(i)).getClass(); 
			if (castableTo.isAssignableFrom(c)) toReturn.add(names.getKey(o));
		}

		return toReturn;
	}

	/**
	* Gets the names of all of the values being used in the PIY project as an array of Strings.
	* @return an array of value names
	*/
	public String[] getNames() {
		List keys = names.keys();		
		return getNames(keys);
	}

	/**
	* Utility method for converting a list of values to an array of strings, presupposing the values
	* represent valid String objects.
	* @param values the list of values to convert
	* @return the array of strings representing the values in the list
	*/
	protected String[] getNames(List strings)	{
		String[] toReturn = new String[strings.size()];
		System.arraycopy(strings.toArray(), 0, toReturn, 0, strings.size());

		return toReturn;
	}

	/**
	* Returns the name of the value, or null if the name is not known.
	* @param value the value for which to find the name
	* @return the name of the specified value
	*/
	public String getName(Object value) {
		return (String)names.getKey(value);
	}

	/**
	* Returns a value name which has not yet been used.
	* @return an unused value name
	*/
	protected String getUniqueName(String stub) {
		if ((stub == null) || (stub.equals(""))) stub = "Name";
		int index = 0;

		do {
			index++;
		} while(!nameIsUnique(stub+index));

		return (stub + index);
	}

	/**
	* Returns true if the specified name has not yet been used.
	* @param name the name to look for in the currently used value names
	* @return true if the specified name has not yet been used, false if it has
	*/
	protected boolean nameIsUnique(String name) {
		return ((name == null) || (name.equals(""))) ? false : (names.getValue(name) == null);
	}

	/**
	* Renames a value. The new name must not be used by a different value,
	* or the name changer will fail and this method will return false.
	* @param oldName the name of the value to be renamed
	* @param newName the name to change to
	* @return true if the name change is successful, false otherwise
	*/
	protected boolean renameValue(String oldName, String newName) {
		if ((oldName == null) || (newName == null)) return false;

		if (oldName.equals(newName)) return true;

		if (nameIsUnique(newName) && !nameIsUnique(oldName)) {
			Object value = removeValue(oldName); //remove old value mapping
			names.put(newName, value); //add new mapping
			return true;
		} else return false;
	}

	/**
	* Retrieves a value given its unique name.
	* @param name the name of the value to retrieve
	* @return the relevant value object
	*/
	public Object getValue(String name) {
		return names.getValue(name);
	}

	/**
	* Retrieves a value given its unique name.
	* @param name the name of the value to retrieve
	* @return the relevant value object
	*/
	public List getValues() {
		return names.values();
	}


	/**
	* Adds a value object to the collection.  The user window is assigned a unique
	* name, which can be changed at a later date.
	* @param value the value object to add
	* @param stub the starting string of what the value object should be initially named
	* @return a unique name that has been assigned to the value
	*/
	protected String add(Object value, String stub) {
		String name = getUniqueName(stub);
		names.put(name, value);

		return name;
	}

	/**
	* Removes a mapping.
	* @param name the name of the mapping to remove
	* @return the object that was mapped to from the name, 
	* or null if the name wasn't found
	*/
	protected Object removeValue(String name) {
		if (name == null) return null;
		return nameIsUnique(name) ? null : names.removeKey(name);
	}

	/**
	* Removes all naming mappings from the piy namer.
	*/
	protected void clear() {
		names.clear();
	}

	/**
	* The number of items in the namer.
	* @return the number of items in the namer.
	*/
	protected int size() {
		return names.size();
	}
}


⌨️ 快捷键说明

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