001 package ps6.tigerdb; 002 import java.io.Serializable; 003 import java.util.ArrayList; 004 import java.util.HashMap; 005 import java.util.Iterator; 006 007 /** 008 * Feature.java 009 * 010 * @author Felix S. Klock II 011 */ 012 013 public class Feature implements Serializable { 014 // public static Set fullNames = new TreeSet(); 015 public static final long serialVersionUID = 4534; 016 private final String prefixDir; 017 private final String name; 018 private final String type; 019 private final String suffixDir; 020 021 public Feature(String prefixDir, String name, 022 String type, String suffixDir) { 023 this.prefixDir = prefixDir.trim().intern(); 024 this.name = name.trim().intern(); 025 this.type = type.trim().intern(); 026 this.suffixDir = suffixDir.trim().intern(); 027 028 // fullNames.add( fullName().intern() ); 029 } 030 031 public Feature(String s) { 032 if (s.length() != 38) 033 throw new RuntimeException 034 ("feature must have length of 38, not "+s.length()); 035 prefixDir = s.substring( 0, 2).trim().intern(); 036 name = s.substring( 2, 32).trim().intern(); 037 type = s.substring(32, 36).trim().intern(); 038 suffixDir = s.substring(36, 38).trim().intern(); 039 040 // fullNames.add( fullName().intern() ); 041 } 042 043 public boolean equals(/*@Nullable*/ Object o) { 044 if (! (o instanceof Feature) ) return false; 045 return ((Feature)o).fullName().equals(this.fullName()); 046 } 047 public int hashCode() { 048 return fullName().hashCode(); 049 } 050 public String fullName() { 051 StringBuilder sb = new StringBuilder(); 052 boolean pre = false; 053 if (prefixDir.length() > 0) { 054 sb.append(prefixDir); pre = true; 055 } 056 if (name.length() > 0) { 057 sb.append((pre)?" ":""); 058 sb.append(name); 059 pre = true; 060 } 061 if (type.length() > 0) { 062 sb.append((pre)?" ":""); 063 sb.append(type); 064 pre = true; 065 } 066 if (suffixDir.length() > 0) { 067 sb.append((pre)?" ":""); 068 sb.append(suffixDir); 069 } 070 071 if (sb.length() == 0) { 072 sb.append("(unnamed street)"); 073 } 074 return sb.toString(); 075 } 076 077 private static HashMap<Feature, Feature> internMap = new HashMap<Feature, Feature>(); 078 public Feature intern() { 079 if (internMap.containsKey(this)) { 080 return internMap.get(this); 081 } else { 082 internMap.put(this, this); 083 return this; 084 } 085 } 086 087 /** Returns an Iterator[String] of names for this feature. */ 088 Iterator<String> names() { 089 ArrayList<String> l = new ArrayList<String>(); 090 l.add(fullName().trim()); 091 l.add(name.trim()); 092 l.add( (prefixDir+" "+name).trim() ); 093 l.add( (prefixDir+" "+name+" "+type).trim() ); 094 l.add( (name+" "+type).trim() ); 095 l.add( (name+" "+type+" "+suffixDir).trim() ); 096 return ImmIterator.wrap(l.iterator()); 097 } 098 099 public String toString() { 100 return "Feature:"+fullName(); 101 } 102 103 /** 104 * @return the prefix directory of the feature. 105 */ 106 public String getPrefixDir() { 107 return prefixDir; 108 } 109 110 /** 111 * @return the name of the feature. 112 */ 113 public String getName() { 114 return name; 115 } 116 /** 117 * @return the type of the feature. 118 */ 119 public String getType() { 120 return type; 121 } 122 /** 123 * @return the suffix directory of the feature. 124 */ 125 public String getSuffixDir() { 126 return suffixDir; 127 } 128 129 } // Feature