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 */
021
022package org.biojava.nbio.ontology;
023
024import org.biojava.nbio.ontology.utils.Annotatable;
025import org.biojava.nbio.ontology.utils.Annotation;
026import org.biojava.nbio.ontology.utils.SmallAnnotation;
027
028import java.util.Arrays;
029import java.util.Set;
030import java.util.TreeSet;
031
032
033
034/**
035 * A term in an ontology.  This has an {@link Annotation Annotation}
036 * which can be used for storing additional human-displayable information.  It
037 * is strongly recommended that the Annotation is not used for any machine-readable
038 * data -- this should be represented by relations in the ontology instead.
039 *
040 * <p>
041 * Terms are things that represent things. They are the same sort of thing as a
042 * Java object or a prolog atom. A sub-set of terms are themselves relations.
043 * This means that they are used to describe associations between pairs of terms.
044 * Since all terms can be described, it is possible (and indeed encouraged) to
045 * describe relations. As a minimum, you should consider saying if they are
046 * identity or partial order relations, or if they are transitive, reflexive,
047 * symmetrical, anti-symmetrical or anything else you know about them. This gives
048 * the inference engine some chance of working out what is going on.
049 * </p>
050 *
051 * @author Thomas Down
052 * @author Matthew Pocock
053 * @since 1.4
054 */
055public interface Term extends Annotatable {
056        /**
057         * ChangeType which indicates that this term's ontology has been
058         * altered
059         */
060
061
062        /**
063         * Return the name of this term.
064         * @return the name of the term
065         */
066
067        public String getName();
068
069        /**
070         * Return a human-readable description of this term, or the empty string if
071         * none is available.
072         * @return the description of the term
073         */
074
075        public String getDescription();
076
077        /** set the description of the term;
078         *
079         * @param description
080         *
081         */
082        public void setDescription(String description);
083
084        /**
085         * Return the ontology in which this term exists.
086         * @return the ontology
087         */
088
089        public Ontology getOntology();
090
091        /**
092         * Return the synonyms for this term.
093         * @return the synonyms
094         */
095
096        public Object[] getSynonyms();
097
098        /**
099         * Add a synonym for this term.
100         * @param synonym the synonym
101         */
102
103        public void addSynonym(Object synonym);
104
105        /**
106         * Remove a synonym for this term.
107         * @param synonym
108         */
109
110        public void removeSynonym(Object synonym);
111
112        /**
113         * Simple in-memory implementation of an ontology term.
114         * This can be used to implement Ontology.createTerm
115         */
116
117        public static class Impl
118        extends AbstractTerm
119        implements Term, java.io.Serializable {
120                /**
121                 *
122                 */
123                private static final long serialVersionUID = 6561668917514377417L;
124
125                private final String name;
126
127                private final Ontology ontology;
128                private Annotation annotation;
129                private Set<Object> synonyms;
130
131                public Impl(Ontology ontology, String name) {
132                        this(ontology,name,null,null);
133                }
134
135                public Impl(Ontology ontology, String name, String description) {
136                        this(ontology,name,description,null);
137                }
138
139                public Impl(Ontology ontology, String name, String description, Object[] synonyms) {
140                        if (name == null) {
141                                throw new NullPointerException("Name must not be null");
142                        }
143                        // by AP - description can change from now on...
144                        //if (description == null) {
145                        //    throw new NullPointerException("Description must not be null");
146                        //}
147                        if (ontology == null) {
148                                throw new NullPointerException("Ontology must not be null");
149                        }
150
151                        this.name = name;
152                        this.description = description;
153                        this.ontology = ontology;
154
155                        this.synonyms = new TreeSet<>();
156                        if (synonyms!=null) this.synonyms.addAll(Arrays.asList(synonyms));
157                }
158
159                @Override
160                public void addSynonym(Object synonym) {
161                        this.synonyms.add(synonym);
162                }
163
164                @Override
165                public void removeSynonym(Object synonym) {
166                        this.synonyms.remove(synonym);
167                }
168
169                @Override
170                public Object[] getSynonyms() {
171                        return this.synonyms.toArray();
172                }
173
174                @Override
175                public String getName() {
176                        return name;
177                }
178
179                public void setAnnotation(Annotation annotation) {
180                        this.annotation = annotation;
181                }
182
183                public void setSynonyms(Set<Object> synonyms) {
184                        this.synonyms = synonyms;
185                }
186
187                @Override
188                public String getDescription() {
189                        return description;
190                }
191
192                @Override
193                public Ontology getOntology() {
194                        return ontology;
195                }
196
197                @Override
198                public String toString() {
199                        return name;
200                }
201
202                @Override
203                public Annotation getAnnotation() {
204                        if (annotation == null) {
205                                annotation = new SmallAnnotation();
206                        }
207                        return annotation;
208                }
209
210          @Override
211        public int hashCode() {
212                int value = 17;
213                if(getName() != null)
214                  value *= 31 * getName().hashCode();
215                return 17 * value;
216          }
217
218          @Override
219        public boolean equals(Object obj)
220          {
221                if(obj == this) return true;
222                if(!(obj instanceof Term)) return false;
223
224                Term that = (Term) obj;
225
226                return this.getOntology() == that.getOntology() &&
227                                this.getName() == that.getName();
228          }
229        }
230}