Media Authoring
with Java API

tv.amwa.maj.embeddable
Class Rational

java.lang.Object
  extended by java.lang.Number
      extended by tv.amwa.maj.embeddable.Rational
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Rational>, MAJCommon, XMLSerializable, Rational

public final class Rational
extends Number
implements Rational, Serializable, XMLSerializable, Cloneable, Comparable<Rational>, MAJCommon

Implements a rational number by means of a Java integer numerator (Int32) and a Java integer (Int32) denominator.

This implementation of the AAF rational number interface extends Java's Number class so that its integer and floating point value can be found, by methods such as intValue() and doubleValue(). Numerical comparison with other numbers can take place using the compareToNumber(Number) method.

Author:
Richard Cartwright
See Also:
TypeDefinitionRecord.Rational, TimelineMobSlot.getEditRate(), Serialized Form

Constructor Summary
Rational()
          Create a rational number with a numerator of 0 and a denominator of 1.
Rational(int numerator, int denominator)
          Create a rational number from its numerator and denominator.
 
Method Summary
 void appendXMLChildren(Node parent)
          Append child elements to the given parent node to serialize the value of an object to an XML fragment.
static Rational castFromInterface(Rational alien)
          Cast a tv.amwa.maj.record.Rational value from the generic interface to this implementation of the interface.
 Rational clone()
          Creates a cloned deep copy of this embeddable value.
 int compareTo(Rational o)
          Numerically compare this rational with the given rational value.
 int compareToNumber(Number n)
          Numerically compare this value with another number.
 double doubleValue()
          Returns the rational calculated as double value.
 boolean equals(Object o)
          Tests to see if this rational value is equal to the given value.
 float floatValue()
          Returns the rational calculated as float value.
 int getDenominator()
          Returns the denominator of the value represented by this rational number.
 int getNumerator()
          Returns the numerator of the value represented by this rational number.
 int hashCode()
          Returns a hash code value for this embeddable object.
 int intValue()
          Calculates the integer value of the numerator divided by the denominator.
 long longValue()
          Calculates the long value corresponding to the numerator devided by the denominator.
static Rational parseRational(String rationalAsString)
          Convert a string representation of a rational value into a value of this type.
 void setDenominator(int denominator)
          Sets the denominator of the value represented by this rational number.
 void setNumerator(int numerator)
          Sets the numerator of the value represented by this rational number.
 void setPropertiesFromInterface(Rational castFrom)
          Set the properties of this value from those available in the given value, which is specified by the rational value interface.
 void setRational(int numerator, int denominator)
          Set the value of this rational number with both its numerator and denominator.
 String toString()
          Returns a string representation of this rational value as the numerator, followed by a slash (/), followed by the denominator.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rational

public Rational(@Int32
                int numerator,
                @Int32
                int denominator)

Create a rational number from its numerator and denominator.

Parameters:
numerator -
denominator -

Rational

public Rational()

Create a rational number with a numerator of 0 and a denominator of 1.

Method Detail

parseRational

public static final Rational parseRational(String rationalAsString)
                                    throws NullPointerException,
                                           NumberFormatException

Convert a string representation of a rational value into a value of this type. The string representation is expected to take the form of the numerator, followed by a slash character ('/'), followed by the denominator, e.g. "-123/4567".

Parameters:
rationalAsString - String to use to create a rational value.
Returns:
Corresponding rational value.
Throws:
NullPointerException - The rational as string argument is null.
NumberFormatException - The string is not formatted as a rational number representation.
See Also:
toString()

castFromInterface

public static final Rational castFromInterface(Rational alien)
                                        throws NullPointerException

Cast a tv.amwa.maj.record.Rational value from the generic interface to this implementation of the interface. If the given value is not this MAJ API implementation, a copy will be instantiated of this type and with an equivalent value.

Parameters:
alien - A potentially alien implementation of an instance of the Rational interface.
Returns:
Instance of this class that is equal to the given value.
Throws:
NullPointerException - The given alien rational value is null.
See Also:
setPropertiesFromInterface(tv.amwa.maj.record.Rational)

setPropertiesFromInterface

public final void setPropertiesFromInterface(Rational castFrom)
                                      throws NullPointerException

Set the properties of this value from those available in the given value, which is specified by the rational value interface.

Parameters:
castFrom - Value to extract properties from.
Throws:
NullPointerException - The given value to use to set this value is null.
See Also:
castFromInterface(tv.amwa.maj.record.Rational)

getDenominator

@Int32
public final int getDenominator()
Description copied from interface: Rational

Returns the denominator of the value represented by this rational number.

Specified by:
getDenominator in interface Rational
Returns:
Denominator part of the rational value.

setDenominator

public final void setDenominator(@Int32
                                 int denominator)
Description copied from interface: Rational

Sets the denominator of the value represented by this rational number.

Specified by:
setDenominator in interface Rational
Parameters:
denominator - Denominator part of the rational value.

getNumerator

@Int32
public final int getNumerator()
Description copied from interface: Rational

Returns the numerator of the value represented by this rational number.

Specified by:
getNumerator in interface Rational
Returns:
Numerator part of the rational value.

setNumerator

public final void setNumerator(@Int32
                               int numerator)
Description copied from interface: Rational

Sets the numerator of the value represented by this rational number.

Specified by:
setNumerator in interface Rational
Parameters:
numerator - Numerator part of the rational value.

setRational

public final void setRational(@Int32
                              int numerator,
                              @Int32
                              int denominator)
Description copied from interface: Rational

Set the value of this rational number with both its numerator and denominator.

Specified by:
setRational in interface Rational
Parameters:
numerator - Numerator part of the rational value.
denominator - Denominator part of the rational value.

hashCode

public final int hashCode()
Description copied from interface: MAJCommon

Returns a hash code value for this embeddable object.

Specified by:
hashCode in interface MAJCommon
Overrides:
hashCode in class Object
Returns:
Hash code value for this embeddable object.
See Also:
Object.hashCode()

equals

public final boolean equals(Object o)

Tests to see if this rational value is equal to the given value. Two values are equals if and only if they are both rational values with the same numerator and denominator. For a numerical comparison, use compareTo(tv.amwa.maj.record.Rational).

Specified by:
equals in interface MAJCommon
Overrides:
equals in class Object
Parameters:
o - Object to test for equality with.
Returns:
Is the given value equal to this one?
See Also:
Object.equals(java.lang.Object)

toString

public final String toString()

Returns a string representation of this rational value as the numerator, followed by a slash (/), followed by the denominator. For example, "1/4". The format is the same as that parsed by parseRational(String).

Specified by:
toString in interface MAJCommon
Overrides:
toString in class Object
Returns:
This rational value formatted as a string.
See Also:
parseRational(String)

doubleValue

public final double doubleValue()

Returns the rational calculated as double value. In the event of a division by zero, a value of Double.POSITIVE_INFINITY is returned.

Specified by:
doubleValue in class Number
See Also:
Number.doubleValue()

floatValue

public final float floatValue()

Returns the rational calculated as float value. In the event of a division by zero, a value of Float.POSITIVE_INFINITY is returned.

Specified by:
floatValue in class Number
See Also:
Number.floatValue()

intValue

public final int intValue()
                   throws ArithmeticException

Calculates the integer value of the numerator divided by the denominator. If the magnitude of the numerator is less than the denominator, a value of 0 is returned.

Specified by:
intValue in class Number
Throws:
ArithmeticException - The denominator is zero.
See Also:
Number.intValue()

longValue

public final long longValue()
                     throws ArithmeticException

Calculates the long value corresponding to the numerator devided by the denominator. If the magnitude of the numerator is less than the denominator, a value of 0 is returned.

Specified by:
longValue in class Number
Throws:
ArithmeticException - The denominator is 0.
See Also:
Number.longValue()

compareTo

public final int compareTo(Rational o)
                    throws NullPointerException

Numerically compare this rational with the given rational value.

Specified by:
compareTo in interface Comparable<Rational>
Parameters:
o - Value to compare with this one with.
Returns:
A value of -1 if this value is less than the one given, 1 if this value is greater than the one given and 0 if they are equal.
Throws:
NullPointerException - Argument is null.
See Also:
Comparable.compareTo(Object)

compareToNumber

public final int compareToNumber(Number n)
                          throws NullPointerException

Numerically compare this value with another number.

Parameters:
n - Number to compare with this one.
Returns:
A value of -1 if this value is less than the one given, 1 if this value is greater than the one given and 0 if they are equal.
Throws:
NullPointerException - Argument is null.

clone

public final Rational clone()
Description copied from interface: MAJCommon

Creates a cloned deep copy of this embeddable value. The clone will be initialized to have the same value as this value but exists independently from then on. Changes to this value will not effect the clone and changes to the clone will not effect this. MAJ API clones are true deep clones with no shared references to areas of memory within the original.

This method does not throw a CloneNotSupportedException as all embeddable values must implement the Cloneable interface.

Specified by:
clone in interface MAJCommon
Overrides:
clone in class Object
Returns:
Independent clone of this value, which is a deep copy.
See Also:
Object.clone()

appendXMLChildren

public void appendXMLChildren(Node parent)
Description copied from interface: XMLSerializable

Append child elements to the given parent node to serialize the value of an object to an XML fragment. Methods of the XMLBuilder class are provided to help with this process.

Specified by:
appendXMLChildren in interface XMLSerializable
Parameters:
parent - XML parent element to append child nodes to.

Media Authoring
with Java API

(c) 2007-2008 Richard Cartwright, all rights reserved. Subject to the terms of the AAF SDK Public Source License.