Coverage Report - org.mule.util.concurrent.WaitableBoolean
 
Classes in this File Line Coverage Branch Coverage Complexity
WaitableBoolean
87%
60/69
96%
23/24
1.368
 
 1  
 /*
 2  
  * $Id: WaitableBoolean.java 8077 2007-08-27 20:15:25Z aperepel $
 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  2382
         super();
 22  2382
         synchronized (lock)
 23  
         {
 24  2382
             value = initialValue;
 25  2382
         }
 26  2382
     }
 27  
 
 28  
     public WaitableBoolean(boolean initialValue, Object lock)
 29  
     {
 30  2
         super(lock);
 31  2
         synchronized (this.lock)
 32  
         {
 33  2
             value = initialValue;
 34  2
         }
 35  2
     }
 36  
 
 37  
     public int compareTo(boolean other)
 38  
     {
 39  20
         synchronized (lock)
 40  
         {
 41  20
             return (value == other ? 0 : (value ? 1 : -1));
 42  0
         }
 43  
     }
 44  
 
 45  
     public int compareTo(WaitableBoolean other)
 46  
     {
 47  12
         return this.compareTo(other.get());
 48  
     }
 49  
 
 50  
     public int compareTo(Object other)
 51  
     {
 52  4
         return this.compareTo((WaitableBoolean) other);
 53  
     }
 54  
 
 55  
     public boolean equals(Object other)
 56  
     {
 57  12
         if (other == this)
 58  
         {
 59  2
             return true;
 60  
         }
 61  10
         else if (other instanceof WaitableBoolean)
 62  
         {
 63  8
             synchronized (lock)
 64  
             {
 65  8
                 return (value == ((WaitableBoolean) other).get());
 66  0
             }
 67  
         }
 68  
         else
 69  
         {
 70  2
             return false;
 71  
         }
 72  
     }
 73  
 
 74  
     public int hashCode()
 75  
     {
 76  12
         return (this.get() ? 3412688 : 8319343); // entirely arbitrary
 77  
     }
 78  
 
 79  
     public String toString()
 80  
     {
 81  1514
         return Boolean.toString(this.get());
 82  
     }
 83  
 
 84  
     public boolean get()
 85  
     {
 86  1784
         synchronized (lock)
 87  
         {
 88  1784
             return value;
 89  0
         }
 90  
     }
 91  
 
 92  
     public boolean set(boolean newValue)
 93  
     {
 94  159
         synchronized (lock)
 95  
         {
 96  159
             lock.notifyAll();
 97  159
             boolean oldValue = value;
 98  159
             value = newValue;
 99  159
             return oldValue;
 100  0
         }
 101  
     }
 102  
 
 103  
     public boolean compareAndSet(boolean assumedValue, boolean newValue)
 104  
     {
 105  36
         synchronized (lock)
 106  
         {
 107  36
             boolean success = (value == assumedValue);
 108  
 
 109  36
             if (success)
 110  
             {
 111  28
                 value = newValue;
 112  28
                 lock.notifyAll();
 113  
             }
 114  
 
 115  36
             return success;
 116  0
         }
 117  
     }
 118  
 
 119  
     public boolean complement()
 120  
     {
 121  4
         synchronized (lock)
 122  
         {
 123  4
             lock.notifyAll();
 124  4
             return (value = !value);
 125  0
         }
 126  
     }
 127  
 
 128  
     public boolean and(boolean b)
 129  
     {
 130  12
         synchronized (lock)
 131  
         {
 132  12
             lock.notifyAll();
 133  12
             return (value &= b);
 134  0
         }
 135  
     }
 136  
 
 137  
     public synchronized boolean or(boolean b)
 138  
     {
 139  12
         synchronized (lock)
 140  
         {
 141  12
             lock.notifyAll();
 142  12
             return (value |= b);
 143  0
         }
 144  
     }
 145  
 
 146  
     public boolean xor(boolean b)
 147  
     {
 148  8
         synchronized (lock)
 149  
         {
 150  8
             lock.notifyAll();
 151  8
             return (value ^= b);
 152  0
         }
 153  
     }
 154  
 
 155  
     public void whenTrue(Runnable action) throws InterruptedException
 156  
     {
 157  4
         this.whenEqual(true, action);
 158  4
     }
 159  
 
 160  
     public void whenFalse(Runnable action) throws InterruptedException
 161  
     {
 162  37
         this.whenNotEqual(true, action);
 163  37
     }
 164  
 
 165  
     public void whenEqual(boolean condition, Runnable action) throws InterruptedException
 166  
     {
 167  41
         synchronized (lock)
 168  
         {
 169  45
             while (value != condition)
 170  
             {
 171  4
                 lock.wait();
 172  
             }
 173  
 
 174  41
             if (action != null)
 175  
             {
 176  8
                 this.execute(action);
 177  
             }
 178  41
         }
 179  41
     }
 180  
 
 181  
     public void whenNotEqual(boolean condition, Runnable action) throws InterruptedException
 182  
     {
 183  37
         this.whenEqual(!condition, action);
 184  37
     }
 185  
 
 186  
 }