001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3.mutable; 018 019import org.apache.commons.lang3.math.NumberUtils; 020 021/** 022 * A mutable <code>long</code> wrapper. 023 * <p> 024 * Note that as MutableLong does not extend Long, it is not treated by String.format as a Long parameter. 025 * 026 * @see Long 027 * @since 2.1 028 * @version $Id: MutableLong.java 1669791 2015-03-28 15:22:59Z britter $ 029 */ 030public class MutableLong extends Number implements Comparable<MutableLong>, Mutable<Number> { 031 032 /** 033 * Required for serialization support. 034 * 035 * @see java.io.Serializable 036 */ 037 private static final long serialVersionUID = 62986528375L; 038 039 /** The mutable value. */ 040 private long value; 041 042 /** 043 * Constructs a new MutableLong with the default value of zero. 044 */ 045 public MutableLong() { 046 super(); 047 } 048 049 /** 050 * Constructs a new MutableLong with the specified value. 051 * 052 * @param value the initial value to store 053 */ 054 public MutableLong(final long value) { 055 super(); 056 this.value = value; 057 } 058 059 /** 060 * Constructs a new MutableLong with the specified value. 061 * 062 * @param value the initial value to store, not null 063 * @throws NullPointerException if the object is null 064 */ 065 public MutableLong(final Number value) { 066 super(); 067 this.value = value.longValue(); 068 } 069 070 /** 071 * Constructs a new MutableLong parsing the given string. 072 * 073 * @param value the string to parse, not null 074 * @throws NumberFormatException if the string cannot be parsed into a long 075 * @since 2.5 076 */ 077 public MutableLong(final String value) throws NumberFormatException { 078 super(); 079 this.value = Long.parseLong(value); 080 } 081 082 //----------------------------------------------------------------------- 083 /** 084 * Gets the value as a Long instance. 085 * 086 * @return the value as a Long, never null 087 */ 088 @Override 089 public Long getValue() { 090 return Long.valueOf(this.value); 091 } 092 093 /** 094 * Sets the value. 095 * 096 * @param value the value to set 097 */ 098 public void setValue(final long value) { 099 this.value = value; 100 } 101 102 /** 103 * Sets the value from any Number instance. 104 * 105 * @param value the value to set, not null 106 * @throws NullPointerException if the object is null 107 */ 108 @Override 109 public void setValue(final Number value) { 110 this.value = value.longValue(); 111 } 112 113 //----------------------------------------------------------------------- 114 /** 115 * Increments the value. 116 * 117 * @since Commons Lang 2.2 118 */ 119 public void increment() { 120 value++; 121 } 122 123 /** 124 * Decrements the value. 125 * 126 * @since Commons Lang 2.2 127 */ 128 public void decrement() { 129 value--; 130 } 131 132 //----------------------------------------------------------------------- 133 /** 134 * Adds a value to the value of this instance. 135 * 136 * @param operand the value to add, not null 137 * @since Commons Lang 2.2 138 */ 139 public void add(final long operand) { 140 this.value += operand; 141 } 142 143 /** 144 * Adds a value to the value of this instance. 145 * 146 * @param operand the value to add, not null 147 * @throws NullPointerException if the object is null 148 * @since Commons Lang 2.2 149 */ 150 public void add(final Number operand) { 151 this.value += operand.longValue(); 152 } 153 154 /** 155 * Subtracts a value from the value of this instance. 156 * 157 * @param operand the value to subtract, not null 158 * @since Commons Lang 2.2 159 */ 160 public void subtract(final long operand) { 161 this.value -= operand; 162 } 163 164 /** 165 * Subtracts a value from the value of this instance. 166 * 167 * @param operand the value to subtract, not null 168 * @throws NullPointerException if the object is null 169 * @since Commons Lang 2.2 170 */ 171 public void subtract(final Number operand) { 172 this.value -= operand.longValue(); 173 } 174 175 //----------------------------------------------------------------------- 176 // shortValue and byteValue rely on Number implementation 177 /** 178 * Returns the value of this MutableLong as an int. 179 * 180 * @return the numeric value represented by this object after conversion to type int. 181 */ 182 @Override 183 public int intValue() { 184 return (int) value; 185 } 186 187 /** 188 * Returns the value of this MutableLong as a long. 189 * 190 * @return the numeric value represented by this object after conversion to type long. 191 */ 192 @Override 193 public long longValue() { 194 return value; 195 } 196 197 /** 198 * Returns the value of this MutableLong as a float. 199 * 200 * @return the numeric value represented by this object after conversion to type float. 201 */ 202 @Override 203 public float floatValue() { 204 return value; 205 } 206 207 /** 208 * Returns the value of this MutableLong as a double. 209 * 210 * @return the numeric value represented by this object after conversion to type double. 211 */ 212 @Override 213 public double doubleValue() { 214 return value; 215 } 216 217 //----------------------------------------------------------------------- 218 /** 219 * Gets this mutable as an instance of Long. 220 * 221 * @return a Long instance containing the value from this mutable, never null 222 */ 223 public Long toLong() { 224 return Long.valueOf(longValue()); 225 } 226 227 //----------------------------------------------------------------------- 228 /** 229 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument 230 * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code> 231 * value as this object. 232 * 233 * @param obj the object to compare with, null returns false 234 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 235 */ 236 @Override 237 public boolean equals(final Object obj) { 238 if (obj instanceof MutableLong) { 239 return value == ((MutableLong) obj).longValue(); 240 } 241 return false; 242 } 243 244 /** 245 * Returns a suitable hash code for this mutable. 246 * 247 * @return a suitable hash code 248 */ 249 @Override 250 public int hashCode() { 251 return (int) (value ^ (value >>> 32)); 252 } 253 254 //----------------------------------------------------------------------- 255 /** 256 * Compares this mutable to another in ascending order. 257 * 258 * @param other the other mutable to compare to, not null 259 * @return negative if this is less, zero if equal, positive if greater 260 */ 261 @Override 262 public int compareTo(final MutableLong other) { 263 return NumberUtils.compare(this.value, other.value); 264 } 265 266 //----------------------------------------------------------------------- 267 /** 268 * Returns the String value of this mutable. 269 * 270 * @return the mutable value as a string 271 */ 272 @Override 273 public String toString() { 274 return String.valueOf(value); 275 } 276 277}