    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.lang3.mutable;
    018
    019    /**
    020     * A mutable <code>float</code> wrapper.
    021     * <p>
    022     * Note that as MutableFloat does not extend Float, it is not treated by String.format as a Float parameter.
    023     *
    024     * @see Float
    025     * @since 2.1
    026     * @version $Id: MutableFloat.java 1199894 2011-11-09 17:53:59Z ggregory $
    027     */
    028    public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> {
    029
    030        /**
    031         * Required for serialization support.
    032         *
    033         * @see java.io.Serializable
    034         */
    035        private static final long serialVersionUID = 5787169186L;
    036
    037        /** The mutable value. */
    038        private float value;
    039
    040        /**
    041         * Constructs a new MutableFloat with the default value of zero.
    042         */
    043        public MutableFloat() {
    044            super();
    045        }
    046
    047        /**
    048         * Constructs a new MutableFloat with the specified value.
    049         *
    050         * @param value  the initial value to store
    051         */
    052        public MutableFloat(float value) {
    053            super();
    054            this.value = value;
    055        }
    056
    057        /**
    058         * Constructs a new MutableFloat with the specified value.
    059         *
    060         * @param value  the initial value to store, not null
    061         * @throws NullPointerException if the object is null
    062         */
    063        public MutableFloat(Number value) {
    064            super();
    065            this.value = value.floatValue();
    066        }
    067
    068        /**
    069         * Constructs a new MutableFloat parsing the given string.
    070         *
    071         * @param value  the string to parse, not null
    072         * @throws NumberFormatException if the string cannot be parsed into a float
    073         * @since 2.5
    074         */
    075        public MutableFloat(String value) throws NumberFormatException {
    076            super();
    077            this.value = Float.parseFloat(value);
    078        }
    079
    080        //-----------------------------------------------------------------------
    081        /**
    082         * Gets the value as a Float instance.
    083         *
    084         * @return the value as a Float, never null
    085         */
    086        public Float getValue() {
    087            return Float.valueOf(this.value);
    088        }
    089
    090        /**
    091         * Sets the value.
    092         *
    093         * @param value  the value to set
    094         */
    095        public void setValue(float value) {
    096            this.value = value;
    097        }
    098
    099        /**
    100         * Sets the value from any Number instance.
    101         *
    102         * @param value  the value to set, not null
    103         * @throws NullPointerException if the object is null
    104         */
    105        public void setValue(Number value) {
    106            this.value = value.floatValue();
    107        }
    108
    109        //-----------------------------------------------------------------------
    110        /**
    111         * Checks whether the float value is the special NaN value.
    112         *
    113         * @return true if NaN
    114         */
    115        public boolean isNaN() {
    116            return Float.isNaN(value);
    117        }
    118
    119        /**
    120         * Checks whether the float value is infinite.
    121         *
    122         * @return true if infinite
    123         */
    124        public boolean isInfinite() {
    125            return Float.isInfinite(value);
    126        }
    127
    128        //-----------------------------------------------------------------------
    129        /**
    130         * Increments the value.
    131         *
    132         * @since Commons Lang 2.2
    133         */
    134        public void increment() {
    135            value++;
    136        }
    137
    138        /**
    139         * Decrements the value.
    140         *
    141         * @since Commons Lang 2.2
    142         */
    143        public void decrement() {
    144            value--;
    145        }
    146
    147        //-----------------------------------------------------------------------
    148        /**
    149         * Adds a value to the value of this instance.
    150         *
    151         * @param operand  the value to add, not null
    152         * @since Commons Lang 2.2
    153         */
    154        public void add(float operand) {
    155            this.value += operand;
    156        }
    157
    158        /**
    159         * Adds a value to the value of this instance.
    160         *
    161         * @param operand  the value to add, not null
    162         * @throws NullPointerException if the object is null
    163         * @since Commons Lang 2.2
    164         */
    165        public void add(Number operand) {
    166            this.value += operand.floatValue();
    167        }
    168
    169        /**
    170         * Subtracts a value from the value of this instance.
    171         *
    172         * @param operand  the value to subtract
    173         * @since Commons Lang 2.2
    174         */
    175        public void subtract(float operand) {
    176            this.value -= operand;
    177        }
    178
    179        /**
    180         * Subtracts a value from the value of this instance.
    181         *
    182         * @param operand  the value to subtract, not null
    183         * @throws NullPointerException if the object is null
    184         * @since Commons Lang 2.2
    185         */
    186        public void subtract(Number operand) {
    187            this.value -= operand.floatValue();
    188        }
    189
    190        //-----------------------------------------------------------------------
    191        // shortValue and byteValue rely on Number implementation
    192        /**
    193         * Returns the value of this MutableFloat as an int.
    194         *
    195         * @return the numeric value represented by this object after conversion to type int.
    196         */
    197        @Override
    198        public int intValue() {
    199            return (int) value;
    200        }
    201
    202        /**
    203         * Returns the value of this MutableFloat as a long.
    204         *
    205         * @return the numeric value represented by this object after conversion to type long.
    206         */
    207        @Override
    208        public long longValue() {
    209            return (long) value;
    210        }
    211
    212        /**
    213         * Returns the value of this MutableFloat as a float.
    214         *
    215         * @return the numeric value represented by this object after conversion to type float.
    216         */
    217        @Override
    218        public float floatValue() {
    219            return value;
    220        }
    221
    222        /**
    223         * Returns the value of this MutableFloat as a double.
    224         *
    225         * @return the numeric value represented by this object after conversion to type double.
    226         */
    227        @Override
    228        public double doubleValue() {
    229            return value;
    230        }
    231
    232        //-----------------------------------------------------------------------
    233        /**
    234         * Gets this mutable as an instance of Float.
    235         *
    236         * @return a Float instance containing the value from this mutable, never null
    237         */
    238        public Float toFloat() {
    239            return Float.valueOf(floatValue());
    240        }
    241
    242        //-----------------------------------------------------------------------
    243        /**
    244         * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
    245         * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
    246         * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
    247         * purpose, two float values are considered to be the same if and only if the method
    248         * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
    249         * <p>
    250         * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
    251         * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
    252         *
    253         * <pre>
    254         *   f1.floatValue() == f2.floatValue()
    255         * </pre>
    256         *
    257         * </blockquote>
    258         * <p>
    259         * also has the value <code>true</code>. However, there are two exceptions:
    260         * <ul>
    261         * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
    262         * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
    263         * the value <code>false</code>.
    264         * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
    265         * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
    266         * <code>0.0f==-0.0f</code> has the value <code>true</code>.
    267         * </ul>
    268         * This definition allows hashtables to operate properly.
    269         *
    270         * @param obj  the object to compare with, null returns false
    271         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
    272         * @see java.lang.Float#floatToIntBits(float)
    273         */
    274        @Override
    275        public boolean equals(Object obj) {
    276            return obj instanceof MutableFloat
    277                && Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value);
    278        }
    279
    280        /**
    281         * Returns a suitable hash code for this mutable.
    282         *
    283         * @return a suitable hash code
    284         */
    285        @Override
    286        public int hashCode() {
    287            return Float.floatToIntBits(value);
    288        }
    289
    290        //-----------------------------------------------------------------------
    291        /**
    292         * Compares this mutable to another in ascending order.
    293         *
    294         * @param other  the other mutable to compare to, not null
    295         * @return negative if this is less, zero if equal, positive if greater
    296         */
    297        public int compareTo(MutableFloat other) {
    298            float anotherVal = other.value;
    299            return Float.compare(value, anotherVal);
    300        }
    301
    302        //-----------------------------------------------------------------------
    303        /**
    304         * Returns the String value of this mutable.
    305         *
    306         * @return the mutable value as a string
    307         */
    308        @Override
    309        public String toString() {
    310            return String.valueOf(value);
    311        }
    312
    313    }