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 */
026package org.biojava.nbio.core.sequence.compound;
027
028import org.biojava.nbio.core.sequence.template.AbstractCompound;
029import org.biojava.nbio.core.sequence.template.ComplementCompound;
030import org.biojava.nbio.core.sequence.template.Compound;
031import org.biojava.nbio.core.sequence.template.CompoundSet;
032
033import java.util.HashSet;
034import java.util.Set;
035
036import static java.util.Arrays.asList;
037import static java.util.Collections.unmodifiableSet;
038
039/**
040 *
041 * @author Scooter Willis
042 * @author Andy Yates
043 */
044public 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        @Override
070        public ComplementCompound getComplement() {
071                return compoundSet.getCompoundForString(complementStr);
072        }
073
074        @Override
075        public boolean equals(Object obj) {
076                if (obj == null) {
077                        return false;
078                }
079                if (!(obj instanceof NucleotideCompound)) {
080                        return false;
081                }
082                NucleotideCompound them = (NucleotideCompound) obj;
083                return toString().equals(them.toString());
084        }
085
086        @Override
087        public int hashCode() {
088                return toString().hashCode();
089        }
090
091        @Override
092        public boolean equalsIgnoreCase(Compound compound) {
093                if (compound == null) {
094                        return false;
095                }
096                if (!(compound instanceof NucleotideCompound)) {
097                        return false;
098                }
099                NucleotideCompound them = (NucleotideCompound) compound;
100                return toString().equalsIgnoreCase(them.toString());
101        }
102
103        public Set<NucleotideCompound> getConstituents() {
104          return constituents;
105        }
106
107        public boolean isAmbiguous() {
108          return !constituents.isEmpty();
109        }
110}