Coverage Report - org.apache.commons.lang3.mutable.MutableByte
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableByte
97%
40/41
100%
4/4
1,087
 
 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  
 package org.apache.commons.lang3.mutable;
 18  
 
 19  
 import org.apache.commons.lang3.math.NumberUtils;
 20  
 
 21  
 /**
 22  
  * A mutable <code>byte</code> wrapper.
 23  
  * <p>
 24  
  * Note that as MutableByte does not extend Byte, it is not treated by String.format as a Byte parameter. 
 25  
  * 
 26  
  * @see Byte
 27  
  * @since 2.1
 28  
  */
 29  0
 public class MutableByte extends Number implements Comparable<MutableByte>, Mutable<Number> {
 30  
 
 31  
     /**
 32  
      * Required for serialization support.
 33  
      * 
 34  
      * @see java.io.Serializable
 35  
      */
 36  
     private static final long serialVersionUID = -1585823265L;
 37  
 
 38  
     /** The mutable value. */
 39  
     private byte value;
 40  
 
 41  
     /**
 42  
      * Constructs a new MutableByte with the default value of zero.
 43  
      */
 44  
     public MutableByte() {
 45  3
         super();
 46  3
     }
 47  
 
 48  
     /**
 49  
      * Constructs a new MutableByte with the specified value.
 50  
      * 
 51  
      * @param value  the initial value to store
 52  
      */
 53  
     public MutableByte(final byte value) {
 54  28
         super();
 55  28
         this.value = value;
 56  28
     }
 57  
 
 58  
     /**
 59  
      * Constructs a new MutableByte with the specified value.
 60  
      * 
 61  
      * @param value  the initial value to store, not null
 62  
      * @throws NullPointerException if the object is null
 63  
      */
 64  
     public MutableByte(final Number value) {
 65  3
         super();
 66  3
         this.value = value.byteValue();
 67  2
     }
 68  
 
 69  
     /**
 70  
      * Constructs a new MutableByte parsing the given string.
 71  
      * 
 72  
      * @param value  the string to parse, not null
 73  
      * @throws NumberFormatException if the string cannot be parsed into a byte
 74  
      * @since 2.5
 75  
      */
 76  
     public MutableByte(final String value) throws NumberFormatException {
 77  1
         super();
 78  1
         this.value = Byte.parseByte(value);
 79  1
     }
 80  
 
 81  
     //-----------------------------------------------------------------------
 82  
     /**
 83  
      * Gets the value as a Byte instance.
 84  
      * 
 85  
      * @return the value as a Byte, never null
 86  
      */
 87  
     @Override
 88  
     public Byte getValue() {
 89  4
         return Byte.valueOf(this.value);
 90  
     }
 91  
 
 92  
     /**
 93  
      * Sets the value.
 94  
      * 
 95  
      * @param value  the value to set
 96  
      */
 97  
     public void setValue(final byte value) {
 98  1
         this.value = value;
 99  1
     }
 100  
 
 101  
     /**
 102  
      * Sets the value from any Number instance.
 103  
      * 
 104  
      * @param value  the value to set, not null
 105  
      * @throws NullPointerException if the object is null
 106  
      */
 107  
     @Override
 108  
     public void setValue(final Number value) {
 109  3
         this.value = value.byteValue();
 110  2
     }
 111  
 
 112  
     //-----------------------------------------------------------------------
 113  
     /**
 114  
      * Increments the value.
 115  
      *
 116  
      * @since Commons Lang 2.2
 117  
      */
 118  
     public void increment() {
 119  1
         value++;
 120  1
     }
 121  
 
 122  
     /**
 123  
      * Decrements the value.
 124  
      *
 125  
      * @since Commons Lang 2.2
 126  
      */
 127  
     public void decrement() {
 128  1
         value--;
 129  1
     }
 130  
 
 131  
     //-----------------------------------------------------------------------
 132  
     /**
 133  
      * Adds a value to the value of this instance.
 134  
      * 
 135  
      * @param operand  the value to add, not null
 136  
      * @since Commons Lang 2.2
 137  
      */
 138  
     public void add(final byte operand) {
 139  1
         this.value += operand;
 140  1
     }
 141  
 
 142  
     /**
 143  
      * Adds a value to the value of this instance.
 144  
      * 
 145  
      * @param operand  the value to add, not null
 146  
      * @throws NullPointerException if the object is null
 147  
      * @since Commons Lang 2.2
 148  
      */
 149  
     public void add(final Number operand) {
 150  1
         this.value += operand.byteValue();
 151  1
     }
 152  
 
 153  
     /**
 154  
      * Subtracts a value from the value of this instance.
 155  
      * 
 156  
      * @param operand  the value to subtract, not null
 157  
      * @since Commons Lang 2.2
 158  
      */
 159  
     public void subtract(final byte operand) {
 160  1
         this.value -= operand;
 161  1
     }
 162  
 
 163  
     /**
 164  
      * Subtracts a value from the value of this instance.
 165  
      * 
 166  
      * @param operand  the value to subtract, not null
 167  
      * @throws NullPointerException if the object is null
 168  
      * @since Commons Lang 2.2
 169  
      */
 170  
     public void subtract(final Number operand) {
 171  1
         this.value -= operand.byteValue();
 172  1
     }
 173  
 
 174  
     //-----------------------------------------------------------------------
 175  
     // shortValue relies on Number implementation
 176  
     /**
 177  
      * Returns the value of this MutableByte as a byte.
 178  
      *
 179  
      * @return the numeric value represented by this object after conversion to type byte.
 180  
      */
 181  
     @Override
 182  
     public byte byteValue() {
 183  25
         return value;
 184  
     }
 185  
 
 186  
     /**
 187  
      * Returns the value of this MutableByte as an int.
 188  
      *
 189  
      * @return the numeric value represented by this object after conversion to type int.
 190  
      */
 191  
     @Override
 192  
     public int intValue() {
 193  4
         return value;
 194  
     }
 195  
 
 196  
     /**
 197  
      * Returns the value of this MutableByte as a long.
 198  
      *
 199  
      * @return the numeric value represented by this object after conversion to type long.
 200  
      */
 201  
     @Override
 202  
     public long longValue() {
 203  3
         return value;
 204  
     }
 205  
 
 206  
     /**
 207  
      * Returns the value of this MutableByte as a float.
 208  
      *
 209  
      * @return the numeric value represented by this object after conversion to type float.
 210  
      */
 211  
     @Override
 212  
     public float floatValue() {
 213  1
         return value;
 214  
     }
 215  
 
 216  
     /**
 217  
      * Returns the value of this MutableByte as a double.
 218  
      *
 219  
      * @return the numeric value represented by this object after conversion to type double.
 220  
      */
 221  
     @Override
 222  
     public double doubleValue() {
 223  1
         return value;
 224  
     }
 225  
 
 226  
     //-----------------------------------------------------------------------
 227  
     /**
 228  
      * Gets this mutable as an instance of Byte.
 229  
      *
 230  
      * @return a Byte instance containing the value from this mutable
 231  
      */
 232  
     public Byte toByte() {
 233  2
         return Byte.valueOf(byteValue());
 234  
     }
 235  
 
 236  
     //-----------------------------------------------------------------------
 237  
     /**
 238  
      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
 239  
      * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
 240  
      * as this object.
 241  
      * 
 242  
      * @param obj  the object to compare with, null returns false
 243  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 244  
      */
 245  
     @Override
 246  
     public boolean equals(final Object obj) {
 247  10
         if (obj instanceof MutableByte) {
 248  7
             return value == ((MutableByte) obj).byteValue();
 249  
         }
 250  3
         return false;
 251  
     }
 252  
 
 253  
     /**
 254  
      * Returns a suitable hash code for this mutable.
 255  
      * 
 256  
      * @return a suitable hash code
 257  
      */
 258  
     @Override
 259  
     public int hashCode() {
 260  7
         return value;
 261  
     }
 262  
 
 263  
     //-----------------------------------------------------------------------
 264  
     /**
 265  
      * Compares this mutable to another in ascending order.
 266  
      * 
 267  
      * @param other  the other mutable to compare to, not null
 268  
      * @return negative if this is less, zero if equal, positive if greater
 269  
      */
 270  
     @Override
 271  
     public int compareTo(final MutableByte other) {
 272  4
         return NumberUtils.compare(this.value, other.value);
 273  
     }
 274  
 
 275  
     //-----------------------------------------------------------------------
 276  
     /**
 277  
      * Returns the String value of this mutable.
 278  
      * 
 279  
      * @return the mutable value as a string
 280  
      */
 281  
     @Override
 282  
     public String toString() {
 283  3
         return String.valueOf(value);
 284  
     }
 285  
 
 286  
 }