Lookup.java
001 /*
002  *  Lookup.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Valentin Tablan, 11/07/2000
013  *  borislav popov, 05/02/2002
014  *
015  *  $Id: Lookup.java 12006 2009-12-01 17:24:28Z thomas_heitz $
016  */
017 
018 package gate.creole.gazetteer;
019 
020 import java.util.ArrayList;
021 import java.util.Collections;
022 import java.util.Iterator;
023 import java.util.List;
024 import java.util.Map;
025 
026 /**
027  * Used to describe a type of lookup annotations. A lookup is described by a
028  * major type a minor type and a list of languages. Added members are :
029  * ontologyClass and list.
030  * All these values are strings (the list of languages is a string and it is
031  * intended to represesnt a comma separated list).
032  
033  * An optional features field stores arbitary features as part of the lookup 
034  * annotation. This can be used to set meta-data for a gazetteer entry.
035  */
036 public class Lookup implements java.io.Serializable {
037 
038   /** Debug flag
039    */
040   private static final boolean DEBUG = false;
041   
042   /** a map of arbitary features */
043   public Map features = null;
044   
045   /**
046    * Creates a new Lookup value with the given major and minor types and
047    * languages.
048    *
049    @param major major type
050    @param minor minor type
051    @param theLanguages the languages
052    */
053   public Lookup(String theList, String major, String minor, String theLanguages){
054     majorType = major;
055     minorType = minor;
056     languages = theLanguages;
057     list = theList;
058   }
059 
060   /** Tha major type for this lookup, e.g. "Organisation" */
061   public String majorType;
062 
063   /** The minor type for this lookup, e.g. "Company"  */
064   public String minorType;
065 
066   /** The languages for this lookup, e.g. "English, French" */
067   public String languages;
068 
069   /** the ontology class of this lookup according to the mapping between
070    *  list and ontology */
071   public String oClass;
072 
073   /**  the ontology ID */
074   public String ontology;
075 
076   /** the list represented by this lookup*/
077   public String list;
078 
079   /**Returns a string representation of this lookup in the format
080    * This method is used in equals()
081    * that caused this method to implement dualistic behaviour :
082    * i.e. whenever class and ontology are filled then use the
083    * long version,incl. list, ontology and class;
084    * else return just majorType.minorType */
085   public String toString(){
086     StringBuffer b = new StringBuffer();
087     boolean longVersion = false;
088     boolean hasArbitaryFeatures = false;
089     if (null!=ontology && null!=oClass){
090       longVersion = true;
091     }
092     
093     if(null != features) {
094       hasArbitaryFeatures = true;
095     }
096 
097     if longVersion ) {
098       b.append(list);
099       b.append(".");
100     }
101     b.append(majorType);
102     b.append(".");
103     if (null != minorType) {
104       b.append(minorType);
105       if (null!= languages) {
106         b.append(".");
107         b.append(languages);
108       }//if
109     }//if
110     if (longVersion) {
111       b.append("|");
112       b.append(ontology);
113       b.append(":");
114       b.append(oClass);
115     }
116     
117     if(hasArbitaryFeatures) {
118       // as the ordering of the featureMap is undefined, create a new list of 
119       // keys and sort it to ensure the string returned is always the same
120       List sortedKeys = new ArrayList(features.keySet())
121       Collections.sort(sortedKeys);
122       
123       for(Iterator it = sortedKeys.iterator(); it.hasNext()) {
124         String key = (String)it.next();
125         b.append("|");
126         b.append(key);
127         b.append(":");
128         b.append(features.get(key).toString());
129         
130       }
131     }
132     return b.toString();
133   }
134 
135   /**
136    *   Two lookups are equal if they have the same string representation
137    *  (major type and minor type).
138    @param obj
139    */
140   public boolean equals(Object obj){
141     if(obj instanceof Lookupreturn obj.toString().equals(toString());
142     else return false;
143   // equals
144 
145   /**    *
146    */
147   public int hashCode(){ return toString().hashCode();}
148 
149 // Lookup