001    // Copyright 2010 Waterken Inc. under the terms of the MIT X license
002    // found at http://www.opensource.org/licenses/mit-license.html
003    package org.ref_send.brand;
004    
005    import java.io.Serializable;
006    
007    import org.joe_e.Powerless;
008    import org.ref_send.Record;
009    import org.ref_send.deserializer;
010    import org.ref_send.name;
011    
012    /**
013     * A typed <code>long</code> value.
014     * @param <T> {@link #unit} type
015     */
016    public class
017    Amount<T> implements Comparable<Amount<T>>, Powerless, Record, Serializable {
018        static private final long serialVersionUID = 1L;
019    
020        /**
021         * unit of {@link #value}
022         */
023        public final Brand<T> unit;
024        
025        /**
026         * number of units
027         */
028        public final long value;
029        
030        /**
031         * Constructs an instance.
032         * @param unit  {@link #unit}
033         * @param value {@link #value}
034         */
035        public @deserializer
036        Amount(@name("unit") final Brand<T> unit,
037               @name("value") final long value) {
038            this.unit = unit;
039            this.value = value;
040        }
041        
042        // java.lang.Object interface
043        
044        public boolean
045        equals(final Object o) {
046            return null != o && Amount.class == o.getClass() &&
047                value == ((Amount<?>)o).value &&
048                Brand.equal(unit, ((Amount<?>)o).unit);
049        }
050        
051        public int
052        hashCode() { return (int)(value ^ (value >>> Integer.SIZE)) + 0x0FAB4A2D; }
053    
054        // java.lang.Comparable interface
055    
056        public int
057        compareTo(final Amount<T> o) throws WrongBrand {
058            Brand.require(unit, o.unit);
059            return value < o.value ? -1 : value == o.value ? 0 : 1;
060        }
061    }