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

📄 namedobject.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
字号:
package toocom.ocgl;

import java.util.*;

/**
 * This class represents a multilingual named object. A NameObject include an id which allows to 
 * identify the object in the system. The id is automatically allocated when the object is created, 
 * freed when the object is discarded. The constructors of all subclasses of NamedObject have to call
 * the constructor of NamedObject by using the super(Terms) method. 
 *
 * @author Fr閐閞ic F黵st
 */

public abstract class NamedObject{

	private static LinkedList freeId = new LinkedList();
	private static int maxUsedId = -1;
	private int id;
	private Terms terms;

	/** Reserve a free id for a named object and returns it. */
	private static int useId(){
		if(freeId.size() == 0){
			maxUsedId ++;
			return maxUsedId;
		}
		else{
			return ((Integer) freeId.removeFirst()).intValue();
		}
	}
	
	/** Free the given id. */
	private static void freeId(int id){
		if(id < maxUsedId){
			freeId.add(new Integer(id));
		}
		else{
			if(id == maxUsedId) maxUsedId --;
		}
	}
	
	public NamedObject(Terms terms){
		this.terms = terms;
		this.id = NamedObject.useId();
	}
	
	public NamedObject(){
		this.id = NamedObject.useId();
	}
	
	protected void finalize(){
		NamedObject.freeId(this.getId());
	}
	
	public int getId(){
		return id;
	}
	
	public final boolean equals(NamedObject object){
		if(object == null) return false;
		else return (object.getId() == this.id);
	}
	
	/** Add a term of the given language or modify the term if it already exists. */
	public void setTerm(String term, Language l){
		terms.setTerm(term,l);
	}
	
	public void setTerms(Terms terms){
		this.terms = terms;
	}
	
	/** Returns the term of the given language, null if any term exists for this type. */
	public String getTerm(Language l){
		return terms.getTerm(l);	
	}
	
	public Terms getTerms(){
		return terms;
	}
	
	/** Returns a string with all the terms of the named object. */
	public String getAllTerms(){
		return terms.toString();
	}
	
	/** Returns true if the named object and object have the same term in each language. */
	public boolean isHomonymousWith(NamedObject object){
		if(this.terms == null){
			if(object.getTerms() == null) return true;
			else return false;
		}
		else{
			if(object.getTerms() != null) return this.terms.equals(object.getTerms());
			else return false;
		}
	}
	
	/** Returns true if the named object and object have the same term in the given language. */
	public boolean isHomonymousWithin(NamedObject object, Language l){
		if(this.terms == null){
			if(object.getTerms() == null) return true;
			else return false;
		}
		else{
			if(object.getTerms() != null) return this.terms.equals(object.getTerms(),l);
			else return false;
		}
	}
	
	/** Returns the string representation of the object for the given language. */
	public abstract String toString(Language l);
	
	/** Returns a list of the elements of the given one sorted by name according to the given language. */
	public static LinkedList sortListByName(Collection list, Language l){
		Object[] tab = list.toArray();
		for(int cpt = 0;cpt < (tab.length - 1);cpt ++){
			NamedObject temp = (NamedObject) tab[0];
			int index = 0;
			int i;
			for(i = 1;i < (tab.length - cpt);i ++){
				NamedObject tempBis = (NamedObject) tab[i];
				if(tempBis.getTerm(l).compareToIgnoreCase(temp.getTerm(l)) > 0){
					index = i;
					temp = tempBis;
				}
			}
			temp = (NamedObject) tab[index];
			tab[index] = tab[tab.length - cpt - 1];
			tab[tab.length - cpt - 1] = temp;
		}
		LinkedList result = new LinkedList();
		for(int i = 0;i < tab.length;i ++){
			result.add(tab[i]);
		}
		return result;
	}
	
}

⌨️ 快捷键说明

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