001/*
002 * BioJava development code This code may be freely distributed and modified
003 * under the terms of the GNU Lesser General Public Licence. This should be
004 * distributed with the code. If you do not have a copy, see:
005 * http://www.gnu.org/copyleft/lesser.html Copyright for this code is held
006 * jointly by the individual authors. These should be listed in @author doc
007 * comments. For more information on the BioJava project and its aims, or to
008 * join the biojava-l mailing list, visit the home page at:
009 * http://www.biojava.org/
010 */
011
012package org.biojavax.ga.impl;
013
014import org.biojava.bio.symbol.SymbolList;
015import org.biojava.utils.AbstractChangeable;
016import org.biojava.utils.ChangeEvent;
017import org.biojava.utils.ChangeSupport;
018import org.biojava.utils.ChangeVetoException;
019import org.biojavax.ga.Organism;
020
021/**
022 * Abstract implementation of Organism. Most implementations would want to
023 * inherit from here.
024 *
025 * @author Mark Schreiber
026 * @version 1.0
027 * @since 1.5
028 */
029
030public abstract class AbstractOrganism extends AbstractChangeable implements
031    Organism {
032
033        private double[]        fitness;
034
035        protected SymbolList[]     chromosomes;
036
037        String           name;
038
039        protected AbstractOrganism() {
040                chromosomes = new SymbolList[0];
041                name = "";
042        }
043
044        protected AbstractOrganism(Organism org, String name) {
045                chromosomes = org.getChromosomes();
046                this.name = name;
047        }
048
049        public final SymbolList[] getChromosomes() {
050                return chromosomes;
051        }
052
053        /*
054         * (non-Javadoc)
055         *
056         * @see org.biojavax.ga.Organism#getFitness()
057         */
058        public final double[] getFitness() {
059                return fitness;
060        }
061
062        public String getName() {
063                return name;
064        }
065
066        public abstract boolean isHaploid();
067
068        protected abstract void setChromImpl(SymbolList[] chromosomes);
069
070        public final void setChromosomes(SymbolList[] chromosomes)
071            throws ChangeVetoException {
072                if (!hasListeners()) {
073                        setChromImpl(chromosomes);
074                } else {
075                        ChangeEvent ce = new ChangeEvent(this, Organism.CHROMOSOMES, chromosomes,
076                            this.chromosomes);
077                        ChangeSupport changeSupport = super
078                            .getChangeSupport(Organism.CHROMOSOMES);
079                        synchronized (changeSupport) {
080                                changeSupport.firePreChangeEvent(ce);
081                                this.chromosomes = chromosomes;
082                                changeSupport.firePostChangeEvent(ce);
083                        }
084                }
085        }
086
087        /*
088         * (non-Javadoc)
089         *
090         * @see org.biojavax.ga.Organism#setFitness(double[])
091         */
092        public final void setFitness(double[] fitness) {
093                this.fitness = fitness.clone();
094        }
095
096        public final void setName(String name) throws ChangeVetoException {
097                if (!hasListeners()) {
098                        this.name = name;
099                } else {
100                        ChangeEvent ce = new ChangeEvent(this, Organism.NAME, chromosomes,
101                            this.chromosomes);
102                        ChangeSupport changeSupport = super.getChangeSupport(Organism.NAME);
103                        synchronized (changeSupport) {
104                                changeSupport.firePreChangeEvent(ce);
105                                this.name = name;
106                                changeSupport.firePostChangeEvent(ce);
107                        }
108                }
109        }
110
111}