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
022package org.biojavax.ga.functions;
023
024import org.biojava.bio.dist.OrderNDistribution;
025import org.biojava.utils.AbstractChangeable;
026import org.biojava.utils.ChangeEvent;
027import org.biojava.utils.ChangeSupport;
028import org.biojava.utils.ChangeVetoException;
029
030/**
031 * Abstract implementation of <code>MutationFunction</code> all custom
032 * implementations should inherit from here.
033 * @author Mark Schreiber
034 * @version 1.0
035 * @since 1.5
036 */
037public abstract class AbstractMutationFunction
038    extends AbstractChangeable implements MutationFunction {
039
040  private double[] mutationProbs;
041  private OrderNDistribution mutationSpectrum;
042
043  protected AbstractMutationFunction() {
044    mutationProbs = MutationFunction.DEFAULT_MUTATION_PROBS;
045  }
046
047  public final void setMutationProbs(double[] mutationProbs) throws ChangeVetoException {
048    if(!hasListeners()){
049      this.mutationProbs = mutationProbs;
050    }else{
051      ChangeEvent ce = new ChangeEvent(this,
052                                       MutationFunction.MUTATION_PROBS,
053                                       mutationProbs,
054                                       this.mutationProbs
055                                       );
056      ChangeSupport changeSupport = super.getChangeSupport(MutationFunction.MUTATION_PROBS);
057      synchronized(changeSupport){
058        changeSupport.firePreChangeEvent(ce);
059          this.mutationProbs = mutationProbs;
060        changeSupport.firePostChangeEvent(ce);
061      }
062    }
063  }
064  public final double[] getMutationProbs() {
065    return mutationProbs;
066  }
067
068  public final void setMutationSpectrum(OrderNDistribution mutationSpectrum)
069      throws ChangeVetoException {
070
071    if(!hasListeners()){
072      this.mutationSpectrum = mutationSpectrum;
073    }else{
074      ChangeEvent ce = new ChangeEvent(this,
075                                       MutationFunction.MUTATION_SPECTRUM,
076                                       mutationSpectrum,
077                                       this.mutationSpectrum
078                                       );
079      ChangeSupport changeSupport =
080          super.getChangeSupport(MutationFunction.MUTATION_SPECTRUM);
081      synchronized(changeSupport){
082        changeSupport.firePreChangeEvent(ce);
083          this.mutationSpectrum = mutationSpectrum;
084        changeSupport.firePostChangeEvent(ce);
085      }
086    }
087  }
088  public final OrderNDistribution getMutationSpectrum() {
089    return mutationSpectrum;
090  }
091}