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.  *      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. import org.apache.commons.lang3.math.NumberUtils;

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

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

  35.     /** The mutable value. */
  36.     private int value;

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

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

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

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

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

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

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

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

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

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

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

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

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

  166.     /**
  167.      * Returns the value of this MutableInt as a float.
  168.      *
  169.      * @return the numeric value represented by this object after conversion to type float.
  170.      */
  171.     @Override
  172.     public float floatValue() {
  173.         return value;
  174.     }

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

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

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

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

  226.     /**
  227.      * Gets the value as a Integer instance.
  228.      *
  229.      * @return the value as a Integer, never null
  230.      */
  231.     @Override
  232.     public Integer getValue() {
  233.         return Integer.valueOf(this.value);
  234.     }

  235.     /**
  236.      * Returns a suitable hash code for this mutable.
  237.      *
  238.      * @return a suitable hash code
  239.      */
  240.     @Override
  241.     public int hashCode() {
  242.         return value;
  243.     }

  244.     /**
  245.      * Increments the value.
  246.      *
  247.      * @since 2.2
  248.      */
  249.     public void increment() {
  250.         value++;
  251.     }

  252.     /**
  253.      * Increments this instance's value by 1; this method returns the value associated with the instance
  254.      * immediately after the increment operation. This method is not thread safe.
  255.      *
  256.      * @return the value associated with the instance after it is incremented
  257.      * @since 3.5
  258.      */
  259.     public int incrementAndGet() {
  260.         value++;
  261.         return value;
  262.     }

  263.     // shortValue and byteValue rely on Number implementation
  264.     /**
  265.      * Returns the value of this MutableInt as an int.
  266.      *
  267.      * @return the numeric value represented by this object after conversion to type int.
  268.      */
  269.     @Override
  270.     public int intValue() {
  271.         return value;
  272.     }

  273.     /**
  274.      * Returns the value of this MutableInt as a long.
  275.      *
  276.      * @return the numeric value represented by this object after conversion to type long.
  277.      */
  278.     @Override
  279.     public long longValue() {
  280.         return value;
  281.     }

  282.     /**
  283.      * Sets the value.
  284.      *
  285.      * @param value  the value to set
  286.      */
  287.     public void setValue(final int value) {
  288.         this.value = value;
  289.     }

  290.     /**
  291.      * Sets the value from any Number instance.
  292.      *
  293.      * @param value  the value to set, not null
  294.      * @throws NullPointerException if the object is null
  295.      */
  296.     @Override
  297.     public void setValue(final Number value) {
  298.         this.value = value.intValue();
  299.     }

  300.     /**
  301.      * Subtracts a value from the value of this instance.
  302.      *
  303.      * @param operand  the value to subtract, not null
  304.      * @since 2.2
  305.      */
  306.     public void subtract(final int operand) {
  307.         this.value -= operand;
  308.     }

  309.     /**
  310.      * Subtracts a value from the value of this instance.
  311.      *
  312.      * @param operand  the value to subtract, not null
  313.      * @throws NullPointerException if the object is null
  314.      * @since 2.2
  315.      */
  316.     public void subtract(final Number operand) {
  317.         this.value -= operand.intValue();
  318.     }

  319.     /**
  320.      * Gets this mutable as an instance of Integer.
  321.      *
  322.      * @return an Integer instance containing the value from this mutable, never null
  323.      */
  324.     public Integer toInteger() {
  325.         return Integer.valueOf(intValue());
  326.     }

  327.     /**
  328.      * Returns the String value of this mutable.
  329.      *
  330.      * @return the mutable value as a string
  331.      */
  332.     @Override
  333.     public String toString() {
  334.         return String.valueOf(value);
  335.     }

  336. }