Coverage Report - org.apache.commons.lang3.mutable.MutableByte
 
Classes in this File Line Coverage Branch Coverage Complexity
MutableByte
98%
60/61
100%
4/4
1,065
 
 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  36
         super();
 55  36
         this.value = value;
 56  36
     }
 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  
      * Increments this instance's value by 1; this method returns the value associated with the instance
 124  
      * immediately prior to the increment operation. This method is not thread safe.
 125  
      *
 126  
      * @return the value associated with the instance before it was incremented
 127  
      * @since 3.5
 128  
      */
 129  
     public byte getAndIncrement() {
 130  1
         byte last = value;
 131  1
         value++;
 132  1
         return last;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Increments this instance's value by 1; this method returns the value associated with the instance
 137  
      * immediately after the increment operation. This method is not thread safe.
 138  
      *
 139  
      * @return the value associated with the instance after it is incremented
 140  
      * @since 3.5
 141  
      */
 142  
     public byte incrementAndGet() {
 143  1
         value++;
 144  1
         return value;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Decrements the value.
 149  
      *
 150  
      * @since Commons Lang 2.2
 151  
      */
 152  
     public void decrement() {
 153  1
         value--;
 154  1
     }
 155  
 
 156  
     /**
 157  
      * Decrements this instance's value by 1; this method returns the value associated with the instance
 158  
      * immediately prior to the decrement operation. This method is not thread safe.
 159  
      *
 160  
      * @return the value associated with the instance before it was decremented
 161  
      * @since 3.5
 162  
      */
 163  
     public byte getAndDecrement() {
 164  1
         byte last = value;
 165  1
         value--;
 166  1
         return last;
 167  
     }
 168  
 
 169  
     /**
 170  
      * Decrements this instance's value by 1; this method returns the value associated with the instance
 171  
      * immediately after the decrement operation. This method is not thread safe.
 172  
      *
 173  
      * @return the value associated with the instance after it is decremented
 174  
      * @since 3.5
 175  
      */
 176  
     public byte decrementAndGet() {
 177  1
         value--;
 178  1
         return value;
 179  
     }
 180  
 
 181  
     //-----------------------------------------------------------------------
 182  
     /**
 183  
      * Adds a value to the value of this instance.
 184  
      * 
 185  
      * @param operand  the value to add, not null
 186  
      * @since Commons Lang 2.2
 187  
      */
 188  
     public void add(final byte operand) {
 189  1
         this.value += operand;
 190  1
     }
 191  
 
 192  
     /**
 193  
      * Adds a value to the value of this instance.
 194  
      * 
 195  
      * @param operand  the value to add, not null
 196  
      * @throws NullPointerException if the object is null
 197  
      * @since Commons Lang 2.2
 198  
      */
 199  
     public void add(final Number operand) {
 200  1
         this.value += operand.byteValue();
 201  1
     }
 202  
 
 203  
     /**
 204  
      * Subtracts a value from the value of this instance.
 205  
      * 
 206  
      * @param operand  the value to subtract, not null
 207  
      * @since Commons Lang 2.2
 208  
      */
 209  
     public void subtract(final byte operand) {
 210  1
         this.value -= operand;
 211  1
     }
 212  
 
 213  
     /**
 214  
      * Subtracts a value from the value of this instance.
 215  
      * 
 216  
      * @param operand  the value to subtract, not null
 217  
      * @throws NullPointerException if the object is null
 218  
      * @since Commons Lang 2.2
 219  
      */
 220  
     public void subtract(final Number operand) {
 221  1
         this.value -= operand.byteValue();
 222  1
     }
 223  
 
 224  
     /**
 225  
      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
 226  
      * immediately after the addition operation. This method is not thread safe.
 227  
      *
 228  
      * @param operand the quantity to add, not null
 229  
      * @return the value associated with this instance after adding the operand
 230  
      * @since 3.5
 231  
      */
 232  
     public byte addAndGet(final byte operand) {
 233  1
         this.value += operand;
 234  1
         return value;
 235  
     }
 236  
 
 237  
     /**
 238  
      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
 239  
      * immediately after the addition operation. This method is not thread safe.
 240  
      *
 241  
      * @param operand the quantity to add, not null
 242  
      * @throws NullPointerException if {@code operand} is null
 243  
      * @return the value associated with this instance after adding the operand
 244  
      * @since 3.5
 245  
      */
 246  
     public byte addAndGet(final Number operand) {
 247  1
         this.value += operand.byteValue();
 248  1
         return value;
 249  
     }
 250  
 
 251  
     /**
 252  
      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
 253  
      * immediately prior to the addition operation. This method is not thread safe.
 254  
      *
 255  
      * @param operand the quantity to add, not null
 256  
      * @return the value associated with this instance immediately before the operand was added
 257  
      * @since 3.5
 258  
      */
 259  
     public byte getAndAdd(final byte operand) {
 260  1
         byte last = value;
 261  1
         this.value += operand;
 262  1
         return last;
 263  
     }
 264  
 
 265  
     /**
 266  
      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
 267  
      * immediately prior to the addition operation. This method is not thread safe.
 268  
      *
 269  
      * @param operand the quantity to add, not null
 270  
      * @throws NullPointerException if {@code operand} is null
 271  
      * @return the value associated with this instance immediately before the operand was added
 272  
      * @since 3.5
 273  
      */
 274  
     public byte getAndAdd(final Number operand) {
 275  1
         byte last = value;
 276  1
         this.value += operand.byteValue();
 277  1
         return last;
 278  
     }
 279  
 
 280  
     //-----------------------------------------------------------------------
 281  
     // shortValue relies on Number implementation
 282  
     /**
 283  
      * Returns the value of this MutableByte as a byte.
 284  
      *
 285  
      * @return the numeric value represented by this object after conversion to type byte.
 286  
      */
 287  
     @Override
 288  
     public byte byteValue() {
 289  29
         return value;
 290  
     }
 291  
 
 292  
     /**
 293  
      * Returns the value of this MutableByte as an int.
 294  
      *
 295  
      * @return the numeric value represented by this object after conversion to type int.
 296  
      */
 297  
     @Override
 298  
     public int intValue() {
 299  8
         return value;
 300  
     }
 301  
 
 302  
     /**
 303  
      * Returns the value of this MutableByte as a long.
 304  
      *
 305  
      * @return the numeric value represented by this object after conversion to type long.
 306  
      */
 307  
     @Override
 308  
     public long longValue() {
 309  7
         return value;
 310  
     }
 311  
 
 312  
     /**
 313  
      * Returns the value of this MutableByte as a float.
 314  
      *
 315  
      * @return the numeric value represented by this object after conversion to type float.
 316  
      */
 317  
     @Override
 318  
     public float floatValue() {
 319  1
         return value;
 320  
     }
 321  
 
 322  
     /**
 323  
      * Returns the value of this MutableByte as a double.
 324  
      *
 325  
      * @return the numeric value represented by this object after conversion to type double.
 326  
      */
 327  
     @Override
 328  
     public double doubleValue() {
 329  1
         return value;
 330  
     }
 331  
 
 332  
     //-----------------------------------------------------------------------
 333  
     /**
 334  
      * Gets this mutable as an instance of Byte.
 335  
      *
 336  
      * @return a Byte instance containing the value from this mutable
 337  
      */
 338  
     public Byte toByte() {
 339  2
         return Byte.valueOf(byteValue());
 340  
     }
 341  
 
 342  
     //-----------------------------------------------------------------------
 343  
     /**
 344  
      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
 345  
      * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
 346  
      * as this object.
 347  
      * 
 348  
      * @param obj  the object to compare with, null returns false
 349  
      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
 350  
      */
 351  
     @Override
 352  
     public boolean equals(final Object obj) {
 353  10
         if (obj instanceof MutableByte) {
 354  7
             return value == ((MutableByte) obj).byteValue();
 355  
         }
 356  3
         return false;
 357  
     }
 358  
 
 359  
     /**
 360  
      * Returns a suitable hash code for this mutable.
 361  
      * 
 362  
      * @return a suitable hash code
 363  
      */
 364  
     @Override
 365  
     public int hashCode() {
 366  7
         return value;
 367  
     }
 368  
 
 369  
     //-----------------------------------------------------------------------
 370  
     /**
 371  
      * Compares this mutable to another in ascending order.
 372  
      * 
 373  
      * @param other  the other mutable to compare to, not null
 374  
      * @return negative if this is less, zero if equal, positive if greater
 375  
      */
 376  
     @Override
 377  
     public int compareTo(final MutableByte other) {
 378  4
         return NumberUtils.compare(this.value, other.value);
 379  
     }
 380  
 
 381  
     //-----------------------------------------------------------------------
 382  
     /**
 383  
      * Returns the String value of this mutable.
 384  
      * 
 385  
      * @return the mutable value as a string
 386  
      */
 387  
     @Override
 388  
     public String toString() {
 389  3
         return String.valueOf(value);
 390  
     }
 391  
 
 392  
 }