001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.lang.mutable;
018    
019    /**
020     * A mutable <code>byte</code> wrapper.
021     * 
022     * @see Byte
023     * @since 2.1
024     * @version $Id: MutableByte.java 618693 2008-02-05 16:33:29Z sebb $
025     */
026    public class MutableByte extends Number implements Comparable, Mutable {
027    
028        /**
029         * Required for serialization support.
030         * 
031         * @see java.io.Serializable
032         */
033        private static final long serialVersionUID = -1585823265L;
034    
035        /** The mutable value. */
036        private byte value;
037    
038        /**
039         * Constructs a new MutableByte with the default value of zero.
040         */
041        public MutableByte() {
042            super();
043        }
044    
045        /**
046         * Constructs a new MutableByte with the specified value.
047         * 
048         * @param value
049         *            a value.
050         */
051        public MutableByte(byte value) {
052            super();
053            this.value = value;
054        }
055    
056        /**
057         * Constructs a new MutableByte with the specified value.
058         * 
059         * @param value
060         *            a value.
061         * @throws NullPointerException
062         *             if the object is null
063         */
064        public MutableByte(Number value) {
065            super();
066            this.value = value.byteValue();
067        }
068    
069        //-----------------------------------------------------------------------
070        /**
071         * Gets the value as a Byte instance.
072         * 
073         * @return the value as a Byte
074         */
075        public Object getValue() {
076            return new Byte(this.value);
077        }
078    
079        /**
080         * Sets the value.
081         * 
082         * @param value
083         *            the value to set
084         */
085        public void setValue(byte value) {
086            this.value = value;
087        }
088    
089        /**
090         * Sets the value from any Number instance.
091         * 
092         * @param value
093         *            the value to set
094         * @throws NullPointerException
095         *             if the object is null
096         * @throws ClassCastException
097         *             if the type is not a {@link Number}
098         */
099        public void setValue(Object value) {
100            setValue(((Number) value).byteValue());
101        }
102    
103        //-----------------------------------------------------------------------
104        // shortValue relies on Number implementation
105        /**
106         * Returns the value of this MutableByte as a byte.
107         *
108         * @return the numeric value represented by this object after conversion to type byte.
109         */
110        public byte byteValue() {
111            return value;
112        }
113    
114        /**
115         * Returns the value of this MutableByte as a int.
116         *
117         * @return the numeric value represented by this object after conversion to type int.
118         */
119        public int intValue() {
120            return value;
121        }
122    
123        /**
124         * Returns the value of this MutableByte as a long.
125         *
126         * @return the numeric value represented by this object after conversion to type long.
127         */
128        public long longValue() {
129            return value;
130        }
131    
132        /**
133         * Returns the value of this MutableByte as a float.
134         *
135         * @return the numeric value represented by this object after conversion to type float.
136         */
137        public float floatValue() {
138            return value;
139        }
140    
141        /**
142         * Returns the value of this MutableByte as a double.
143         *
144         * @return the numeric value represented by this object after conversion to type double.
145         */
146        public double doubleValue() {
147            return value;
148        }
149    
150        //-----------------------------------------------------------------------
151        /**
152         * Gets this mutable as an instance of Byte.
153         *
154         * @return a Byte instance containing the value from this mutable
155         */
156        public Byte toByte() {
157            return new Byte(byteValue());
158        }
159    
160        //-----------------------------------------------------------------------
161        /**
162         * Increments the value.
163         *
164         * @since Commons Lang 2.2
165         */
166        public void increment() {
167            value++;
168        }
169    
170        /**
171         * Decrements the value.
172         *
173         * @since Commons Lang 2.2
174         */
175        public void decrement() {
176            value--;
177        }
178    
179        //-----------------------------------------------------------------------
180        /**
181         * Adds a value.
182         * 
183         * @param operand
184         *            the value to add
185         *
186         * @since Commons Lang 2.2
187         */
188        public void add(byte operand) {
189            this.value += operand;
190        }
191    
192        /**
193         * Adds a value.
194         * 
195         * @param operand
196         *            the value to add
197         * @throws NullPointerException
198         *             if the object is null
199         *
200         * @since Commons Lang 2.2
201         */
202        public void add(Number operand) {
203            this.value += operand.byteValue();
204        }
205    
206        /**
207         * Subtracts a value.
208         * 
209         * @param operand
210         *            the value to add
211         *
212         * @since Commons Lang 2.2
213         */
214        public void subtract(byte operand) {
215            this.value -= operand;
216        }
217    
218        /**
219         * Subtracts a value.
220         * 
221         * @param operand
222         *            the value to add
223         * @throws NullPointerException
224         *             if the object is null
225         *
226         * @since Commons Lang 2.2
227         */
228        public void subtract(Number operand) {
229            this.value -= operand.byteValue();
230        }
231    
232        //-----------------------------------------------------------------------
233        /**
234         * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
235         * is not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code>
236         * value as this object.
237         * 
238         * @param obj
239         *            the object to compare with.
240         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
241         */
242        public boolean equals(Object obj) {
243            if (obj instanceof MutableByte) {
244                return value == ((MutableByte) obj).byteValue();
245            }
246            return false;
247        }
248    
249        /**
250         * Returns a suitable hashcode for this mutable.
251         * 
252         * @return a suitable hashcode
253         */
254        public int hashCode() {
255            return value;
256        }
257    
258        /**
259         * Compares this mutable to another in ascending order.
260         * 
261         * @param obj
262         *            the mutable to compare to
263         * @return negative if this is less, zero if equal, positive if greater
264         * @throws ClassCastException if the argument is not a MutableByte
265         */
266        public int compareTo(Object obj) {
267            MutableByte other = (MutableByte) obj;
268            byte anotherVal = other.value;
269            return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
270        }
271    
272        /**
273         * Returns the String value of this mutable.
274         * 
275         * @return the mutable value as a string
276         */
277        public String toString() {
278            return String.valueOf(value);
279        }
280    
281    }