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.biojavax;
023
024import java.util.Set;
025
026import org.biojava.bio.Annotation;
027import org.biojava.utils.Unchangeable;
028
029/**
030 * A basic CrossRef implementation.
031 * @author Richard Holland
032 * @author Mark Schreiber
033 * @author George Waldon (made Unchangeable)
034 * @since 1.5
035 */
036public class SimpleCrossRef extends Unchangeable implements CrossRef {
037    
038    private RichAnnotation notes = new SimpleRichAnnotation();
039    private String accession;
040    private String dbname;
041    private int version;
042    
043    /**
044     * Creates a new instance of SimpleCrossRef with the values to use for
045     * the immutable database name, accession and version.
046     * @param dbname the dbname for this crossref.
047     * @param accession the accession for this crossref.
048     * @param version the version for this crossref.
049     */
050    public SimpleCrossRef(String dbname, String accession, int version) {
051        if (dbname==null) throw new IllegalArgumentException("Database name cannot be null");
052        if (accession==null) throw new IllegalArgumentException("Accession cannot be null");
053        this.accession = accession;
054        this.dbname = dbname;
055        this.version = version;
056    }
057    
058    /**
059     * Creates a new instance of SimpleCrossRef with the values to use for
060     * the immutable database name, accession and version. Identical to other
061     * dbname/accession/version constructor except the version is specified
062     * as an Integer object rather than an int primitive. Will throw an
063     * exception if version is null.
064     * @param dbname the dbname for this crossref.
065     * @param accession the accession for this crossref.
066     * @param version the version for this crossref.
067     */
068    public SimpleCrossRef(String dbname, String accession, Integer version) {
069        this(dbname,accession,version.intValue());
070    }
071    
072    // Hibernate requirement - not for public use.
073    protected SimpleCrossRef() {}
074    
075    /**
076     * {@inheritDoc}
077     */
078    public Annotation getAnnotation() { return getRichAnnotation(); }
079
080    /**
081     * {@inheritDoc}
082     */
083    public RichAnnotation getRichAnnotation() { return this.notes; }
084
085    /**
086     * {@inheritDoc}
087     */
088    public Set getNoteSet() { return this.notes.getNoteSet(); }
089    
090    /**
091     * {@inheritDoc}
092     */
093    public void setNoteSet(Set notes) { this.notes.setNoteSet(notes); }
094    
095    /**
096     * {@inheritDoc}
097     */
098    public String getAccession() { return this.accession; }
099    
100    // Hibernate requirement - not for public use.
101    void setAccession(String accession) { this.accession = accession; }
102    
103    /**
104     * {@inheritDoc}
105     */
106    public String getDbname() { return this.dbname; }
107    
108    // Hibernate requirement - not for public use.
109    void setDbname(String dbname) { this.dbname = dbname; }
110    
111    /**
112     * {@inheritDoc}
113     */
114    public int getVersion() { return this.version; }
115    
116    // Hibernate requirement - not for public use.
117    void setVersion(int version) { this.version = version; }
118    
119    /**
120     * {@inheritDoc}
121     * Compares cross references first by database name, then by accession,
122     * then by version.
123     */
124    public int compareTo(Object o) {
125        if (o==this) return 0;
126        // Hibernate comparison - we haven't been populated yet
127        if (this.dbname==null) return -1;
128        // Normal comparison
129        CrossRef them = (CrossRef)o;
130        if (!this.dbname.equals(them.getDbname())) return this.dbname.compareTo(them.getDbname());
131        if (!this.accession.equals(them.getAccession())) return this.accession.compareTo(them.getAccession());
132        return this.version-them.getVersion();
133    }
134    
135    /**
136     * {@inheritDoc}
137     * Equality is defined as having the same database name, accession and
138     * version.
139     */
140    public boolean equals(Object obj) {
141        if(this == obj) return true;
142        if (obj==null || !(obj instanceof CrossRef)) return false;
143        // Hibernate comparison - we haven't been populated yet
144        if (this.dbname==null) return false;
145        // Normal comparison
146        CrossRef them = (CrossRef)obj;
147        return (this.dbname.equals(them.getDbname()) &&
148                this.accession.equals(them.getAccession()) &&
149                this.version==them.getVersion()
150                );
151    }
152    
153    /**
154     * {@inheritDoc}
155     */
156    public int hashCode() {
157        int code = 17;
158        // Hibernate comparison - we haven't been populated yet
159        if (this.dbname==null) return code;
160        // Normal comparison
161        code = 37*code + this.dbname.hashCode();
162        code = 37*code + this.accession.hashCode();
163        code = 37*code + this.version;
164        return code;
165    }
166    
167    /**
168     * {@inheritDoc}
169     * Form: "dbname:accession.version"
170     */
171    public String toString() {
172        return this.getDbname()+":"+this.getAccession()+"."+this.getVersion();
173    }
174    
175    // Hibernate requirement - not for public use.
176    private Integer id;
177    
178    /**
179     * Gets the Hibernate ID. Should be used with caution.
180     * @return the Hibernate ID, if using Hibernate.
181     */
182    public Integer getId() { return this.id; }
183    
184    /**
185     * Sets the Hibernate ID. Should be used with caution.
186     * @param id the Hibernate ID, if using Hibernate.
187     */
188    public void setId(Integer id) { this.id = id;}
189}
190