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.net.URI;
025import java.net.URISyntaxException;
026
027import org.biojava.utils.AbstractChangeable;
028import org.biojava.utils.ChangeEvent;
029import org.biojava.utils.ChangeSupport;
030import org.biojava.utils.ChangeVetoException;
031
032/**
033 * A basic Namespace implemenation.
034 * @author Richard Holland
035 * @author Mark Schreiber
036 * @since 1.5
037 */
038
039public class SimpleNamespace extends AbstractChangeable implements Namespace {
040    
041    private String name;
042    private String acronym;
043    private String authority;
044    private String description;
045    private URI URI;
046    
047    /**
048     * Creates a new instance of SimpleNamespace with the given name,
049     * which cannot be null.
050     * @param name the name of the namespace.
051     */
052    public SimpleNamespace(String name) {
053        if (name==null) throw new IllegalArgumentException("Name cannot be null");
054        this.name = name;
055        this.acronym = null;
056        this.authority = null;
057        this.description = null;
058        this.URI = null;
059    }
060    
061    // Hibernate requirement - not for public use.
062    protected SimpleNamespace() {}
063    
064    /**
065     * {@inheritDoc}
066     */
067    public void setAcronym(String acronym) throws ChangeVetoException {
068        if(!this.hasListeners(Namespace.ACRONYM)) {
069            this.acronym = acronym;
070        } else {
071            ChangeEvent ce = new ChangeEvent(
072                    this,
073                    Namespace.ACRONYM,
074                    acronym,
075                    this.acronym
076                    );
077            ChangeSupport cs = this.getChangeSupport(Namespace.ACRONYM);
078            synchronized(cs) {
079                cs.firePreChangeEvent(ce);
080                this.acronym = acronym;
081                cs.firePostChangeEvent(ce);
082            }
083        }
084    }
085    
086    /**
087     * {@inheritDoc}
088     */
089    public void setAuthority(String authority) throws ChangeVetoException {
090        if(!this.hasListeners(Namespace.AUTHORITY)) {
091            this.authority = authority; } else {
092            ChangeEvent ce = new ChangeEvent(
093                    this,
094                    Namespace.AUTHORITY,
095                    authority,
096                    this.authority
097                    );
098            ChangeSupport cs = this.getChangeSupport(Namespace.AUTHORITY);
099            synchronized(cs) {
100                cs.firePreChangeEvent(ce);
101                this.authority = authority;
102                cs.firePostChangeEvent(ce);
103            }
104            }
105    }
106    
107    /**
108     * {@inheritDoc}
109     */
110    public void setDescription(String description) throws ChangeVetoException {
111        if(!this.hasListeners(Namespace.DESCRIPTION)) {
112            this.description = description;
113        } else {
114            ChangeEvent ce = new ChangeEvent(
115                    this,
116                    Namespace.DESCRIPTION,
117                    description,
118                    this.description
119                    );
120            ChangeSupport cs = this.getChangeSupport(Namespace.DESCRIPTION);
121            synchronized(cs) {
122                cs.firePreChangeEvent(ce);
123                this.description = description;
124                cs.firePostChangeEvent(ce);
125            }
126        }
127    }
128    
129    // Hibernate requirement - not for public use.
130    // Converts a String object representing a URI into an actual URI object.
131    void setURIString(String URI) throws ChangeVetoException, URISyntaxException {
132        if (URI!=null) this.setURI(new URI(URI));
133        else this.URI=null;
134    }
135    
136    // Hibernate requirement - not for public use.
137    // Converts a URI object into a String representation of that URI
138    String getURIString() {
139        if (this.URI==null) return null;
140        else return this.URI.toASCIIString();
141    }
142    
143    /**
144     * {@inheritDoc}
145     */
146    public void setURI(URI URI) throws ChangeVetoException {
147        if(!this.hasListeners(Namespace.URI)) {
148            this.URI = URI;
149        } else {
150            ChangeEvent ce = new ChangeEvent(
151                    this,
152                    Namespace.URI,
153                    URI,
154                    this.URI
155                    );
156            ChangeSupport cs = this.getChangeSupport(Namespace.URI);
157            synchronized(cs) {
158                cs.firePreChangeEvent(ce);
159                this.URI = URI;
160                cs.firePostChangeEvent(ce);
161            }
162        }
163    }
164    
165    // Hibernate requirement - not for public use.
166    void setName(String name) { this.name = name; }
167    
168    /**
169     * {@inheritDoc}
170     */
171    public String getAcronym() { return this.acronym; }
172    
173    /**
174     * {@inheritDoc}
175     */
176    public String getAuthority() { return this.authority; }
177    
178    /**
179     * {@inheritDoc}
180     */
181    public String getDescription() { return this.description; }
182    
183    /**
184     * {@inheritDoc}
185     */
186    public String getName() { return this.name; }
187    
188    /**
189     * {@inheritDoc}
190     */
191    public URI getURI() { return this.URI; }
192    
193    /**
194     * {@inheritDoc}
195     * Namespaces are compared only by name.
196     */
197    public int compareTo(Object o) {
198        if (o==this) return 0;
199        // Hibernate comparison - we haven't been populated yet
200        if (this.name==null) return -1;
201        // Normal comparison
202        Namespace them = (Namespace)o;
203        return this.name.compareTo(them.getName());
204    }
205    
206    /**
207     * {@inheritDoc}
208     * Namespaces are equal only by name.
209     */
210    public boolean equals(Object obj) {
211        if(this == obj) return true;
212        if (obj==null || !(obj instanceof Namespace)) return false;
213        // Hibernate comparison - we haven't been populated yet
214        if (this.name==null) return false;
215        // Normal comparison
216        Namespace them = (Namespace)obj;
217        return this.name.equals(them.getName());
218    }
219    
220    /**
221     * {@inheritDoc}
222     */
223    public int hashCode() {
224        int hash = 17;
225        // Hibernate comparison - we haven't been populated yet
226        if (this.name==null) return hash;
227        // Normal comparison
228        return 31*hash + this.name.hashCode();
229    }
230    
231    /**
232     * {@inheritDoc}
233     * Form: "name"
234     */
235    public String toString() { 
236        return this.getName(); 
237    }
238    
239    // Hibernate requirement - not for public use.
240    private Integer id;
241    
242    /**
243     * Gets the Hibernate ID. Should be used with caution.
244     * @return the Hibernate ID, if using Hibernate.
245     */
246    public Integer getId() { return this.id; }
247    
248    /**
249     * Sets the Hibernate ID. Should be used with caution.
250     * @param id the Hibernate ID, if using Hibernate.
251     */
252    public void setId(Integer id) { this.id = id;}
253}