MutableFloat.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.mutable;

  18. /**
  19.  * A mutable {@code float} wrapper.
  20.  * <p>
  21.  * Note that as MutableFloat does not extend Float, it is not treated by String.format as a Float parameter.
  22.  * </p>
  23.  *
  24.  * @see Float
  25.  * @since 2.1
  26.  */
  27. public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> {

  28.     /**
  29.      * Required for serialization support.
  30.      *
  31.      * @see java.io.Serializable
  32.      */
  33.     private static final long serialVersionUID = 5787169186L;

  34.     /** The mutable value. */
  35.     private float value;

  36.     /**
  37.      * Constructs a new MutableFloat with the default value of zero.
  38.      */
  39.     public MutableFloat() {
  40.     }

  41.     /**
  42.      * Constructs a new MutableFloat with the specified value.
  43.      *
  44.      * @param value  the initial value to store
  45.      */
  46.     public MutableFloat(final float value) {
  47.         this.value = value;
  48.     }

  49.     /**
  50.      * Constructs a new MutableFloat with the specified value.
  51.      *
  52.      * @param value  the initial value to store, not null
  53.      * @throws NullPointerException if the object is null
  54.      */
  55.     public MutableFloat(final Number value) {
  56.         this.value = value.floatValue();
  57.     }

  58.     /**
  59.      * Constructs a new MutableFloat parsing the given string.
  60.      *
  61.      * @param value  the string to parse, not null
  62.      * @throws NumberFormatException if the string cannot be parsed into a float
  63.      * @since 2.5
  64.      */
  65.     public MutableFloat(final String value) {
  66.         this.value = Float.parseFloat(value);
  67.     }

  68.     /**
  69.      * Adds a value to the value of this instance.
  70.      *
  71.      * @param operand  the value to add, not null
  72.      * @since 2.2
  73.      */
  74.     public void add(final float operand) {
  75.         this.value += operand;
  76.     }

  77.     /**
  78.      * Adds a value to the value of this instance.
  79.      *
  80.      * @param operand  the value to add, not null
  81.      * @throws NullPointerException if the object is null
  82.      * @since 2.2
  83.      */
  84.     public void add(final Number operand) {
  85.         this.value += operand.floatValue();
  86.     }

  87.     /**
  88.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  89.      * immediately after the addition operation. This method is not thread safe.
  90.      *
  91.      * @param operand the quantity to add, not null
  92.      * @return the value associated with this instance after adding the operand
  93.      * @since 3.5
  94.      */
  95.     public float addAndGet(final float operand) {
  96.         this.value += operand;
  97.         return value;
  98.     }

  99.     /**
  100.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  101.      * immediately after the addition operation. This method is not thread safe.
  102.      *
  103.      * @param operand the quantity to add, not null
  104.      * @throws NullPointerException if {@code operand} is null
  105.      * @return the value associated with this instance after adding the operand
  106.      * @since 3.5
  107.      */
  108.     public float addAndGet(final Number operand) {
  109.         this.value += operand.floatValue();
  110.         return value;
  111.     }

  112.     /**
  113.      * Compares this mutable to another in ascending order.
  114.      *
  115.      * @param other  the other mutable to compare to, not null
  116.      * @return negative if this is less, zero if equal, positive if greater
  117.      */
  118.     @Override
  119.     public int compareTo(final MutableFloat other) {
  120.         return Float.compare(this.value, other.value);
  121.     }

  122.     /**
  123.      * Decrements the value.
  124.      *
  125.      * @since 2.2
  126.      */
  127.     public void decrement() {
  128.         value--;
  129.     }

  130.     /**
  131.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  132.      * immediately after the decrement operation. This method is not thread safe.
  133.      *
  134.      * @return the value associated with the instance after it is decremented
  135.      * @since 3.5
  136.      */
  137.     public float decrementAndGet() {
  138.         value--;
  139.         return value;
  140.     }

  141.     /**
  142.      * Returns the value of this MutableFloat as a double.
  143.      *
  144.      * @return the numeric value represented by this object after conversion to type double.
  145.      */
  146.     @Override
  147.     public double doubleValue() {
  148.         return value;
  149.     }

  150.     /**
  151.      * Compares this object against some other object. The result is {@code true} if and only if the argument is
  152.      * not {@code null} and is a {@link Float} object that represents a {@code float} that has the
  153.      * identical bit pattern to the bit pattern of the {@code float} represented by this object. For this
  154.      * purpose, two float values are considered to be the same if and only if the method
  155.      * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
  156.      * <p>
  157.      * Note that in most cases, for two instances of class {@link Float},{@code f1} and {@code f2},
  158.      * the value of {@code f1.equals(f2)} is {@code true} if and only if <blockquote>
  159.      *
  160.      * <pre>
  161.      *   f1.floatValue() == f2.floatValue()
  162.      * </pre>
  163.      *
  164.      * </blockquote>
  165.      * <p>
  166.      * also has the value {@code true}. However, there are two exceptions:
  167.      * <ul>
  168.      * <li>If {@code f1} and {@code f2} both represent {@code Float.NaN}, then the
  169.      * {@code equals} method returns {@code true}, even though {@code Float.NaN == Float.NaN} has
  170.      * the value {@code false}.
  171.      * <li>If {@code f1} represents {@code +0.0f} while {@code f2} represents {@code -0.0f},
  172.      * or vice versa, the {@code equal} test has the value {@code false}, even though
  173.      * {@code 0.0f == -0.0f} has the value {@code true}.
  174.      * </ul>
  175.      * This definition allows hashtables to operate properly.
  176.      *
  177.      * @param obj  the object to compare with, null returns false
  178.      * @return {@code true} if the objects are the same; {@code false} otherwise.
  179.      * @see Float#floatToIntBits(float)
  180.      */
  181.     @Override
  182.     public boolean equals(final Object obj) {
  183.         return obj instanceof MutableFloat
  184.             && Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value);
  185.     }

  186.     /**
  187.      * Returns the value of this MutableFloat as a float.
  188.      *
  189.      * @return the numeric value represented by this object after conversion to type float.
  190.      */
  191.     @Override
  192.     public float floatValue() {
  193.         return value;
  194.     }

  195.     /**
  196.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  197.      * immediately prior to the addition operation. This method is not thread safe.
  198.      *
  199.      * @param operand the quantity to add, not null
  200.      * @return the value associated with this instance immediately before the operand was added
  201.      * @since 3.5
  202.      */
  203.     public float getAndAdd(final float operand) {
  204.         final float last = value;
  205.         this.value += operand;
  206.         return last;
  207.     }

  208.     /**
  209.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  210.      * immediately prior to the addition operation. This method is not thread safe.
  211.      *
  212.      * @param operand the quantity to add, not null
  213.      * @throws NullPointerException if {@code operand} is null
  214.      * @return the value associated with this instance immediately before the operand was added
  215.      * @since 3.5
  216.      */
  217.     public float getAndAdd(final Number operand) {
  218.         final float last = value;
  219.         this.value += operand.floatValue();
  220.         return last;
  221.     }

  222.     /**
  223.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  224.      * immediately prior to the decrement operation. This method is not thread safe.
  225.      *
  226.      * @return the value associated with the instance before it was decremented
  227.      * @since 3.5
  228.      */
  229.     public float getAndDecrement() {
  230.         final float last = value;
  231.         value--;
  232.         return last;
  233.     }

  234.     /**
  235.      * Increments this instance's value by 1; this method returns the value associated with the instance
  236.      * immediately prior to the increment operation. This method is not thread safe.
  237.      *
  238.      * @return the value associated with the instance before it was incremented
  239.      * @since 3.5
  240.      */
  241.     public float getAndIncrement() {
  242.         final float last = value;
  243.         value++;
  244.         return last;
  245.     }

  246.     /**
  247.      * Gets the value as a Float instance.
  248.      *
  249.      * @return the value as a Float, never null
  250.      */
  251.     @Override
  252.     public Float getValue() {
  253.         return Float.valueOf(this.value);
  254.     }

  255.     /**
  256.      * Returns a suitable hash code for this mutable.
  257.      *
  258.      * @return a suitable hash code
  259.      */
  260.     @Override
  261.     public int hashCode() {
  262.         return Float.floatToIntBits(value);
  263.     }

  264.     /**
  265.      * Increments the value.
  266.      *
  267.      * @since 2.2
  268.      */
  269.     public void increment() {
  270.         value++;
  271.     }

  272.     /**
  273.      * Increments this instance's value by 1; this method returns the value associated with the instance
  274.      * immediately after the increment operation. This method is not thread safe.
  275.      *
  276.      * @return the value associated with the instance after it is incremented
  277.      * @since 3.5
  278.      */
  279.     public float incrementAndGet() {
  280.         value++;
  281.         return value;
  282.     }

  283.     // shortValue and byteValue rely on Number implementation
  284.     /**
  285.      * Returns the value of this MutableFloat as an int.
  286.      *
  287.      * @return the numeric value represented by this object after conversion to type int.
  288.      */
  289.     @Override
  290.     public int intValue() {
  291.         return (int) value;
  292.     }

  293.     /**
  294.      * Checks whether the float value is infinite.
  295.      *
  296.      * @return true if infinite
  297.      */
  298.     public boolean isInfinite() {
  299.         return Float.isInfinite(value);
  300.     }

  301.     /**
  302.      * Checks whether the float value is the special NaN value.
  303.      *
  304.      * @return true if NaN
  305.      */
  306.     public boolean isNaN() {
  307.         return Float.isNaN(value);
  308.     }

  309.     /**
  310.      * Returns the value of this MutableFloat as a long.
  311.      *
  312.      * @return the numeric value represented by this object after conversion to type long.
  313.      */
  314.     @Override
  315.     public long longValue() {
  316.         return (long) value;
  317.     }

  318.     /**
  319.      * Sets the value.
  320.      *
  321.      * @param value  the value to set
  322.      */
  323.     public void setValue(final float value) {
  324.         this.value = value;
  325.     }

  326.     /**
  327.      * Sets the value from any Number instance.
  328.      *
  329.      * @param value  the value to set, not null
  330.      * @throws NullPointerException if the object is null
  331.      */
  332.     @Override
  333.     public void setValue(final Number value) {
  334.         this.value = value.floatValue();
  335.     }

  336.     /**
  337.      * Subtracts a value from the value of this instance.
  338.      *
  339.      * @param operand  the value to subtract
  340.      * @since 2.2
  341.      */
  342.     public void subtract(final float operand) {
  343.         this.value -= operand;
  344.     }

  345.     /**
  346.      * Subtracts a value from the value of this instance.
  347.      *
  348.      * @param operand  the value to subtract, not null
  349.      * @throws NullPointerException if the object is null
  350.      * @since 2.2
  351.      */
  352.     public void subtract(final Number operand) {
  353.         this.value -= operand.floatValue();
  354.     }

  355.     /**
  356.      * Gets this mutable as an instance of Float.
  357.      *
  358.      * @return a Float instance containing the value from this mutable, never null
  359.      */
  360.     public Float toFloat() {
  361.         return Float.valueOf(floatValue());
  362.     }

  363.     /**
  364.      * Returns the String value of this mutable.
  365.      *
  366.      * @return the mutable value as a string
  367.      */
  368.     @Override
  369.     public String toString() {
  370.         return String.valueOf(value);
  371.     }

  372. }