View Javadoc
1   package org.djutils.base;
2   
3   import java.util.Objects;
4   
5   /**
6    * A simple version of a mutable boolean with get() and set(boolean) functions.
7    * <p>
8    * Copyright (c) 2023-2024 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 MutableBoolean
16  {
17      /** the current value of the mutable boolean. */
18      private boolean value;
19  
20      /**
21       * Initialize the mutable boolean with a value.
22       * @param value boolean; the initial value of the mutable boolean
23       */
24      public MutableBoolean(final boolean value)
25      {
26          set(value);
27      }
28  
29      /**
30       * set the mutable boolean to a new value.
31       * @param newValue boolean; the new value of the mutable boolean
32       */
33      public void set(final boolean newValue)
34      {
35          this.value = newValue;
36      }
37  
38      /**
39       * @return boolean; the current value of the mutable boolean
40       */
41      public boolean get()
42      {
43          return this.value;
44      }
45  
46      /**
47       * Flip the value of modifiable boolean.
48       */
49      public void flip()
50      {
51          this.value = !this.value;
52      }
53  
54      @Override
55      public int hashCode()
56      {
57          return Objects.hash(this.value);
58      }
59  
60      @Override
61      @SuppressWarnings("checkstyle:needbraces")
62      public boolean equals(final Object obj)
63      {
64          if (this == obj)
65              return true;
66          if (obj == null)
67              return false;
68          if (getClass() != obj.getClass())
69              return false;
70          MutableBoolean other = (MutableBoolean) obj;
71          return this.value == other.value;
72      }
73  
74      @Override
75      public String toString()
76      {
77          return "MutableBoolean [value=" + this.value + "]";
78      }
79  
80  }