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