View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util.concurrent;
8   
9   // @ThreadSafe
10  public class WaitableBoolean extends AbstractSynchronizedVariable
11  {
12      // @GuardedBy(lock)
13      private boolean value;
14  
15      public WaitableBoolean(boolean initialValue)
16      {
17          super();
18          synchronized (lock)
19          {
20              value = initialValue;
21          }
22      }
23  
24      public WaitableBoolean(boolean initialValue, Object lock)
25      {
26          super(lock);
27          synchronized (this.lock)
28          {
29              value = initialValue;
30          }
31      }
32  
33      public int compareTo(boolean other)
34      {
35          synchronized (lock)
36          {
37              return (value == other ? 0 : (value ? 1 : -1));
38          }
39      }
40  
41      public int compareTo(WaitableBoolean other)
42      {
43          return this.compareTo(other.get());
44      }
45  
46      public int compareTo(Object other)
47      {
48          return this.compareTo((WaitableBoolean) other);
49      }
50  
51      public boolean equals(Object other)
52      {
53          if (other == this)
54          {
55              return true;
56          }
57          else if (other instanceof WaitableBoolean)
58          {
59              synchronized (lock)
60              {
61                  return (value == ((WaitableBoolean) other).get());
62              }
63          }
64          else
65          {
66              return false;
67          }
68      }
69  
70      public int hashCode()
71      {
72          return (this.get() ? 3412688 : 8319343); // entirely arbitrary
73      }
74  
75      public String toString()
76      {
77          return Boolean.toString(this.get());
78      }
79  
80      public boolean get()
81      {
82          synchronized (lock)
83          {
84              return value;
85          }
86      }
87  
88      public boolean set(boolean newValue)
89      {
90          synchronized (lock)
91          {
92              lock.notifyAll();
93              boolean oldValue = value;
94              value = newValue;
95              return oldValue;
96          }
97      }
98  
99      public boolean compareAndSet(boolean assumedValue, boolean newValue)
100     {
101         synchronized (lock)
102         {
103             boolean success = (value == assumedValue);
104 
105             if (success)
106             {
107                 value = newValue;
108                 lock.notifyAll();
109             }
110 
111             return success;
112         }
113     }
114 
115     public boolean complement()
116     {
117         synchronized (lock)
118         {
119             lock.notifyAll();
120             return (value = !value);
121         }
122     }
123 
124     public boolean and(boolean b)
125     {
126         synchronized (lock)
127         {
128             lock.notifyAll();
129             return (value &= b);
130         }
131     }
132 
133     public synchronized boolean or(boolean b)
134     {
135         synchronized (lock)
136         {
137             lock.notifyAll();
138             return (value |= b);
139         }
140     }
141 
142     public boolean xor(boolean b)
143     {
144         synchronized (lock)
145         {
146             lock.notifyAll();
147             return (value ^= b);
148         }
149     }
150 
151     public void whenTrue(Runnable action) throws InterruptedException
152     {
153         this.whenEqual(true, action);
154     }
155 
156     public void whenFalse(Runnable action) throws InterruptedException
157     {
158         this.whenNotEqual(true, action);
159     }
160 
161     public void whenEqual(boolean condition, Runnable action) throws InterruptedException
162     {
163         synchronized (lock)
164         {
165             while (value != condition)
166             {
167                 lock.wait();
168             }
169 
170             if (action != null)
171             {
172                 this.execute(action);
173             }
174         }
175     }
176 
177     public void whenNotEqual(boolean condition, Runnable action) throws InterruptedException
178     {
179         this.whenEqual(!condition, action);
180     }
181 
182 }