1 10 11 package org.mule.util.concurrent; 12 13 public class WaitableBoolean extends SynchronizedVariable 15 { 16 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 (_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); } 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 commit(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 } 187 | Popular Tags |