Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 167   Methods: 4
NCLOC: 75   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NumberGene.java 100% 100% 100% 100%
coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.impl;
 11   
 12    import org.jgap.*;
 13   
 14    /**
 15    * Base class for all Genes based on numbers.
 16    *
 17    * @author Klaus Meffert
 18    * @since 1.1 (most code moved and adapted from IntegerGene)
 19    */
 20    public abstract class NumberGene
 21    extends BaseGene {
 22    /** String containing the CVS revision. Read out via reflection!*/
 23    private static final String CVS_REVISION = "$Revision: 1.22 $";
 24   
 25    /**
 26    * References the internal value (allele) of this Gene
 27    * E.g., for DoubleGene this is of type Double
 28    */
 29    private Object m_value;
 30   
 31  95904 public NumberGene(Configuration a_config) throws InvalidConfigurationException {
 32  95904 super(a_config);
 33    }
 34   
 35    /**
 36    * Compares this NumberGene with the specified object (which must also
 37    * be a NumberGene) for order, which is determined by the number value of
 38    * this Gene compared to the one provided for comparison.
 39    *
 40    * @param a_other the NumberGene to be compared to this NumberGene
 41    * @return a negative integer, zero, or a positive integer as this object
 42    * is less than, equal to, or greater than the object provided for comparison
 43    *
 44    * @throws ClassCastException if the specified object's type prevents it
 45    * from being compared to this NumberGene
 46    *
 47    * @author Klaus Meffert
 48    * @since 1.1
 49    */
 50  1722938 public int compareTo(final Object a_other) {
 51  1722938 NumberGene otherGene = (NumberGene) a_other;
 52    // First, if the other gene (or its value) is null, then this is
 53    // the greater allele. Otherwise, just use the overridden compareToNative
 54    // method to perform the comparison.
 55    // ----------------------------------------------------------------------
 56  1712864 if (otherGene == null) {
 57  3 return 1;
 58    }
 59  1712861 else if (otherGene.m_value == null) {
 60    // Check if type corresponds (because we could have a type not inherited
 61    // from NumberGene).
 62    // ---------------------------------------------------------------------
 63  22431 if (!otherGene.getClass().equals(getClass())) {
 64  2490 throw new ClassCastException(
 65    "Comparison not possible: different types!");
 66    }
 67    // If our value is also null, then we're the same. Otherwise,
 68    // this is the greater gene.
 69    // ----------------------------------------------------------
 70  19941 if (m_value == null) {
 71  19934 if (isCompareApplicationData()) {
 72  2 return compareApplicationData(getApplicationData(),
 73    otherGene.getApplicationData());
 74    }
 75    else {
 76  19932 return 0;
 77    }
 78    }
 79    else {
 80  7 return 1;
 81    }
 82    }
 83    else {
 84  1690430 try {
 85  1690430 if (!otherGene.getClass().equals(getClass())) {
 86  4 throw new ClassCastException(
 87    "Comparison not possible: different types!");
 88    }
 89  1690426 if (m_value == null) {
 90  2 return -1;
 91    }
 92  1690424 int res = compareToNative(m_value, otherGene.m_value);
 93  1690424 if (res == 0) {
 94  794956 if (isCompareApplicationData()) {
 95  10 return compareApplicationData(getApplicationData(),
 96    otherGene.getApplicationData());
 97    }
 98    else {
 99  794946 return 0;
 100    }
 101    }
 102    else {
 103  895468 return res;
 104    }
 105    }
 106    catch (ClassCastException e) {
 107  4 throw e;
 108    }
 109    }
 110    }
 111   
 112    /**
 113    * Compares to objects by first casting them into their expected type
 114    * (e.g. Integer for IntegerGene) and then calling the compareTo-method
 115    * of the casted type.
 116    * @param a_o1 first object to be compared, always is not null
 117    * @param a_o2 second object to be compared, always is not null
 118    * @return a negative integer, zero, or a positive integer as this object
 119    * is less than, equal to, or greater than the object provided for comparison
 120    *
 121    * @author Klaus Meffert
 122    * @since 1.1
 123    */
 124    protected abstract int compareToNative(Object a_o1, Object a_o2);
 125   
 126    /**
 127    * Sets the value (allele) of this Gene to the new given value. This class
 128    * expects the value to be an instance of current type (e.g. Integer).
 129    * If the value is above or below the upper or lower bounds, it will be
 130    * mappped to within the allowable range.
 131    *
 132    * @param a_newValue the new value of this Gene instance
 133    *
 134    * @author Klaus Meffert
 135    * @since 1.1
 136    */
 137  74487 public void setAllele(final Object a_newValue) {
 138  74487 if (getConstraintChecker() != null) {
 139  4 if (!getConstraintChecker().verify(this, a_newValue, null, -1)) {
 140  1 return;
 141    }
 142    }
 143  74486 m_value = a_newValue;
 144    // If the value isn't between the upper and lower bounds of this
 145    // Gene, map it to a value within those bounds.
 146    // -------------------------------------------------------------
 147  74486 mapValueToWithinBounds();
 148    }
 149   
 150    /**
 151    * Maps the value of this NumberGene to within the bounds specified by
 152    * the m_upperBounds and m_lowerBounds instance variables. The value's
 153    * relative position within the integer range will be preserved within the
 154    * bounds range (in other words, if the value is about halfway between the
 155    * integer max and min, then the resulting value will be about halfway
 156    * between the upper bounds and lower bounds). If the value is null or
 157    * is already within the bounds, it will be left unchanged.
 158    *
 159    * @author Klaus Meffert
 160    * @since 1.1
 161    */
 162    protected abstract void mapValueToWithinBounds();
 163   
 164  394418 protected Object getInternalValue() {
 165  394418 return m_value;
 166    }
 167    }