Class RepresentativeFractionImpl
- Object
-
- Number
-
- RepresentativeFractionImpl
-
- All Implemented Interfaces:
Serializable
,RepresentativeFraction
public class RepresentativeFractionImpl extends Number implements RepresentativeFraction
A scale where denominator =1 / scale
. This implementation is set up as a number - because it is.- Since:
- 2.4
- Author:
- Jody Garnett
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description RepresentativeFractionImpl()
Default empty constructor.RepresentativeFractionImpl(long denominator)
Creates a new representative fraction from the specified denominator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
doubleValue()
Returns the scale in a form usable for computation.boolean
equals(Object object)
Compares this object with the specified value for equality.float
floatValue()
Returns the scale as afloat
type.static RepresentativeFraction
fromScale(double scale)
Creates a representative fraction from a scale as adouble
value.long
getDenominator()
Returns the number below the line in a vulgar fraction.int
hashCode()
Returns a hash value for this representative fraction.int
intValue()
Returns the scale as an integer.long
longValue()
Returns the scale as an integer.void
setDenominator(long denominator)
Sets the denominator value.
-
-
-
Constructor Detail
-
RepresentativeFractionImpl
public RepresentativeFractionImpl()
Default empty constructor.
-
RepresentativeFractionImpl
public RepresentativeFractionImpl(long denominator)
Creates a new representative fraction from the specified denominator.- Parameters:
denominator
- The denominator.
-
-
Method Detail
-
fromScale
public static RepresentativeFraction fromScale(double scale) throws IllegalArgumentException
Creates a representative fraction from a scale as adouble
value. The denominator will be set to1/scale
.- Parameters:
scale
- The scale as a number between 0 and 1.- Returns:
- The representative fraction created from the given scale.
- Throws:
IllegalArgumentException
- if the conditionabs(scale) <= 1
is not meet.
-
doubleValue
public double doubleValue()
Returns the scale in a form usable for computation.- Specified by:
doubleValue
in interfaceRepresentativeFraction
- Specified by:
doubleValue
in classNumber
- Returns:
1.0 / denominator
-
floatValue
public float floatValue()
Returns the scale as afloat
type.- Specified by:
floatValue
in classNumber
-
longValue
public long longValue() throws ArithmeticException
Returns the scale as an integer. This method returns 0, 1 or throws an exception as specified inintValue()
.- Specified by:
longValue
in classNumber
- Throws:
ArithmeticException
- if the denominator is 0.
-
intValue
public int intValue() throws ArithmeticException
Returns the scale as an integer. If the denominator is 0, then this method throws anArithmeticException
since infinities can not be represented by an integer. Otherwise if the denominator is 1, then this method returns 1. Otherwise returns 0 0 since the scale is a fraction between 0 and 1, and such value can not be represented as an integer.- Specified by:
intValue
in classNumber
- Throws:
ArithmeticException
- if the denominator is 0.
-
getDenominator
public long getDenominator()
Returns the number below the line in a vulgar fraction.- Specified by:
getDenominator
in interfaceRepresentativeFraction
- Returns:
- The denominator.
-
setDenominator
public void setDenominator(long denominator)
Sets the denominator value.- Parameters:
denominator
- The new denominator value.
-
equals
public boolean equals(Object object)
Compares this object with the specified value for equality.- Specified by:
equals
in interfaceRepresentativeFraction
- Overrides:
equals
in classObject
- Parameters:
object
- The object to compare with.- Returns:
true
if both objects are equal.
-
hashCode
public int hashCode()
Returns a hash value for this representative fraction.- Specified by:
hashCode
in interfaceRepresentativeFraction
- Overrides:
hashCode
in classObject
- Returns:
- A hash code value for this representative fraction.
-
-