Coverage Report - org.apache.commons.lang3.mutable.MutableBoolean
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableBoolean
96%
27/28
100%
10/10
1,188
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.commons.lang3.mutable;
 19  
 
 20  
 import java.io.Serializable;
 21  
 
 22  
 import org.apache.commons.lang3.BooleanUtils;
 23  
 
 24  
 /**
 25  
  * A mutable <code>boolean</code> wrapper.
 26  
  * <p>
 27  
  * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter. 
 28  
  * 
 29  
  * @see Boolean
 30  
  * @since 2.2
 31  
  */
 32  0
 public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
 33  
 
 34  
     /**
 35  
      * Required for serialization support.
 36  
      * 
 37  
      * @see java.io.Serializable
 38  
      */
 39  
     private static final long serialVersionUID = -4830728138360036487L;
 40  
 
 41  
     /** The mutable value. */
 42  
     private boolean value;
 43  
 
 44  
     /**
 45  
      * Constructs a new MutableBoolean with the default value of false.
 46  
      */
 47  
     public MutableBoolean() {
 48  3
         super();
 49  3
     }
 50  
 
 51  
     /**
 52  
      * Constructs a new MutableBoolean with the specified value.
 53  
      * 
 54  
      * @param value  the initial value to store
 55  
      */
 56  
     public MutableBoolean(final boolean value) {
 57  18
         super();
 58  18
         this.value = value;
 59  18
     }
 60  
 
 61  
     /**
 62  
      * Constructs a new MutableBoolean with the specified value.
 63  
      * 
 64  
      * @param value  the initial value to store, not null
 65  
      * @throws NullPointerException if the object is null
 66  
      */
 67  
     public MutableBoolean(final Boolean value) {
 68  3
         super();
 69  3
         this.value = value.booleanValue();
 70  2
     }
 71  
 
 72  
     //-----------------------------------------------------------------------
 73  
     /**
 74  
      * Gets the value as a Boolean instance.
 75  
      * 
 76  
      * @return the value as a Boolean, never null
 77  
      */
 78  
     @Override
 79  
     public Boolean getValue() {
 80  1
         return Boolean.valueOf(this.value);
 81  
     }
 82  
 
 83  
     /**
 84  
      * Sets the value.
 85  
      * 
 86  
      * @param value  the value to set
 87  
      */
 88  
     public void setValue(final boolean value) {
 89  3
         this.value = value;
 90  3
     }
 91  
 
 92  
     /**
 93  
      * Sets the value to true.
 94  
      * 
 95  
      * @since 3.3
 96  
      */
 97  
     public void setFalse() {
 98  1
         this.value = false;
 99  1
     }
 100  
 
 101  
     /**
 102  
      * Sets the value to false.
 103  
      * 
 104  
      * @since 3.3
 105  
      */
 106  
     public void setTrue() {
 107  1
         this.value = true;
 108  1
     }
 109  
 
 110  
     /**
 111  
      * Sets the value from any Boolean instance.
 112  
      * 
 113  
      * @param value  the value to set, not null
 114  
      * @throws NullPointerException if the object is null
 115  
      */
 116  
     @Override
 117  
     public void setValue(final Boolean value) {
 118  2
         this.value = value.booleanValue();
 119  1
     }
 120  
 
 121  
     //-----------------------------------------------------------------------
 122  
     /**
 123  
      * Checks if the current value is <code>true</code>.
 124  
      * 
 125  
      * @return <code>true</code> if the current value is <code>true</code>
 126  
      * @since 2.5
 127  
      */
 128  
     public boolean isTrue() {
 129  2
         return value == true;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Checks if the current value is <code>false</code>.
 134  
      * 
 135  
      * @return <code>true</code> if the current value is <code>false</code>
 136  
      * @since 2.5
 137  
      */
 138  
     public boolean isFalse() {
 139  2
         return value == false;
 140  
     }
 141  
 
 142  
     //-----------------------------------------------------------------------
 143  
     /**
 144  
      * Returns the value of this MutableBoolean as a boolean.
 145  
      * 
 146  
      * @return the boolean value represented by this object.
 147  
      */
 148  
     public boolean booleanValue() {
 149  21
         return value;
 150  
     }
 151  
 
 152  
     //-----------------------------------------------------------------------
 153  
     /**
 154  
      * Gets this mutable as an instance of Boolean.
 155  
      *
 156  
      * @return a Boolean instance containing the value from this mutable, never null
 157  
      * @since 2.5
 158  
      */
 159  
     public Boolean toBoolean() {
 160  2
         return Boolean.valueOf(booleanValue());
 161  
     }
 162  
 
 163  
     //-----------------------------------------------------------------------
 164  
     /**
 165  
      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
 166  
      * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
 167  
      * <code>boolean</code> value as this object.
 168  
      * 
 169  
      * @param obj  the object to compare with, null returns false
 170  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 171  
      */
 172  
     @Override
 173  
     public boolean equals(final Object obj) {
 174  10
         if (obj instanceof MutableBoolean) {
 175  7
             return value == ((MutableBoolean) obj).booleanValue();
 176  
         }
 177  3
         return false;
 178  
     }
 179  
 
 180  
     /**
 181  
      * Returns a suitable hash code for this mutable.
 182  
      * 
 183  
      * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
 184  
      */
 185  
     @Override
 186  
     public int hashCode() {
 187  8
         return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
 188  
     }
 189  
 
 190  
     //-----------------------------------------------------------------------
 191  
     /**
 192  
      * Compares this mutable to another in ascending order.
 193  
      * 
 194  
      * @param other  the other mutable to compare to, not null
 195  
      * @return negative if this is less, zero if equal, positive if greater
 196  
      *  where false is less than true
 197  
      */
 198  
     @Override
 199  
     public int compareTo(final MutableBoolean other) {
 200  5
         return BooleanUtils.compare(this.value, other.value);
 201  
     }
 202  
 
 203  
     //-----------------------------------------------------------------------
 204  
     /**
 205  
      * Returns the String value of this mutable.
 206  
      * 
 207  
      * @return the mutable value as a string
 208  
      */
 209  
     @Override
 210  
     public String toString() {
 211  2
         return String.valueOf(value);
 212  
     }
 213  
 
 214  
 }