Coverage Report - com.sun.javafx.runtime.location.BooleanVariable
 
Classes in this File Line Coverage Branch Coverage Complexity
BooleanVariable
82%
53/65
83%
30/36
0
BooleanVariable$1
100%
2/2
N/A
0
BooleanVariable$2
100%
3/3
N/A
0
BooleanVariable$3
33%
1/3
N/A
0
 
 1  
 package com.sun.javafx.runtime.location;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 import com.sun.javafx.runtime.AssignToBoundException;
 7  
 import com.sun.javafx.runtime.ErrorHandler;
 8  
 
 9  
 /**
 10  
  * BooleanVariable
 11  
  *
 12  
  * @author Brian Goetz
 13  
  */
 14  1022
 public class BooleanVariable
 15  
         extends AbstractVariable<Boolean, BooleanLocation, BooleanBindingExpression>
 16  
         implements BooleanLocation {
 17  
 
 18  
     public static final boolean DEFAULT = false;
 19  672
     protected boolean $value = DEFAULT;
 20  
     private List<BooleanChangeListener> replaceListeners;
 21  
 
 22  
     public static BooleanVariable make() {
 23  197
         return new BooleanVariable();
 24  
     }
 25  
 
 26  
     public static BooleanVariable make(boolean value) {
 27  15
         return new BooleanVariable(value);
 28  
     }
 29  
 
 30  
     public static BooleanVariable make(boolean lazy, BooleanBindingExpression binding, Location... dependencies) {
 31  6
         return new BooleanVariable(lazy, binding, dependencies);
 32  
     }
 33  
 
 34  
     public static BooleanVariable make(BooleanBindingExpression binding, Location... dependencies) {
 35  447
         return new BooleanVariable(false, binding, dependencies);
 36  
     }
 37  
 
 38  
     /** Create a bijectively bound variable */
 39  
     public static BooleanVariable makeBijective(ObjectVariable<Boolean> other) {
 40  0
         BooleanVariable me = BooleanVariable.make();
 41  0
         me.bijectiveBind(other);
 42  0
         return me;
 43  
     }
 44  
 
 45  1344
     protected BooleanVariable() { }
 46  
 
 47  
     protected BooleanVariable(boolean value) {
 48  15
         this();
 49  15
         $value = value;
 50  15
         setValid();
 51  15
     }
 52  
 
 53  
     protected BooleanVariable(boolean lazy, BooleanBindingExpression binding, Location... dependencies) {
 54  453
         this();
 55  453
         bind(lazy, binding);
 56  453
         addDependencies(dependencies);
 57  453
     }
 58  
 
 59  
     protected boolean replaceValue(boolean newValue) {
 60  1330
         boolean oldValue = $value;
 61  1330
         if (oldValue != newValue || !isInitialized() || !isEverValid()) {
 62  1012
             boolean notifyDependencies = isValid() || !isInitialized() || !isEverValid();
 63  1012
             $value = newValue;
 64  1012
             setValid();
 65  1012
             notifyListeners(oldValue, newValue, notifyDependencies);
 66  1012
         }
 67  
         else
 68  318
             setValid();
 69  1330
         return newValue;
 70  
     }
 71  
 
 72  
     public boolean getAsBoolean() {
 73  2310
         if (isBound() && !isValid())
 74  541
             update();
 75  2310
         return $value;
 76  
     }
 77  
 
 78  
     protected BooleanBindingExpression makeBindingExpression(final BooleanLocation otherLocation) {
 79  15
         return new BooleanBindingExpression() {
 80  
             public boolean computeValue() {
 81  34
                 return otherLocation.getAsBoolean();
 82  
             }
 83  
         };
 84  
     }
 85  
 
 86  
     public boolean setAsBoolean(boolean value) {
 87  294
         if (isBound() && $value != value)
 88  2
             throw new AssignToBoundException("Cannot assign to bound variable");
 89  292
         return replaceValue(value);
 90  
     }
 91  
 
 92  
     public boolean setAsBooleanFromLiteral(final boolean value) {
 93  11
         deferredLiteral = new DeferredInitializer() {
 94  
             public void apply() {
 95  11
                 setAsBoolean(value);
 96  11
             }
 97  
         };
 98  11
         return value;
 99  
     }
 100  
 
 101  
     public void setDefault() {
 102  0
         setAsBoolean(DEFAULT);
 103  0
     }
 104  
 
 105  
     public Boolean set(Boolean value) {
 106  8
         if (value == null) {
 107  0
             ErrorHandler.nullToPrimitiveCoercion("Boolean");
 108  0
             setDefault();
 109  
         }
 110  
         else
 111  8
             setAsBoolean(value);
 112  7
         return value;
 113  
     }
 114  
 
 115  
     @Override
 116  
     public void update() {
 117  
         try {
 118  1587
             if (isBound() && !isValid())
 119  1038
                 replaceValue(binding.computeValue());
 120  
         }
 121  0
         catch (RuntimeException e) {
 122  0
             ErrorHandler.bindException(e);
 123  0
             if (isInitialized())
 124  0
                 replaceValue(DEFAULT);
 125  1587
         }
 126  1587
     }
 127  
 
 128  
     public Boolean get() {
 129  999
         return getAsBoolean();
 130  
     }
 131  
 
 132  
     public boolean isNull() {
 133  0
         return false;
 134  
     }
 135  
 
 136  
     public void addChangeListener(BooleanChangeListener listener) {
 137  67
         if (replaceListeners == null)
 138  67
             replaceListeners = new ArrayList<BooleanChangeListener>();
 139  67
         replaceListeners.add(listener);
 140  67
     }
 141  
 
 142  
     public void addChangeListener(final ObjectChangeListener<Boolean> listener) {
 143  1
         addChangeListener(new BooleanChangeListener() {
 144  
             public void onChange(boolean oldValue, boolean newValue) {
 145  0
                 listener.onChange(oldValue, newValue);
 146  0
             }
 147  
         });
 148  1
     }
 149  
 
 150  
     private void notifyListeners(boolean oldValue, boolean newValue, boolean notifyDependencies) {
 151  1012
         if (notifyDependencies)
 152  720
             invalidateDependencies();
 153  1012
         if (replaceListeners != null) {
 154  81
             for (BooleanChangeListener listener : replaceListeners)
 155  81
                 listener.onChange(oldValue, newValue);
 156  
         }
 157  1012
     }
 158  
 
 159  
 }