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     * Created on 01-21-2010
021     *
022     * @author Richard Holland
023     * @auther Scooter Willis
024     *
025     */
026    package org.biojava3.core.sequence.compound;
027    
028    import static java.util.Arrays.asList;
029    import static java.util.Collections.unmodifiableSet;
030    
031    import java.util.HashSet;
032    import java.util.Set;
033    
034    import org.biojava3.core.sequence.template.AbstractCompound;
035    import org.biojava3.core.sequence.template.Compound;
036    import org.biojava3.core.sequence.template.CompoundSet;
037    import org.biojava3.core.sequence.template.ComplementCompound;
038    
039    /**
040     *
041     * @author Scooter Willis
042     * @author Andy Yates
043     */
044    public class NucleotideCompound extends AbstractCompound implements ComplementCompound {
045    
046        private final CompoundSet<NucleotideCompound> compoundSet;
047        private final String complementStr;
048        private final Set<NucleotideCompound> constituents;
049    
050        public NucleotideCompound(String base, CompoundSet<NucleotideCompound> compoundSet, String complementStr) {
051          super(base);
052          this.compoundSet = compoundSet;
053          this.complementStr = complementStr;
054          this.constituents = unmodifiableSet(new HashSet<NucleotideCompound>(asList(this)));
055        }
056    
057        public NucleotideCompound(String base, CompoundSet<NucleotideCompound> compoundSet, String complementStr, NucleotideCompound[] constituents) {
058            super(base);
059            this.compoundSet = compoundSet;
060            this.complementStr = complementStr;
061            this.constituents = unmodifiableSet(new HashSet<NucleotideCompound>(asList(constituents)));
062        }
063    
064        @Override
065        public String getShortName() {
066          return getBase();
067        }
068    
069        public ComplementCompound getComplement() {
070            return compoundSet.getCompoundForString(complementStr);
071        }
072    
073        public boolean equals(Object obj) {
074            if (obj == null) {
075                return false;
076            }
077            if (!(obj instanceof NucleotideCompound)) {
078                return false;
079            }
080            NucleotideCompound them = (NucleotideCompound) obj;
081            return toString().equals(them.toString());
082        }
083    
084        public int hashCode() {
085            return toString().hashCode();
086        }
087    
088        public boolean equalsIgnoreCase(Compound compound) {
089            if (compound == null) {
090                return false;
091            }
092            if (!(compound instanceof NucleotideCompound)) {
093                return false;
094            }
095            NucleotideCompound them = (NucleotideCompound) compound;
096            return toString().equalsIgnoreCase(them.toString());
097        }
098    
099        public Set<NucleotideCompound> getConsituents() {
100          return constituents;
101        }
102    
103        public boolean isAmbiguous() {
104          return !constituents.isEmpty();
105        }
106    }