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.ontology;
023
024import java.util.Arrays;
025import java.util.Set;
026import java.util.TreeSet;
027
028import org.biojava.bio.Annotation;
029
030/**
031 * A term in another ontology.
032 *
033 * <p>
034 * This is how you allow one ontology to refer to terms in another one. Since
035 * these ontologies are designed to be modular and self-contained, it is
036 * expected that you would not copy terms from one ontology into another. The
037 * best-practice way to represent terms from another ontology in your one is to
038 * use RemoteTerm instances. Ontology has a method importTerm that does this
039 * for you. By default, imported terms will have names composed from the source
040 * ontology and the imported term name. However, this should be over-rideable.
041 * </p>
042 *
043 * <p>
044 * The imported term will have the same name as the original term. They are
045 * implicitly identical to each other. The most common use of imports will be
046 * to slurp in the "core" ontology so that operations such as <code>is-a</code>
047 * and <code>has-a</code> are available.
048 * </p>
049 *
050 * @author Thomas Down
051 * @author Matthew Pocock
052 * @since 1.4
053 */
054
055public interface RemoteTerm extends Term {
056    /**
057     * Return the imported term
058     * @return the term
059     */
060
061    public Term getRemoteTerm();
062
063    /**
064     * Simple in-memory implementation of a remote ontology term.
065     *
066     * This can be used to implement Ontology.importTerm
067     */
068
069    public final static class Impl
070    extends AbstractTerm
071    implements RemoteTerm, java.io.Serializable {
072        /**
073                 * 
074                 */
075                private static final long serialVersionUID = 922700041939183676L;
076                private final Ontology ontology;
077        private final Term remoteTerm;
078        private final String name;
079        private Set synonyms;
080
081        public Impl(Ontology ontology, Term remoteTerm, String name) {
082            this(ontology, remoteTerm, name, null);
083        }
084         
085        public Impl(Ontology ontology, Term remoteTerm, String name, Object[] synonyms) {
086            if (ontology == null) {
087                throw new NullPointerException("Ontology must not be null");
088            }
089            if (remoteTerm == null) {
090                throw new NullPointerException("RemoteTerm must not be null");
091            }
092            if(name == null) {
093              name = remoteTerm.getOntology().getName() + "." + remoteTerm.getName();
094            }
095
096            this.ontology = ontology;
097            this.remoteTerm = remoteTerm;
098            this.name = name;
099            
100            this.synonyms = new TreeSet();
101            if (synonyms!=null) this.synonyms.addAll(Arrays.asList(synonyms));
102        }
103
104        public void addSynonym(Object synonym) {
105            this.synonyms.add(synonym);
106        }
107        
108        public void removeSynonym(Object synonym) {
109            this.synonyms.remove(synonym);
110        }
111        
112        public Object[] getSynonyms() {
113            return this.synonyms.toArray();
114        }
115
116        public String getName() {
117            return getOntology().getName() + ":" + remoteTerm.getName();
118        }
119
120        public String getDescription() {
121            return remoteTerm.getDescription();
122        }
123
124        public Ontology getOntology() {
125            return ontology;
126        }
127
128        public Term getRemoteTerm() {
129            return remoteTerm;
130        }
131
132        public String toString() {
133            return name;
134        }
135
136        public Annotation getAnnotation() {
137            return remoteTerm.getAnnotation();
138        }
139    }
140}