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
022
023package org.biojavax.ga.impl;
024
025import java.util.Iterator;
026import java.util.Set;
027
028import org.biojava.utils.AbstractChangeable;
029import org.biojava.utils.ChangeEvent;
030import org.biojava.utils.ChangeSupport;
031import org.biojava.utils.ChangeVetoException;
032import org.biojavax.ga.GeneticAlgorithm;
033import org.biojavax.ga.Organism;
034import org.biojavax.ga.Population;
035import org.biojavax.ga.exception.IllegalOrganismException;
036
037/**
038 * Most Population implementations will want to inherit from here.
039 * This class doesn't define how Organims are stored or accessed so inheriting classes
040 * can define that themselves.
041 *
042 * @author Mark Schreiber
043 * @version 1.0
044 * @since 1.5
045 */
046
047public abstract class AbstractPopulation extends AbstractChangeable implements Population {
048  String name;
049
050  public AbstractPopulation() {
051    this("");
052  }
053
054  public AbstractPopulation(String name){
055    this.name = name;
056  }
057
058  public String getName() {
059    return name;
060  }
061  public final void setName(String name) throws ChangeVetoException {
062    if(!hasListeners()){
063      this.name = name;
064   }else{
065     ChangeEvent ce = new ChangeEvent(this,
066                                      Population.NAME,
067                                      name,
068                                      this.name
069                                      );
070     ChangeSupport changeSupport = super.getChangeSupport(GeneticAlgorithm.POPULATION);
071     synchronized(changeSupport){
072       changeSupport.firePreChangeEvent(ce);
073       this.name = name;
074       changeSupport.firePostChangeEvent(ce);
075     }
076    }
077  }
078
079  public final void addOrganism(Organism org) throws ChangeVetoException, IllegalOrganismException {
080    if(!hasListeners()){
081      addOrganismImpl(org);
082   }else{
083     ChangeEvent ce = new ChangeEvent(this,
084                                      Population.ORGANISMS,
085                                      org,
086                                      getOrganisms()
087                                      );
088     ChangeSupport changeSupport = super.getChangeSupport(GeneticAlgorithm.POPULATION);
089     synchronized(changeSupport){
090       changeSupport.firePreChangeEvent(ce);
091       addOrganismImpl(org);
092       changeSupport.firePostChangeEvent(ce);
093     }
094    }
095  }
096
097  protected abstract void addOrganismImpl(Organism org) throws IllegalOrganismException;
098
099  public final void addOrganisms(Organism[] orgs) throws ChangeVetoException, IllegalOrganismException {
100    for (int i = 0; i < orgs.length; i++) {
101      addOrganism(orgs[i]);
102    }
103  }
104
105  public final void addOrganisms(Set orgs)throws ChangeVetoException, IllegalOrganismException {
106    for (Iterator i = orgs.iterator(); i.hasNext(); ) {
107      Object o = i.next();
108
109      if (o instanceof Organism) {
110        addOrganism((Organism)o);
111      }
112      else {
113        throw new IllegalOrganismException(
114            "Attempting to add a non Organism object to a population, object is: "+
115            o.getClass().getName()
116            );
117      }
118    }
119  }
120  public final void addOrganisms(Population orgs) throws ChangeVetoException, IllegalOrganismException {
121    for (Iterator i = orgs.organisms(); i.hasNext(); ) {
122      Object o = i.next();
123
124      if (o instanceof Organism) {
125        addOrganism((Organism)o);
126      }
127      else {
128        throw new IllegalOrganismException(
129              "Attempting to add a non Organism object to a population, object is: "+
130              o.getClass().getName()
131            );
132      }
133
134    }
135  }
136
137
138
139
140
141  public final void removeOrganisms(Organism[] orgs)throws ChangeVetoException{
142    for (int i = 0; i < orgs.length; i++) {
143      removeOrganismImpl(orgs[i]);
144    }
145  }
146
147  public final void removeOrganisms(Set orgs) throws ChangeVetoException{
148    for (Iterator i = orgs.iterator(); i.hasNext(); ) {
149      removeOrganismImpl((Organism)i.next());
150    }
151  }
152
153  public final void removeAllOrganisms() throws ChangeVetoException{
154    if(!hasListeners()){
155      removeAllOrganismsImpl();
156   }else{
157     ChangeEvent ce = new ChangeEvent(this,
158                                      Population.ORGANISMS,
159                                      null,
160                                      getOrganisms()
161                                      );
162     ChangeSupport changeSupport = super.getChangeSupport(GeneticAlgorithm.POPULATION);
163     synchronized(changeSupport){
164       changeSupport.firePreChangeEvent(ce);
165       removeAllOrganismsImpl();
166       changeSupport.firePostChangeEvent(ce);
167     }
168    }
169  }
170
171
172  public final void removeOrganism(Organism org) throws ChangeVetoException {
173    if(!hasListeners()){
174      removeOrganismImpl(org);
175   }else{
176     ChangeEvent ce = new ChangeEvent(this,
177                                      Population.ORGANISMS,
178                                      org,
179                                      getOrganisms()
180                                      );
181     ChangeSupport changeSupport = super.getChangeSupport(GeneticAlgorithm.POPULATION);
182     synchronized(changeSupport){
183       changeSupport.firePreChangeEvent(ce);
184       removeOrganismImpl(org);
185       changeSupport.firePostChangeEvent(ce);
186     }
187    }
188  }
189
190  protected abstract void removeOrganismImpl(Organism org);
191  protected abstract void removeAllOrganismsImpl();
192}