001package org.biojava.ontology;
002
003import java.util.AbstractSet;
004import java.util.Arrays;
005import java.util.Collections;
006import java.util.Iterator;
007import java.util.Map;
008import java.util.NoSuchElementException;
009import java.util.Set;
010import java.util.TreeSet;
011
012import org.biojava.bio.Annotation;
013import org.biojava.utils.ChangeVetoException;
014import org.biojava.utils.Unchangeable;
015import org.biojava.utils.cache.WeakValueHashMap;
016
017/**
018 *
019 *
020 * @author Matthew Pocock
021 */
022public class IntegerOntology
023extends Unchangeable
024implements Ontology {
025        private final Map termCache;
026
027        IntegerOntology() {
028                termCache = new WeakValueHashMap();
029        }
030
031        public String getName() {
032                return "core.integer";
033        }
034
035        public String getDescription() {
036                return "Ontology containing all integers";
037        }
038        
039        public void setDescription(String description){         
040        }
041
042        public Set getTerms() {
043                return new AbstractSet() {
044                        public boolean contains(Object o) {
045                                return o instanceof IntTerm;
046                        }
047
048                        public int size() {
049                                return Integer.MAX_VALUE;
050                        }
051
052                        public Iterator iterator() {
053                                return new Iterator() {
054                                        int i = 0;
055
056                                        public boolean hasNext() {
057                                                return i > 0;
058                                        }
059
060                                        public Object next() {
061                                                return resolveInt(i++);
062                                        }
063
064                                        public void remove() {
065                                                throw new UnsupportedOperationException();
066                                        }
067                                };
068                        }
069                };
070        }
071
072        public Term getTerm(String s) throws NoSuchElementException {
073                int val = Integer.parseInt(s);
074                return resolveInt(val);
075        }
076
077        public Set getTriples(Term subject, Term object, Term predicate) {
078                return Collections.EMPTY_SET;
079        }
080
081        public OntologyOps getOps() {
082                return new DefaultOps() {
083                        public Set getRemoteTerms() {
084                                return Collections.EMPTY_SET;
085                        }
086                };
087        }
088
089        public Term createTerm(String name) throws AlreadyExistsException, ChangeVetoException, IllegalArgumentException {
090                throw new ChangeVetoException(getName() + " is immutable");
091        }
092
093        public Term createTerm(String name, String description)
094        throws
095        AlreadyExistsException,
096        ChangeVetoException,
097        IllegalArgumentException
098        {
099                throw new ChangeVetoException(getName() + " is immutable");
100        }
101
102        public Term createTerm(String name, String description, Object[] synonyms)
103        throws
104        AlreadyExistsException,
105        ChangeVetoException,
106        IllegalArgumentException
107        {
108                throw new ChangeVetoException(getName() + " is immutable");
109        }
110
111        public Variable createVariable(String name, String description)
112        throws
113        AlreadyExistsException,
114        ChangeVetoException,
115        IllegalArgumentException
116        {
117                throw new ChangeVetoException(getName() + " is immutable");
118        }
119
120        public Term importTerm(Term t, String name)
121        throws
122        ChangeVetoException
123        {
124                throw new ChangeVetoException(getName() + " is immutable");
125        }
126
127        public Triple createTriple(Term subject, Term object, Term predicate, String name, String description)
128        throws
129        AlreadyExistsException,
130        ChangeVetoException {
131                throw new ChangeVetoException(getName() + " is immutable");
132        }
133
134        public boolean containsTriple(Term subject, Term object, Term predicate) {
135                return false;
136        }
137
138        public void deleteTerm(Term t) throws ChangeVetoException {
139                throw new ChangeVetoException(getName() + " is immutable");
140        }
141
142        public boolean containsTerm(String name) {
143                // uglee hack - perhaps we should use a regex?
144                try {
145                        Integer.parseInt(name);
146                } catch (NumberFormatException e) {
147                        return false;
148                }
149
150                return true;
151        }
152
153        public IntTerm resolveInt(int val) {
154                Integer i = new Integer(val);
155                IntTerm term = (IntTerm) termCache.get(i);
156
157                if(term == null) {
158                        term = new IntTerm(val);
159                        termCache.put(i, term);
160                }
161
162                return term;
163        }
164
165        public final class IntTerm
166        extends Unchangeable
167        implements Term {
168                private final int val;
169                private Set synonyms;
170
171                public IntTerm(int val) {
172                        this(val, null);
173                }
174
175                public IntTerm(int val, Object[] synonyms) {
176                        this.val = val;
177
178                        this.synonyms = new TreeSet();
179                        if (synonyms!=null) this.synonyms.addAll(Arrays.asList(synonyms));
180                }
181
182                public void addSynonym(Object synonym) {
183                        this.synonyms.add(synonym);
184                }
185
186                public void removeSynonym(Object synonym) {
187                        this.synonyms.remove(synonym);
188                }
189
190                public Object[] getSynonyms() {
191                        return this.synonyms.toArray();
192                }
193
194                public int intValue() {
195                        return val;
196                }
197
198                public String getName() {
199                        return String.valueOf(val);
200                }
201
202                public String getDescription() {
203                        return "The integer " + getName();
204                }
205
206                public void setDescription(String description){
207
208                }
209
210                public Ontology getOntology() {
211                        return IntegerOntology.this;
212                }
213
214                public Annotation getAnnotation() {
215                        return Annotation.EMPTY_ANNOTATION;
216                }
217        }
218
219        public void setName(String name) {
220                //ignore
221                
222        }
223
224
225}