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