001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.ontology; 022 023import org.biojava.nbio.ontology.utils.Annotation; 024import org.biojava.nbio.ontology.utils.WeakValueHashMap; 025 026import java.util.*; 027 028 029 030/** 031 * 032 * 033 * @author Matthew Pocock 034 */ 035public class IntegerOntology 036 037implements Ontology { 038 private final Map termCache; 039 040 IntegerOntology() { 041 termCache = new WeakValueHashMap(); 042 } 043 044 @Override 045 public String getName() { 046 return "core.integer"; 047 } 048 049 @Override 050 public String getDescription() { 051 return "Ontology containing all integers"; 052 } 053 054 @Override 055 public void setDescription(String description){ 056 } 057 058 @Override 059 public Set getTerms() { 060 return new AbstractSet() { 061 @Override 062 public boolean contains(Object o) { 063 return o instanceof IntTerm; 064 } 065 066 @Override 067 public int size() { 068 return Integer.MAX_VALUE; 069 } 070 071 @Override 072 public Iterator iterator() { 073 return new Iterator() { 074 int i = 0; 075 076 @Override 077 public boolean hasNext() { 078 return i > 0; 079 } 080 081 @Override 082 public Object next() { 083 if(!hasNext()){ 084 throw new NoSuchElementException(); 085 } 086 return resolveInt(i++); 087 } 088 089 @Override 090 public void remove() { 091 throw new UnsupportedOperationException(); 092 } 093 }; 094 } 095 }; 096 } 097 098 @Override 099 public Term getTerm(String s) throws NoSuchElementException { 100 int val = Integer.parseInt(s); 101 return resolveInt(val); 102 } 103 104 @Override 105 public Set getTriples(Term subject, Term object, Term predicate) { 106 return Collections.EMPTY_SET; 107 } 108 109 @Override 110 public OntologyOps getOps() { 111 return new DefaultOps() { 112 @Override 113 public Set getRemoteTerms() { 114 return Collections.EMPTY_SET; 115 } 116 }; 117 } 118 119 @Override 120 public Term createTerm(String name) throws AlreadyExistsException, IllegalArgumentException { 121 throw new IllegalArgumentException(getName() + " is immutable"); 122 } 123 124 @Override 125 public Term createTerm(String name, String description) 126 throws 127 AlreadyExistsException, 128 129 IllegalArgumentException 130 { 131 throw new IllegalArgumentException(getName() + " is immutable"); 132 } 133 134 @Override 135 public Term createTerm(String name, String description, Object[] synonyms) 136 throws 137 AlreadyExistsException, 138 139 IllegalArgumentException 140 { 141 throw new IllegalArgumentException(getName() + " is immutable"); 142 } 143 144 @Override 145 public Variable createVariable(String name, String description) 146 throws 147 AlreadyExistsException, 148 149 IllegalArgumentException 150 { 151 throw new IllegalArgumentException(getName() + " is immutable"); 152 } 153 154 @Override 155 public Term importTerm(Term t, String name) 156 157 158 { 159 throw new IllegalArgumentException(getName() + " is immutable"); 160 } 161 162 @Override 163 public Triple createTriple(Term subject, Term object, Term predicate, String name, String description) 164 throws 165 AlreadyExistsException 166 { 167 throw new IllegalArgumentException(getName() + " is immutable"); 168 } 169 170 @Override 171 public boolean containsTriple(Term subject, Term object, Term predicate) { 172 return false; 173 } 174 175 @Override 176 public void deleteTerm(Term t) { 177 throw new RuntimeException(getName() + " is immutable"); 178 } 179 180 @Override 181 public boolean containsTerm(String name) { 182 // uglee hack - perhaps we should use a regex? 183 try { 184 Integer.parseInt(name); 185 } catch (NumberFormatException e) { 186 return false; 187 } 188 189 return true; 190 } 191 192 public IntTerm resolveInt(int val) { 193 Integer i = new Integer(val); 194 IntTerm term = (IntTerm) termCache.get(i); 195 196 if(term == null) { 197 term = new IntTerm(val); 198 termCache.put(i, term); 199 } 200 201 return term; 202 } 203 204 public final class IntTerm 205 206 implements Term { 207 private final int val; 208 private Set synonyms; 209 210 public IntTerm(int val) { 211 this(val, null); 212 } 213 214 public IntTerm(int val, Object[] synonyms) { 215 this.val = val; 216 217 this.synonyms = new TreeSet(); 218 if (synonyms!=null) this.synonyms.addAll(Arrays.asList(synonyms)); 219 } 220 221 @Override 222 public void addSynonym(Object synonym) { 223 this.synonyms.add(synonym); 224 } 225 226 @Override 227 public void removeSynonym(Object synonym) { 228 this.synonyms.remove(synonym); 229 } 230 231 @Override 232 public Object[] getSynonyms() { 233 return this.synonyms.toArray(); 234 } 235 236 public int intValue() { 237 return val; 238 } 239 240 @Override 241 public String getName() { 242 return String.valueOf(val); 243 } 244 245 @Override 246 public String getDescription() { 247 return "The integer " + getName(); 248 } 249 250 @Override 251 public void setDescription(String description){ 252 253 } 254 255 @Override 256 public Ontology getOntology() { 257 return IntegerOntology.this; 258 } 259 260 @Override 261 public Annotation getAnnotation() { 262 return Annotation.EMPTY_ANNOTATION; 263 } 264 } 265 266 @Override 267 public void setName(String name) { 268 //ignore 269 270 } 271 272 273}