View Javadoc

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