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.orDocRef
019 */
020
021package org.biojavax;
022import org.biojava.utils.Unchangeable;
023
024
025/**
026 * Represents an author of a documentary reference.
027 * @author Richard Holland
028 * @author George Waldon
029 * @see DocRef
030 * @since 1.5
031 */
032public class SimpleDocRefAuthor extends Unchangeable implements DocRefAuthor  {
033    
034    private String name;
035    private boolean editor;
036    private boolean consortium;
037    
038    /**
039     * Constructs a new author instance.
040     * @param name the author name
041     * @param consortium are they a consortium?
042     * @param editor are they an editor?
043     */
044    public SimpleDocRefAuthor(String name, boolean consortium, boolean editor) {
045        if (name==null) throw new IllegalArgumentException("Name cannot be null");
046        this.name = name;
047        this.editor = editor;
048        if(editor==true) {
049            int idx = name.lastIndexOf(" (ed.)");
050            if(idx!=-1 && idx==(name.length()-6))
051                this.name = name.substring(0,name.length()-6);
052        }
053        this.consortium = consortium;
054        if(consortium==true) {
055            int idx = name.lastIndexOf(" (consortium)");
056            if(idx!=-1 && idx==(name.length()-13))
057                this.name = name.substring(0,name.length()-13);
058        }
059    }
060    
061    /**
062     * Constructs a new author instance from a string.
063     * @param s the input string (author name with (ed.) and (consortium) suffixes).
064     */
065    public SimpleDocRefAuthor(String s) {
066        if (s==null) throw new IllegalArgumentException("Name cannot be null");
067        String[] parts = s.split("\\(");
068        this.name = parts[0].trim();
069        if (parts.length ==3) {
070            parts[1] = parts[1].trim();
071            parts[2] = parts[2].trim();
072            parts[1] = parts[1].substring(0,parts[1].length()-1); // chomp bracket
073            parts[2] = parts[2].substring(0,parts[2].length()-1); // chomp bracket
074            if (parts[1].equals("ed.") || parts[2].equals("ed.")) this.editor=true;
075            else this.editor = false;
076            if (parts[1].equals("consortium") || parts[2].equals("consortium")) this.consortium = true;
077            else this.consortium = false;
078        } else if (parts.length ==2) {
079            parts[1] = parts[1].trim();
080            parts[1] = parts[1].substring(0,parts[1].length()-1); // chomp bracket
081            if (parts[1].equals("ed.")) this.editor=true;
082            else this.editor = false;
083            if (parts[1].equals("consortium")) this.consortium = true;
084            else this.consortium = false;
085        } else {
086            this.editor = false;
087            this.consortium = false;
088        }
089        if(editor==true) {
090            int idx = this.name.lastIndexOf(" (ed.)");
091            if(idx!=-1 && idx==(this.name.length()-6))
092                this.name = this.name.substring(0,this.name.length()-6);
093        }
094        if(consortium==true) {
095            int idx = this.name.lastIndexOf(" (consortium)");
096            if(idx!=-1 && idx==(this.name.length()-13))
097                this.name = this.name.substring(0,this.name.length()-13);
098        }
099    }
100    
101    /**
102     * {@inheritDoc}
103     */
104    public String getName() {
105        return this.name;
106    }
107    
108    /**
109     * {@inheritDoc}
110     */
111    public String getExtendedName() {
112        StringBuffer result = new StringBuffer();
113        result.append(this.name);
114        if (this.consortium) result.append(" (consortium)");
115        if (this.editor) result.append(" (ed.)");
116        return result.toString();
117    }
118    
119    /**
120     * {@inheritDoc}
121     */
122    public boolean isEditor() {
123        return this.editor;
124    }
125    
126    /**
127     * {@inheritDoc}
128     */
129    public boolean isConsortium() {
130        return this.consortium;
131    }
132    
133    /**
134     * {@inheritDoc}
135     * Document authors are compared first by name, then consortium status, then editor status.
136     */
137    public int compareTo(Object o) {
138        if (o==this) return 0;
139        DocRefAuthor them = (DocRefAuthor)o;
140        if (!this.name.equals(them.getName())) return this.name.compareTo(them.getName());
141        if (this.consortium!=them.isConsortium()) return this.consortium?-1:1;
142        if (this.editor!=them.isEditor()) return this.editor?-1:1;
143        return 0;
144    }
145    
146    /**
147     * {@inheritDoc}
148     * Document references are equal if they have all fields the same.
149     */
150    public boolean equals(Object obj) {
151        if(this == obj) return true;
152        if (obj==null || !(obj instanceof DocRefAuthor)) return false;
153        DocRefAuthor them = (DocRefAuthor)obj;
154        return (this.name.equals(them.getName()) &&
155                this.consortium==them.isConsortium() &&
156                this.editor==them.isEditor());
157    }
158    
159    /**
160     * {@inheritDoc}
161     */
162    public int hashCode() {
163        int code = 17;
164        code = 37*code + this.name.hashCode();
165        code = 37*code + (this.consortium?1:0);
166        code = 37*code + (this.editor?1:0);
167        return code;
168    }
169    
170    /**
171     * {@inheritDoc}
172     * Form: "name (consortium) (ed.)" where sections in brackets are optional.
173     */
174    public String toString() {
175        return this.getExtendedName();
176    }
177}