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.bio.taxa;
023
024/**
025 * Represents a name class plus name combination for an NCBITaxon object.
026 * @author Richard Holland
027 * @since 1.5
028 */
029public class SimpleNCBITaxonName implements Comparable {
030    
031    private String nameClass;
032    private String name;
033
034    // Hibernate requirement - not for public use.
035    protected SimpleNCBITaxonName() {}
036    
037    /**
038     * Creates a new taxon name based on the given class and name, both of
039     * which cannot be null.
040     * @param nameClass the class of the new name. Use one of the constants from
041     * {@link org.biojavax.bio.taxa.NCBITaxon} (for example {@link org.biojavax.bio.taxa.NCBITaxon#SCIENTIFIC}).
042     * @param name the name itself
043     */
044    public SimpleNCBITaxonName(String nameClass, String name) {
045        if (nameClass==null) throw new IllegalArgumentException("Name class cannot be null");
046        if (name==null) throw new IllegalArgumentException("Name cannot be null");
047        if (name.indexOf('\n') >= 0) throw new IllegalArgumentException("NCBI taxonomy names cannot embed new lines - at:"+name.indexOf('\n')+", in name: <"+name+">");
048        this.nameClass = nameClass;
049        this.name = name; 
050    }
051    
052    /**
053     * Changes the class of this name.
054     * @param nameClass the new class for this name.
055     */
056    public void setNameClass(String nameClass) { 
057        if (nameClass==null) throw new IllegalArgumentException("Name class cannot be null");
058        this.nameClass = nameClass; 
059    }
060    
061    /**
062     * Returns the class of this name.
063     * @return the class of this name.
064     */
065    public String getNameClass() { return this.nameClass; }
066    
067    /**
068     * Changes the name.
069     * @param name the new name.
070     */
071    public void setName(String name) {       
072        if (name==null) throw new IllegalArgumentException("Name cannot be null");
073        this.name = name; 
074    }
075    
076    /**
077     * Returns this name.
078     * @return this name.
079     */
080    public String getName() { return this.name; }
081    
082    /**
083     * {@inheritDoc}
084     * Two taxon names are equal if their name and class match.
085     */
086    public boolean equals(Object o) {
087        if (o==this) return true;
088        if (!(o instanceof SimpleNCBITaxonName)) return false;
089        // Hibernate comparison - we haven't been populated yet
090        if (this.nameClass==null) return false;
091        // Normal comparison
092        SimpleNCBITaxonName them = (SimpleNCBITaxonName) o;
093        return them.getNameClass().equals(this.nameClass) &&
094                them.getName().equals(this.name);
095    }    
096    
097    /**
098     * {@inheritDoc}
099     * Taxon names are sorted by class first, then name.
100     */
101    public int compareTo(Object o) {
102        if (o==this) return 0;
103        // Hibernate comparison - we haven't been populated yet
104        if (this.nameClass==null) return -1;
105        // Normal comparison
106        SimpleNCBITaxonName them = (SimpleNCBITaxonName)o;
107        if (!them.getNameClass().equals(this.nameClass)) return this.nameClass.compareTo(them.getNameClass());
108        return this.name.compareTo(them.getName());
109    }
110    
111    /**
112     * {@inheritDoc}
113     */
114    public int hashCode() {
115        int code = 17;
116        // Hibernate comparison - we haven't been populated yet
117        if (this.nameClass==null) return code;
118        // Normal comparison
119        code = 31*code + this.name.hashCode();
120        code = 31*code + this.nameClass.hashCode();
121        return code;
122    }
123    
124    /**
125     * {@inheritDoc}
126     * Form: "class:name"
127     */
128    public String toString() {
129        return this.nameClass+":"+this.name;
130    }
131}