MutableInt.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.  *      https://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. import java.util.concurrent.atomic.AtomicInteger;

  19. import org.apache.commons.lang3.math.NumberUtils;

  20. /**
  21.  * A mutable {@code int} wrapper.
  22.  * <p>
  23.  * This class was created before the introduction of {@link AtomicInteger}.
  24.  * </p>
  25.  * <p>
  26.  * Note that as MutableInt does not extend {@link Integer}, it is not treated by {@link String#format(String, Object...)} as an Integer parameter.
  27.  * </p>
  28.  *
  29.  * @see Integer
  30.  * @see AtomicInteger
  31.  * @since 2.1
  32.  */
  33. public class MutableInt extends Number implements Comparable<MutableInt>, Mutable<Number> {

  34.     /**
  35.      * Required for serialization support.
  36.      *
  37.      * @see java.io.Serializable
  38.      */
  39.     private static final long serialVersionUID = 512176391864L;

  40.     /** The mutable value. */
  41.     private int value;

  42.     /**
  43.      * Constructs a new MutableInt with the default value of zero.
  44.      */
  45.     public MutableInt() {
  46.     }

  47.     /**
  48.      * Constructs a new MutableInt with the specified value.
  49.      *
  50.      * @param value  the initial value to store
  51.      */
  52.     public MutableInt(final int value) {
  53.         this.value = value;
  54.     }

  55.     /**
  56.      * Constructs a new MutableInt with the specified value.
  57.      *
  58.      * @param value  the initial value to store, not null
  59.      * @throws NullPointerException if the object is null
  60.      */
  61.     public MutableInt(final Number value) {
  62.         this.value = value.intValue();
  63.     }

  64.     /**
  65.      * Constructs a new MutableInt parsing the given string.
  66.      *
  67.      * @param value  the string to parse, not null
  68.      * @throws NumberFormatException if the string cannot be parsed into an int, see {@link Integer#parseInt(String)}.
  69.      * @since 2.5
  70.      */
  71.     public MutableInt(final String value) {
  72.         this.value = Integer.parseInt(value);
  73.     }

  74.     /**
  75.      * Adds a value to the value of this instance.
  76.      *
  77.      * @param operand  the value to add, not null
  78.      * @since 2.2
  79.      */
  80.     public void add(final int operand) {
  81.         this.value += operand;
  82.     }

  83.     /**
  84.      * Adds a value to the value of this instance.
  85.      *
  86.      * @param operand  the value to add, not null
  87.      * @throws NullPointerException if the object is null
  88.      * @since 2.2
  89.      */
  90.     public void add(final Number operand) {
  91.         this.value += operand.intValue();
  92.     }

  93.     /**
  94.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  95.      * immediately after the addition operation. This method is not thread safe.
  96.      *
  97.      * @param operand the quantity to add, not null
  98.      * @return the value associated with this instance after adding the operand
  99.      * @since 3.5
  100.      */
  101.     public int addAndGet(final int operand) {
  102.         this.value += operand;
  103.         return value;
  104.     }

  105.     /**
  106.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  107.      * immediately after the addition operation. This method is not thread safe.
  108.      *
  109.      * @param operand the quantity to add, not null
  110.      * @throws NullPointerException if {@code operand} is null
  111.      * @return the value associated with this instance after adding the operand
  112.      * @since 3.5
  113.      */
  114.     public int addAndGet(final Number operand) {
  115.         this.value += operand.intValue();
  116.         return value;
  117.     }

  118.     /**
  119.      * Compares this mutable to another in ascending order.
  120.      *
  121.      * @param other  the other mutable to compare to, not null
  122.      * @return negative if this is less, zero if equal, positive if greater
  123.      */
  124.     @Override
  125.     public int compareTo(final MutableInt other) {
  126.         return NumberUtils.compare(this.value, other.value);
  127.     }

  128.     /**
  129.      * Decrements the value.
  130.      *
  131.      * @since 2.2
  132.      */
  133.     public void decrement() {
  134.         value--;
  135.     }

  136.     /**
  137.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  138.      * immediately after the decrement operation. This method is not thread safe.
  139.      *
  140.      * @return the value associated with the instance after it is decremented
  141.      * @since 3.5
  142.      */
  143.     public int decrementAndGet() {
  144.         value--;
  145.         return value;
  146.     }

  147.     /**
  148.      * Returns the value of this MutableInt as a double.
  149.      *
  150.      * @return the numeric value represented by this object after conversion to type double.
  151.      */
  152.     @Override
  153.     public double doubleValue() {
  154.         return value;
  155.     }

  156.     /**
  157.      * Compares this object to the specified object. The result is {@code true} if and only if the argument is
  158.      * not {@code null} and is a {@link MutableInt} object that contains the same {@code int} value
  159.      * as this object.
  160.      *
  161.      * @param obj  the object to compare with, null returns false
  162.      * @return {@code true} if the objects are the same; {@code false} otherwise.
  163.      */
  164.     @Override
  165.     public boolean equals(final Object obj) {
  166.         if (obj instanceof MutableInt) {
  167.             return value == ((MutableInt) obj).intValue();
  168.         }
  169.         return false;
  170.     }

  171.     /**
  172.      * Returns the value of this MutableInt as a float.
  173.      *
  174.      * @return the numeric value represented by this object after conversion to type float.
  175.      */
  176.     @Override
  177.     public float floatValue() {
  178.         return value;
  179.     }

  180.     /**
  181.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  182.      * immediately prior to the addition operation. This method is not thread safe.
  183.      *
  184.      * @param operand the quantity to add, not null
  185.      * @return the value associated with this instance immediately before the operand was added
  186.      * @since 3.5
  187.      */
  188.     public int getAndAdd(final int operand) {
  189.         final int last = value;
  190.         this.value += operand;
  191.         return last;
  192.     }

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

  207.     /**
  208.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  209.      * immediately prior to the decrement operation. This method is not thread safe.
  210.      *
  211.      * @return the value associated with the instance before it was decremented
  212.      * @since 3.5
  213.      */
  214.     public int getAndDecrement() {
  215.         final int last = value;
  216.         value--;
  217.         return last;
  218.     }

  219.     /**
  220.      * Increments this instance's value by 1; this method returns the value associated with the instance
  221.      * immediately prior to the increment operation. This method is not thread safe.
  222.      *
  223.      * @return the value associated with the instance before it was incremented
  224.      * @since 3.5
  225.      */
  226.     public int getAndIncrement() {
  227.         final int last = value;
  228.         value++;
  229.         return last;
  230.     }

  231.     /**
  232.      * Gets the value as a Integer instance.
  233.      *
  234.      * @return the value as a Integer, never null.
  235.      * @deprecated Use {@link #get()}.
  236.      */
  237.     @Deprecated
  238.     @Override
  239.     public Integer getValue() {
  240.         return Integer.valueOf(this.value);
  241.     }

  242.     /**
  243.      * Returns a suitable hash code for this mutable.
  244.      *
  245.      * @return a suitable hash code
  246.      */
  247.     @Override
  248.     public int hashCode() {
  249.         return value;
  250.     }

  251.     /**
  252.      * Increments the value.
  253.      *
  254.      * @since 2.2
  255.      */
  256.     public void increment() {
  257.         value++;
  258.     }

  259.     /**
  260.      * Increments this instance's value by 1; this method returns the value associated with the instance
  261.      * immediately after the increment operation. This method is not thread safe.
  262.      *
  263.      * @return the value associated with the instance after it is incremented
  264.      * @since 3.5
  265.      */
  266.     public int incrementAndGet() {
  267.         value++;
  268.         return value;
  269.     }

  270.     // shortValue and byteValue rely on Number implementation
  271.     /**
  272.      * Returns the value of this MutableInt as an int.
  273.      *
  274.      * @return the numeric value represented by this object after conversion to type int.
  275.      */
  276.     @Override
  277.     public int intValue() {
  278.         return value;
  279.     }

  280.     /**
  281.      * Returns the value of this MutableInt as a long.
  282.      *
  283.      * @return the numeric value represented by this object after conversion to type long.
  284.      */
  285.     @Override
  286.     public long longValue() {
  287.         return value;
  288.     }

  289.     /**
  290.      * Sets the value.
  291.      *
  292.      * @param value  the value to set
  293.      */
  294.     public void setValue(final int value) {
  295.         this.value = value;
  296.     }

  297.     /**
  298.      * Sets the value from any Number instance.
  299.      *
  300.      * @param value  the value to set, not null
  301.      * @throws NullPointerException if the object is null
  302.      */
  303.     @Override
  304.     public void setValue(final Number value) {
  305.         this.value = value.intValue();
  306.     }

  307.     /**
  308.      * Subtracts a value from the value of this instance.
  309.      *
  310.      * @param operand  the value to subtract, not null
  311.      * @since 2.2
  312.      */
  313.     public void subtract(final int operand) {
  314.         this.value -= operand;
  315.     }

  316.     /**
  317.      * Subtracts a value from the value of this instance.
  318.      *
  319.      * @param operand  the value to subtract, not null
  320.      * @throws NullPointerException if the object is null
  321.      * @since 2.2
  322.      */
  323.     public void subtract(final Number operand) {
  324.         this.value -= operand.intValue();
  325.     }

  326.     /**
  327.      * Gets this mutable as an instance of Integer.
  328.      *
  329.      * @return an Integer instance containing the value from this mutable, never null
  330.      */
  331.     public Integer toInteger() {
  332.         return Integer.valueOf(intValue());
  333.     }

  334.     /**
  335.      * Returns the String value of this mutable.
  336.      *
  337.      * @return the mutable value as a string
  338.      */
  339.     @Override
  340.     public String toString() {
  341.         return String.valueOf(value);
  342.     }

  343. }