001    /**
002     * Copyright (C) 2011 The Roslin Institute <contact andy.law@roslin.ed.ac.uk>
003     *
004     * This file is part of the Ensembl Java API demonstration project developed by the
005     * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of
006     * Veterinary Studies, University of Edinburgh.
007     *
008     * This is free software: you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (version 3) as published by
010     * the Free Software Foundation.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * in this software distribution. If not, see <http://www.gnu.org/licenses/gpl-3.0.html/>.
019     */
020    package uk.ac.roslin.ensembl.model.relationship;
021    
022    import java.io.ObjectStreamException;
023    import java.util.Collection;
024    import java.util.HashMap;
025    import uk.ac.roslin.ensembl.config.EnsemblType;
026    
027    public class RelationshipType extends EnsemblType {
028    
029        //REMEMBER : IF YOU SUBTYPE THIS YOU SHOULD "OVERRIDE" THE STATIC METHODS etc.
030    
031        private static HashMap<String, RelationshipType> typeHash;
032    
033        // the types....
034        public static final RelationshipType UNKNOWN = RelationshipType.makeRelationshipType("unknown");
035    
036        private static RelationshipType makeRelationshipType(String typeName) {
037            if (RelationshipType.typeHash == null) {
038                RelationshipType.typeHash = new HashMap<String, RelationshipType>();
039            }
040    
041            RelationshipType rt = getRelationshipType(typeName);
042    
043            if (rt == null) {
044                rt = new RelationshipType(typeName);
045                RelationshipType.typeHash.put(typeName, rt);
046            }
047            return rt;
048        }
049    
050        protected RelationshipType(String value) {
051            this.label = value;
052        }
053    
054        public static Collection<? extends RelationshipType> getAllTypes() {
055            return typeHash.values();
056        }
057    
058        public static RelationshipType getRelationshipType(String value) {
059            return typeHash.get(value);
060        }
061    
062        /**
063         * Ensure Singleton class
064         */
065        private Object readResolve() throws ObjectStreamException {
066            return this;
067        }
068    
069        /**
070         * Overrides the Object method to test for equality
071         * @param   obj   the reference object with which to compare.
072         * @return  <code>true</code> if this object is the same as the obj
073         *          argument; <code>false</code> otherwise.
074         */
075        @Override
076        public boolean equals(Object obj) {
077            if (obj == null) {
078                return false;
079            }
080            if (getClass() != obj.getClass()) {
081                return false;
082            }
083    
084            String otherString = obj.toString();
085            return this.toString().equals(otherString);
086    
087        }
088    
089        /**
090         * hashCode() is overriden because we have overridden equals.
091         * @return int the hash code
092         */
093        @Override
094        public int hashCode() {
095            int hash = 7;
096            hash = 59 * hash + (this.label != null ? this.label.hashCode() : 0);
097            return hash;
098        }
099    }