wyvern.lib.properties
Class Weight

java.lang.Object
  extended bywyvern.lib.properties.Weight
All Implemented Interfaces:
SelfExternalizable

public class Weight
extends java.lang.Object
implements SelfExternalizable

All game objects have a weight that's used for various things. To make display and conversions simpler, we encapsulate weight in a Bean property that provides various utilities, getters and setters for dealing with different units.

Version:
1.0, Jun 12, 1998
Author:
Steve Yegge

Field Summary
static double GM_PER_LB
           
static double GM_PER_OZ
           
static int GRAMS
           
static double KG_PER_LB
           
static double KG_PER_OZ
           
static int KILOS
           
static double LB_PER_GM
           
static double LB_PER_KG
           
static double LB_PER_OZ
           
static int OUNCES
           
static double OZ_PER_GM
           
static double OZ_PER_KG
           
static double OZ_PER_LB
           
static int POUNDS
           
 
Constructor Summary
Weight()
          Constructs a new Weight with a value of 0.
Weight(long grams)
          Constructs a new Weight with the specified value in grams.
Weight(java.lang.String value)
          Constructs a new Weight with the specified value (as a string).
Weight(Weight w)
          Constructs a new Weight copied from the passed Weight.
 
Method Summary
 boolean equals(java.lang.Object obj)
          Returns true if the passed weight equals this weight.
static java.lang.String formatAsGrams(long grams)
          Formats the specified gram-weight.
static java.lang.String formatAsKilos(long grams)
          Formats the specified gram-weight as a number of kilograms (kg).
static java.lang.String formatAsOunces(long grams)
          Formats the specified gram-weight as a number of ounces.
static java.lang.String formatAsPounds(long grams)
          Formats the specified gram-weight as a number of pounds.
 long getValue()
          Returns the value of this weight in grams, the default weight-measurement unit in the game.
 long getWeightInGrams()
          Returns the weight in grams; same as getValue().
 double getWeightInKilos()
          Returns the value of the weight in kg.
 double getWeightInOunces()
          Returns the weight of the object, in ounces.
 double getWeightInPounds()
          Returns the value of this Weight in lbs.
static double gramsToKilos(long grams)
          Converts the specified gram-value to kilograms.
static double gramsToOunces(long grams)
          Converts the specified gram-value to ounces.
static double gramsToPounds(long grams)
          Converts the specified gram-value to pounds.
 int hashCode()
          Overridden hashcode method, required since we overrode equals().
static long kilosToGrams(double kg)
          Rounds the passed kg weight to a whole number of grams.
static double kilosToOunces(double kg)
          Converts the passed kg weight to ounces.
static double kilosToPounds(double kg)
          Converts the passed kg to pounds.
static void main(java.lang.String[] args)
          Tests the conversion and formatting utilities.
static long ouncesToGrams(double oz)
          Rounds the specified oz-value to the nearest whole number of grams.
static double ouncesToKilos(double oz)
          Converts the passed ounce-weight to kilograms.
static double ouncesToPounds(double oz)
          Converts the passed ounce-weight to pounds.
static long parseWeight(java.lang.String args)
          Takes the passed weight-specifier and figures out what it is in grams.
static long poundsToGrams(double lbs)
          Rounds the specified lb-value to the nearest whole number of grams.
static double poundsToKilos(double lbs)
          Converts the passed pound-weight to kilograms.
static double poundsToOunces(double lbs)
          Converts a pound-weight to ounces.
 void readData(org.w3c.dom.Node bean)
          Tells the object to parse its externalized representation from a list of lines.
 void setValue(long gram_weight)
          Sets the value of the weight in grams.
 void setValue(java.lang.String weight)
          Sets the value from the passed weight-specifier string.
 void setWeightInGrams(long gram_weight)
          Sets the object's weight in grams; same as setValue().
 void setWeightInKilos(double kg)
          Sets the object's weight by rounding the passed value to the nearest gram.
 void setWeightInOunces(double oz)
          Sets the object's weight by rounding the passed value to the nearest gram.
 void setWeightInPounds(double lbs)
          Sets the object's weight by rounding the passed value to the nearest gram.
 boolean shouldWrite()
          Returns true if we should write the property at all.
 java.lang.String toString()
          Returns a String representation of this Weight object.
 java.lang.String toString(int units)
          Prints out the weight in the specified units (e.g.
 java.util.List writeData()
          Tells the object to write its properties out to a text file.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

LB_PER_KG

public static final double LB_PER_KG
See Also:
Constant Field Values

KG_PER_LB

public static final double KG_PER_LB
See Also:
Constant Field Values

OZ_PER_LB

public static final double OZ_PER_LB
See Also:
Constant Field Values

LB_PER_OZ

public static final double LB_PER_OZ
See Also:
Constant Field Values

GM_PER_LB

public static final double GM_PER_LB
See Also:
Constant Field Values

GM_PER_OZ

public static final double GM_PER_OZ
See Also:
Constant Field Values

OZ_PER_GM

public static final double OZ_PER_GM
See Also:
Constant Field Values

OZ_PER_KG

public static final double OZ_PER_KG
See Also:
Constant Field Values

KG_PER_OZ

public static final double KG_PER_OZ
See Also:
Constant Field Values

LB_PER_GM

public static final double LB_PER_GM
See Also:
Constant Field Values

KILOS

public static final int KILOS
See Also:
Constant Field Values

GRAMS

public static final int GRAMS
See Also:
Constant Field Values

OUNCES

public static final int OUNCES
See Also:
Constant Field Values

POUNDS

public static final int POUNDS
See Also:
Constant Field Values
Constructor Detail

Weight

public Weight()
Constructs a new Weight with a value of 0.


Weight

public Weight(long grams)
Constructs a new Weight with the specified value in grams.

Parameters:
grams - the weight

Weight

public Weight(java.lang.String value)
Constructs a new Weight with the specified value (as a string).

Parameters:
value - a specifier string consisting of a long or double number, optional whitespace, and one of four units: "kg", "gm", "lb" or "oz".
Throws:
java.lang.IllegalArgumentException - if the value fails to parse

Weight

public Weight(Weight w)
Constructs a new Weight copied from the passed Weight.

Parameters:
w - another Weight object whose value we wish to copy
Method Detail

getValue

public long getValue()
Returns the value of this weight in grams, the default weight-measurement unit in the game.

Returns:
the mass in grams

setValue

public void setValue(long gram_weight)
Sets the value of the weight in grams.

Parameters:
gram_weight - the object's mass in grams

setValue

public void setValue(java.lang.String weight)
Sets the value from the passed weight-specifier string.

Parameters:
weight - a specifier string consisting of a long or double number, optional whitespace, and one of four units: "kg", "gm", "lb" or "oz".
Throws:
java.lang.IllegalArgumentException - if the value fails to parse

getWeightInPounds

public double getWeightInPounds()
Returns the value of this Weight in lbs.

Returns:
the object's weight, converted to pounds

setWeightInPounds

public void setWeightInPounds(double lbs)
Sets the object's weight by rounding the passed value to the nearest gram.

Parameters:
lbs - the object's new weight, in pounds

getWeightInKilos

public double getWeightInKilos()
Returns the value of the weight in kg.

Returns:
the object's weight in kg

setWeightInKilos

public void setWeightInKilos(double kg)
Sets the object's weight by rounding the passed value to the nearest gram.

Parameters:
kg - the object's new weight, in kg

getWeightInOunces

public double getWeightInOunces()
Returns the weight of the object, in ounces.

Returns:
the object's weight, converted to ounces

setWeightInOunces

public void setWeightInOunces(double oz)
Sets the object's weight by rounding the passed value to the nearest gram.

Parameters:
oz - the object's new weight, specified in ounces

getWeightInGrams

public long getWeightInGrams()
Returns the weight in grams; same as getValue().

Returns:
the object's mass in grams

setWeightInGrams

public void setWeightInGrams(long gram_weight)
Sets the object's weight in grams; same as setValue().

Parameters:
gram_weight - the object's new mass in grams

gramsToOunces

public static double gramsToOunces(long grams)
Converts the specified gram-value to ounces.


gramsToPounds

public static double gramsToPounds(long grams)
Converts the specified gram-value to pounds.


gramsToKilos

public static double gramsToKilos(long grams)
Converts the specified gram-value to kilograms.


ouncesToGrams

public static long ouncesToGrams(double oz)
Rounds the specified oz-value to the nearest whole number of grams.


ouncesToPounds

public static double ouncesToPounds(double oz)
Converts the passed ounce-weight to pounds.


ouncesToKilos

public static double ouncesToKilos(double oz)
Converts the passed ounce-weight to kilograms.


poundsToGrams

public static long poundsToGrams(double lbs)
Rounds the specified lb-value to the nearest whole number of grams.


poundsToKilos

public static double poundsToKilos(double lbs)
Converts the passed pound-weight to kilograms.


poundsToOunces

public static double poundsToOunces(double lbs)
Converts a pound-weight to ounces.


kilosToPounds

public static double kilosToPounds(double kg)
Converts the passed kg to pounds.


kilosToOunces

public static double kilosToOunces(double kg)
Converts the passed kg weight to ounces.


kilosToGrams

public static long kilosToGrams(double kg)
Rounds the passed kg weight to a whole number of grams.


formatAsPounds

public static java.lang.String formatAsPounds(long grams)
Formats the specified gram-weight as a number of pounds.


formatAsGrams

public static java.lang.String formatAsGrams(long grams)
Formats the specified gram-weight.


formatAsOunces

public static java.lang.String formatAsOunces(long grams)
Formats the specified gram-weight as a number of ounces.


formatAsKilos

public static java.lang.String formatAsKilos(long grams)
Formats the specified gram-weight as a number of kilograms (kg).


parseWeight

public static long parseWeight(java.lang.String args)
Takes the passed weight-specifier and figures out what it is in grams.

The specifier format is . can be a long or double value. can be one of: "kg" (kilograms), "lb" (pounds), "oz" (ounces), "gm" (grams), or it can be unspecified, which defaults to kilograms. Whitespace between the value and unit is optional.

Parameters:
args - the weight specifier, such as "40.2 lb"
Returns:
the value of the weight, converted to the nearest whole number of grams.
Throws:
java.lang.IllegalArgumentException - if the weight specifier is invalid.

toString

public java.lang.String toString()
Returns a String representation of this Weight object.


toString

public java.lang.String toString(int units)
Prints out the weight in the specified units (e.g. Weight.POUNDS);


readData

public void readData(org.w3c.dom.Node bean)
              throws java.lang.Exception
Tells the object to parse its externalized representation from a list of lines.

Specified by:
readData in interface SelfExternalizable
Parameters:
bean - the node representing the SelfExternalizable bean object, which should have at least one child data node.
Throws:
java.lang.Exception

writeData

public java.util.List writeData()
                         throws java.lang.Exception
Tells the object to write its properties out to a text file. The object does not need to write the block braces; they are written to the stream by the system.

Specified by:
writeData in interface SelfExternalizable
Returns:
a String representation that can be written to the text file.
Throws:
java.lang.Exception

shouldWrite

public boolean shouldWrite()
Returns true if we should write the property at all.

Specified by:
shouldWrite in interface SelfExternalizable
Returns:
true to write the object, false to skip it

hashCode

public int hashCode()
Overridden hashcode method, required since we overrode equals().


equals

public boolean equals(java.lang.Object obj)
Returns true if the passed weight equals this weight.

Parameters:
obj - the object to compare for equality
Returns:
true if our value equals the passed weight's value

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Tests the conversion and formatting utilities. With a command-line argument, attempts to perform the requested conversion for you. This is useful during development, when you know how much a thing weighs in pounds, but want to specify it in grams for efficiency.

Throws:
java.lang.Exception