DefaultRepresentativeFraction at revision 24897

The key point in this contribution is the idea to define RepresentativeFraction as a kind of java.lang.Number. But this is not really an idea specific to that commit, since it is suggested by the GeoAPI interface javadoc. From that point, everything else is mandated by the GeoAPI interface and the Number contract.

This code has been rewritten in Geotk/SIS. But because of the above constraints, it can only vary in minor details: intValue() checking for the case where the denominator is equals to 1, the useless (obj == null) check removed from the equals method, the serialVersionUID value and name of the static factory method. Note that the details of the hashCode() method is specified in the GeoAPI interface, so that method can not be different.

Command line:

svn cat -r24897 https://svn.osgeo.org/geotools/trunk/modules/library/metadata/src/main/java/org/geotools/metadata/iso/identification/RepresentativeFractionImpl.java
Revision 24897
package org.geotools.metadata.iso.identification;

import org.opengis.metadata.identification.RepresentativeFraction;

/**
 * Set this up as a number - because it is.
 *
 * @author Jody
 */
public class RepresentativeFractionImpl extends Number implements RepresentativeFraction {
    private static final long serialVersionUID = 7228422109144637537L;
    int denominator;

    public RepresentativeFractionImpl( int denominator ){
        this.denominator = denominator;
    }

    public static RepresentativeFraction fromDouble( double value ){
        return new RepresentativeFractionImpl( (int) (1.0 / value) ); // flip!
    }
    public double doubleValue() {
        return 1.0 / (double) denominator;
    }
    public float floatValue() {
        return 1.0f / (float) denominator;
    }
    public int intValue() {
        return 0;
    }
    public long longValue() {
        return 0;
    }
    public int getDenominator() {
        return denominator;
    }
    public boolean equals( Object obj ) {
        if( obj == null || !(obj instanceof RepresentativeFraction) ){
            return false;
        }
        RepresentativeFraction other = (RepresentativeFraction) obj;
        return denominator == other.getDenominator();
    }
    public int hashCode() {
        return denominator;
    }
}