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