001    package org.nakedobjects.applib.value;
002    
003    import org.nakedobjects.applib.annotation.Value;
004    
005    
006    @Value(semanticsProviderName = "org.nakedobjects.metamodel.value.PercentageValueSemanticsProvider")
007    public class Percentage extends Magnitude {
008        private static final long serialVersionUID = 1L;
009        private final float value;
010    
011        public Percentage(final float value) {
012            this.value = value;
013        }
014    
015        public Percentage add(final float value) {
016            return new Percentage((floatValue() + value));
017        }
018    
019        public Percentage add(final Percentage value) {
020            return add(value.floatValue());
021        }
022    
023        /**
024         * Returns this value as an double.
025         */
026        public double doubleValue() {
027            return value;
028        }
029    
030        /**
031         * Returns this value as an float.
032         */
033        public float floatValue() {
034            return value;
035        }
036    
037        /**
038         * Returns this value as an int.
039         */
040        public int intValue() {
041            return (int) value;
042        }
043    
044        /**
045         */
046        @Override
047        public boolean isEqualTo(final Magnitude magnitude) {
048            if (magnitude instanceof Percentage) {
049                return ((Percentage) magnitude).value == value;
050            } else {
051                throw new IllegalArgumentException("Parameter must be of type WholeNumber");
052            }
053        }
054    
055        @Override
056        public boolean isLessThan(final Magnitude magnitude) {
057            if (magnitude instanceof Percentage) {
058                return value < ((Percentage) magnitude).value;
059            } else {
060                throw new IllegalArgumentException("Parameter must be of type WholeNumber");
061            }
062        }
063    
064        /**
065         * Returns this value as an long.
066         */
067        public long longValue() {
068            return (long) value;
069        }
070    
071        public Percentage multiply(final float value) {
072            return new Percentage((floatValue() * value));
073        }
074    
075        /**
076         * Returns this value as an short.
077         */
078        public short shortValue() {
079            return (short) value;
080        }
081    
082        public Percentage subtract(final float value) {
083            return add(-value);
084        }
085    
086        public Percentage subtract(final Percentage value) {
087            return add(-value.floatValue());
088        }
089    
090        @Override
091        public boolean equals(final Object other) {
092            if (this == other) {
093                return true;
094            }
095            if (other == null) {
096                return false;
097            }
098            return other.getClass() == this.getClass() && equals((Percentage) other);
099        }
100    
101        public boolean equals(final Percentage other) {
102            return value == other.value;
103        }
104    
105        @Override
106        public int hashCode() {
107            // multiply by 100 just in case the percentage is being stored as 0.0 to 1.0
108            return (int) (floatValue() * 100);
109        }
110    
111        @Override
112        public String toString() {
113            return "" + value;
114        }
115    }
116    // Copyright (c) Naked Objects Group Ltd.