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                                                return resolveInt(i++);
084                                        }
085
086                                        @Override
087                                        public void remove() {
088                                                throw new UnsupportedOperationException();
089                                        }
090                                };
091                        }
092                };
093        }
094
095        @Override
096        public Term getTerm(String s) throws NoSuchElementException {
097                int val = Integer.parseInt(s);
098                return resolveInt(val);
099        }
100
101        @Override
102        public Set getTriples(Term subject, Term object, Term predicate) {
103                return Collections.EMPTY_SET;
104        }
105
106        @Override
107        public OntologyOps getOps() {
108                return new DefaultOps() {
109                        @Override
110                        public Set getRemoteTerms() {
111                                return Collections.EMPTY_SET;
112                        }
113                };
114        }
115
116        @Override
117        public Term createTerm(String name) throws AlreadyExistsException,  IllegalArgumentException {
118                throw new IllegalArgumentException(getName() + " is immutable");
119        }
120
121        @Override
122        public Term createTerm(String name, String description)
123                        throws
124                        AlreadyExistsException,
125
126                        IllegalArgumentException
127                        {
128                throw new IllegalArgumentException(getName() + " is immutable");
129                        }
130
131        @Override
132        public Term createTerm(String name, String description, Object[] synonyms)
133                        throws
134                        AlreadyExistsException,
135
136                        IllegalArgumentException
137                        {
138                throw new IllegalArgumentException(getName() + " is immutable");
139                        }
140
141        @Override
142        public Variable createVariable(String name, String description)
143                        throws
144                        AlreadyExistsException,
145
146                        IllegalArgumentException
147                        {
148                throw new IllegalArgumentException(getName() + " is immutable");
149                        }
150
151        @Override
152        public Term importTerm(Term t, String name)
153
154
155        {
156                throw new IllegalArgumentException(getName() + " is immutable");
157        }
158
159        @Override
160        public Triple createTriple(Term subject, Term object, Term predicate, String name, String description)
161                        throws
162                        AlreadyExistsException
163                        {
164                throw new IllegalArgumentException(getName() + " is immutable");
165                        }
166
167        @Override
168        public boolean containsTriple(Term subject, Term object, Term predicate) {
169                return false;
170        }
171
172        @Override
173        public void deleteTerm(Term t)  {
174                throw new RuntimeException(getName() + " is immutable");
175        }
176
177        @Override
178        public boolean containsTerm(String name) {
179                // uglee hack - perhaps we should use a regex?
180                try {
181                        Integer.parseInt(name);
182                } catch (NumberFormatException e) {
183                        return false;
184                }
185
186                return true;
187        }
188
189        public IntTerm resolveInt(int val) {
190                Integer i = new Integer(val);
191                IntTerm term = (IntTerm) termCache.get(i);
192
193                if(term == null) {
194                        term = new IntTerm(val);
195                        termCache.put(i, term);
196                }
197
198                return term;
199        }
200
201        public final class IntTerm
202
203        implements Term {
204                private final int val;
205                private Set synonyms;
206
207                public IntTerm(int val) {
208                        this(val, null);
209                }
210
211                public IntTerm(int val, Object[] synonyms) {
212                        this.val = val;
213
214                        this.synonyms = new TreeSet();
215                        if (synonyms!=null) this.synonyms.addAll(Arrays.asList(synonyms));
216                }
217
218                @Override
219                public void addSynonym(Object synonym) {
220                        this.synonyms.add(synonym);
221                }
222
223                @Override
224                public void removeSynonym(Object synonym) {
225                        this.synonyms.remove(synonym);
226                }
227
228                @Override
229                public Object[] getSynonyms() {
230                        return this.synonyms.toArray();
231                }
232
233                public int intValue() {
234                        return val;
235                }
236
237                @Override
238                public String getName() {
239                        return String.valueOf(val);
240                }
241
242                @Override
243                public String getDescription() {
244                        return "The integer " + getName();
245                }
246
247                @Override
248                public void setDescription(String description){
249
250                }
251
252                @Override
253                public Ontology getOntology() {
254                        return IntegerOntology.this;
255                }
256
257                @Override
258                public Annotation getAnnotation() {
259                        return Annotation.EMPTY_ANNOTATION;
260                }
261        }
262
263        @Override
264        public void setName(String name) {
265                //ignore
266
267        }
268
269
270}