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.functions;
024
025import org.biojava.bio.BioError;
026import org.biojava.utils.AbstractChangeable;
027import org.biojava.utils.ChangeEvent;
028import org.biojava.utils.ChangeSupport;
029import org.biojava.utils.ChangeVetoException;
030
031/**
032 * Abstract implementation of <code>CrossOverFunction</code>. All custom
033 * implementations should inherit from here.
034 * @author Mark Schreiber
035 * @version 1.0
036 * @since 1.5
037 */
038public abstract class AbstractCrossOverFunction extends AbstractChangeable 
039        implements CrossOverFunction {
040  private int maxCross;
041  private double[] crossProbs;
042
043  protected AbstractCrossOverFunction() {
044    try {
045      setMaxCrossOvers(CrossOverFunction.DEFAULT_MAX_CROSS);
046      setCrossOverProbs(CrossOverFunction.DEFAULT_CROSS_PROB);
047    }
048    catch (ChangeVetoException ex) {
049      throw new BioError("Cannot set the default values of the CrossOverFunction", ex);
050    }
051  }
052
053  public final void setMaxCrossOvers(int maxCrossOvers) throws ChangeVetoException {
054    if(!hasListeners()){
055      maxCross = maxCrossOvers;
056    }else{
057      ChangeEvent ce = new ChangeEvent(this,
058                                       CrossOverFunction.MAX_CROSSES,
059                                       new Integer(maxCrossOvers),
060                                       new Integer(this.maxCross)
061                                       );
062      ChangeSupport changeSupport = super.getChangeSupport(CrossOverFunction.MAX_CROSSES);
063      synchronized(changeSupport){
064        changeSupport.firePreChangeEvent(ce);
065        maxCross = maxCrossOvers;
066        changeSupport.firePostChangeEvent(ce);
067      }
068    }
069  }
070
071  public final int getMaxCrossOvers() {
072    return maxCross;
073  }
074
075  public final void setCrossOverProbs(double[] crossOverProbs) throws ChangeVetoException {
076    if(!hasListeners()){
077      crossProbs = crossOverProbs;
078    }else{
079      ChangeEvent ce = new ChangeEvent(this,
080                                       CrossOverFunction.CROSS_PROB,
081                                       crossOverProbs,
082                                       this.crossProbs
083                                       );
084      ChangeSupport changeSupport = super.getChangeSupport(CrossOverFunction.CROSS_PROB);
085      synchronized(changeSupport){
086        changeSupport.firePreChangeEvent(ce);
087        crossProbs = crossOverProbs;
088        changeSupport.firePostChangeEvent(ce);
089      }
090    }
091  }
092  public final double[] getCrossOverProbs() {
093    return crossProbs;
094  }
095}