Coverage Report - org.apache.commons.lang3.mutable.MutableBoolean
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableBoolean
96%
28/29
100%
14/14
1,312
 
 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  
 /**
 23  
  * A mutable <code>boolean</code> wrapper.
 24  
  * <p>
 25  
  * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter. 
 26  
  * 
 27  
  * @see Boolean
 28  
  * @since 2.2
 29  
  * @version $Id: MutableBoolean.java 1565915 2014-02-08 04:35:17Z ggregory $
 30  
  */
 31  0
 public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
 32  
 
 33  
     /**
 34  
      * Required for serialization support.
 35  
      * 
 36  
      * @see java.io.Serializable
 37  
      */
 38  
     private static final long serialVersionUID = -4830728138360036487L;
 39  
 
 40  
     /** The mutable value. */
 41  
     private boolean value;
 42  
 
 43  
     /**
 44  
      * Constructs a new MutableBoolean with the default value of false.
 45  
      */
 46  
     public MutableBoolean() {
 47  3
         super();
 48  3
     }
 49  
 
 50  
     /**
 51  
      * Constructs a new MutableBoolean with the specified value.
 52  
      * 
 53  
      * @param value  the initial value to store
 54  
      */
 55  
     public MutableBoolean(final boolean value) {
 56  18
         super();
 57  18
         this.value = value;
 58  18
     }
 59  
 
 60  
     /**
 61  
      * Constructs a new MutableBoolean with the specified value.
 62  
      * 
 63  
      * @param value  the initial value to store, not null
 64  
      * @throws NullPointerException if the object is null
 65  
      */
 66  
     public MutableBoolean(final Boolean value) {
 67  3
         super();
 68  3
         this.value = value.booleanValue();
 69  2
     }
 70  
 
 71  
     //-----------------------------------------------------------------------
 72  
     /**
 73  
      * Gets the value as a Boolean instance.
 74  
      * 
 75  
      * @return the value as a Boolean, never null
 76  
      */
 77  
     @Override
 78  
     public Boolean getValue() {
 79  1
         return Boolean.valueOf(this.value);
 80  
     }
 81  
 
 82  
     /**
 83  
      * Sets the value.
 84  
      * 
 85  
      * @param value  the value to set
 86  
      */
 87  
     public void setValue(final boolean value) {
 88  3
         this.value = value;
 89  3
     }
 90  
 
 91  
     /**
 92  
      * Sets the value to true.
 93  
      * 
 94  
      * @since 3.3
 95  
      */
 96  
     public void setFalse() {
 97  1
         this.value = false;
 98  1
     }
 99  
 
 100  
     /**
 101  
      * Sets the value to false.
 102  
      * 
 103  
      * @since 3.3
 104  
      */
 105  
     public void setTrue() {
 106  1
         this.value = true;
 107  1
     }
 108  
 
 109  
     /**
 110  
      * Sets the value from any Boolean instance.
 111  
      * 
 112  
      * @param value  the value to set, not null
 113  
      * @throws NullPointerException if the object is null
 114  
      */
 115  
     @Override
 116  
     public void setValue(final Boolean value) {
 117  2
         this.value = value.booleanValue();
 118  1
     }
 119  
 
 120  
     //-----------------------------------------------------------------------
 121  
     /**
 122  
      * Checks if the current value is <code>true</code>.
 123  
      * 
 124  
      * @return <code>true</code> if the current value is <code>true</code>
 125  
      * @since 2.5
 126  
      */
 127  
     public boolean isTrue() {
 128  2
         return value == true;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Checks if the current value is <code>false</code>.
 133  
      * 
 134  
      * @return <code>true</code> if the current value is <code>false</code>
 135  
      * @since 2.5
 136  
      */
 137  
     public boolean isFalse() {
 138  2
         return value == false;
 139  
     }
 140  
 
 141  
     //-----------------------------------------------------------------------
 142  
     /**
 143  
      * Returns the value of this MutableBoolean as a boolean.
 144  
      * 
 145  
      * @return the boolean value represented by this object.
 146  
      */
 147  
     public boolean booleanValue() {
 148  21
         return value;
 149  
     }
 150  
 
 151  
     //-----------------------------------------------------------------------
 152  
     /**
 153  
      * Gets this mutable as an instance of Boolean.
 154  
      *
 155  
      * @return a Boolean instance containing the value from this mutable, never null
 156  
      * @since 2.5
 157  
      */
 158  
     public Boolean toBoolean() {
 159  2
         return Boolean.valueOf(booleanValue());
 160  
     }
 161  
 
 162  
     //-----------------------------------------------------------------------
 163  
     /**
 164  
      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
 165  
      * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
 166  
      * <code>boolean</code> value as this object.
 167  
      * 
 168  
      * @param obj  the object to compare with, null returns false
 169  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 170  
      */
 171  
     @Override
 172  
     public boolean equals(final Object obj) {
 173  10
         if (obj instanceof MutableBoolean) {
 174  7
             return value == ((MutableBoolean) obj).booleanValue();
 175  
         }
 176  3
         return false;
 177  
     }
 178  
 
 179  
     /**
 180  
      * Returns a suitable hash code for this mutable.
 181  
      * 
 182  
      * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
 183  
      */
 184  
     @Override
 185  
     public int hashCode() {
 186  8
         return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
 187  
     }
 188  
 
 189  
     //-----------------------------------------------------------------------
 190  
     /**
 191  
      * Compares this mutable to another in ascending order.
 192  
      * 
 193  
      * @param other  the other mutable to compare to, not null
 194  
      * @return negative if this is less, zero if equal, positive if greater
 195  
      *  where false is less than true
 196  
      */
 197  
     @Override
 198  
     public int compareTo(final MutableBoolean other) {
 199  5
         final boolean anotherVal = other.value;
 200  4
         return value == anotherVal ? 0 : (value ? 1 : -1);
 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  
 }