View Javadoc
1   package org.djutils.base;
2   
3   import java.util.Objects;
4   
5   /**
6    * A simple version of a mutable float with get() and set(float) functions.
7    * <p>
8    * Copyright (c) 2023-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9    * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
10   * distributed under a three-clause BSD-style license, which can be found at
11   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
12   * </p>
13   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   */
15  public class MutableFloat
16  {
17      /** the current value of the mutable float. */
18      private float value;
19  
20      /**
21       * Initialize the mutable float with a value.
22       * @param value float; the initial value of the mutable float
23       */
24      public MutableFloat(final float value)
25      {
26          set(value);
27      }
28  
29      /**
30       * set the mutable float to a new value.
31       * @param newValue float; the new value of the mutable float
32       */
33      public void set(final float newValue)
34      {
35          this.value = newValue;
36      }
37  
38      /**
39       * @return float; the current value of the mutable float
40       */
41      public float get()
42      {
43          return this.value;
44      }
45  
46      /**
47       * Increment the modifiable float with a value.
48       * @param increment float; the value to increment the modifiable float with
49       */
50      public void inc(final float increment)
51      {
52          this.value += increment;
53      }
54  
55      /**
56       * Increment the modifiable float with 1.
57       */
58      public void inc()
59      {
60          inc(1);
61      }
62  
63      /**
64       * Decrement the modifiable float with a value.
65       * @param decrement float; the value to decrement the modifiable float with
66       */
67      public void dec(final float decrement)
68      {
69          this.value -= decrement;
70      }
71  
72      /**
73       * Decrement the modifiable float with 1.
74       */
75      public void dec()
76      {
77          dec(1);
78      }
79  
80      /**
81       * Increment the modifiable float with a value.
82       * @param multiplier float; the value to multiply the modifiable float with
83       */
84      public void mul(final float multiplier)
85      {
86          this.value *= multiplier;
87      }
88  
89      /**
90       * Divide the modifiable float by a value.
91       * @param divisor float; the value to divide the modifiable float by
92       */
93      public void div(final float divisor)
94      {
95          this.value /= divisor;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public int hashCode()
101     {
102         return Objects.hash(this.value);
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     @SuppressWarnings("checkstyle:needbraces")
108     public boolean equals(final Object obj)
109     {
110         if (this == obj)
111             return true;
112         if (obj == null)
113             return false;
114         if (getClass() != obj.getClass())
115             return false;
116         MutableFloat other = (MutableFloat) obj;
117         return this.value == other.value;
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public String toString()
123     {
124         return "MutableFloat [value=" + this.value + "]";
125     }
126 
127 }