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 * https://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 java.util.concurrent.atomic.AtomicInteger; 020 021/** 022 * A mutable {@code short} wrapper. 023 * <p> 024 * This class was created before the introduction of the {@link java.util.concurrent.atomic} package and the {@link AtomicInteger} class. 025 * </p> 026 * <p> 027 * Note that as MutableShort does not extend Short, it is not treated by String.format as a Short parameter. 028 * </p> 029 * 030 * @see Short 031 * @see AtomicInteger 032 * @since 2.1 033 */ 034public class MutableShort extends Number implements Comparable<MutableShort>, Mutable<Number> { 035 036 /** 037 * Required for serialization support. 038 * 039 * @see java.io.Serializable 040 */ 041 private static final long serialVersionUID = -2135791679L; 042 043 /** The mutable value. */ 044 private short value; 045 046 /** 047 * Constructs a new MutableShort with the default value of zero. 048 */ 049 public MutableShort() { 050 } 051 052 /** 053 * Constructs a new MutableShort with the specified value. 054 * 055 * @param value The initial value to store, not null. 056 * @throws NullPointerException Thrown if the object is null. 057 */ 058 public MutableShort(final Number value) { 059 this.value = value.shortValue(); 060 } 061 062 /** 063 * Constructs a new MutableShort with the specified value. 064 * 065 * @param value The initial value to store. 066 */ 067 public MutableShort(final short value) { 068 this.value = value; 069 } 070 071 /** 072 * Constructs a new MutableShort parsing the given string. 073 * 074 * @param value The string to parse, not null. 075 * @throws NumberFormatException Thrown if the string cannot be parsed into a short, see {@link Short#parseShort(String)}. 076 * @since 2.5 077 */ 078 public MutableShort(final String value) { 079 this.value = Short.parseShort(value); 080 } 081 082 /** 083 * Adds a value to the value of this instance. 084 * 085 * @param operand The value to add, not null. 086 * @throws NullPointerException Thrown if the object is null. 087 * @since 2.2 088 */ 089 public void add(final Number operand) { 090 this.value += operand.shortValue(); 091 } 092 093 /** 094 * Adds a value to the value of this instance. 095 * 096 * @param operand The value to add, not null. 097 * @since 2.2 098 */ 099 public void add(final short operand) { 100 this.value += operand; 101 } 102 103 /** 104 * Increments this instance's value by {@code operand}; this method returns the value associated with the instance 105 * immediately after the addition operation. This method is not thread safe. 106 * 107 * @param operand The quantity to add, not null. 108 * @throws NullPointerException Thrown if {@code operand} is null. 109 * @return The value associated with this instance after adding the operand. 110 * @since 3.5 111 */ 112 public short addAndGet(final Number operand) { 113 this.value += operand.shortValue(); 114 return value; 115 } 116 117 /** 118 * Increments this instance's value by {@code operand}; this method returns the value associated with the instance 119 * immediately after the addition operation. This method is not thread safe. 120 * 121 * @param operand The quantity to add, not null. 122 * @return The value associated with this instance after adding the operand. 123 * @since 3.5 124 */ 125 public short addAndGet(final short operand) { 126 this.value += operand; 127 return value; 128 } 129 130 /** 131 * Compares this mutable to another in ascending order. 132 * 133 * @param other The other mutable to compare to, not null. 134 * @return negative if this is less, zero if equal, positive if greater. 135 */ 136 @Override 137 public int compareTo(final MutableShort other) { 138 return Short.compare(this.value, other.value); 139 } 140 141 /** 142 * Decrements the value. 143 * 144 * @since 2.2 145 */ 146 public void decrement() { 147 value--; 148 } 149 150 /** 151 * Decrements this instance's value by 1; this method returns the value associated with the instance 152 * immediately after the decrement operation. This method is not thread safe. 153 * 154 * @return The value associated with the instance after it is decremented. 155 * @since 3.5 156 */ 157 public short decrementAndGet() { 158 value--; 159 return value; 160 } 161 162 /** 163 * Returns the value of this MutableShort as a double. 164 * 165 * @return The numeric value represented by this object after conversion to type double. 166 */ 167 @Override 168 public double doubleValue() { 169 return value; 170 } 171 172 /** 173 * Compares this object to the specified object. The result is {@code true} if and only if the argument 174 * is not {@code null} and is a {@link MutableShort} object that contains the same {@code short} 175 * value as this object. 176 * 177 * @param obj The object to compare with, null returns false. 178 * @return {@code true} if the objects are the same; {@code false} otherwise. 179 */ 180 @Override 181 public boolean equals(final Object obj) { 182 if (obj instanceof MutableShort) { 183 return value == ((MutableShort) obj).shortValue(); 184 } 185 return false; 186 } 187 188 /** 189 * Returns the value of this MutableShort as a float. 190 * 191 * @return The numeric value represented by this object after conversion to type float. 192 */ 193 @Override 194 public float floatValue() { 195 return value; 196 } 197 198 /** 199 * Gets this instance's current value, then adds {@code operand}. This method is not thread-safe. 200 * 201 * @param operand The quantity to add, not null. 202 * @throws NullPointerException Thrown if {@code operand} is null. 203 * @return The value associated with this instance immediately before the operand was added. 204 * @since 3.5 205 */ 206 public short getAndAdd(final Number operand) { 207 final short last = value; 208 this.value += operand.shortValue(); 209 return last; 210 } 211 212 /** 213 * Gets this instance's current value, then adds {@code operand}. This method is not thread-safe. 214 * 215 * @param operand The quantity to add, not null. 216 * @return The value associated with this instance immediately before the operand was added. 217 * @since 3.5 218 */ 219 public short getAndAdd(final short operand) { 220 final short last = value; 221 this.value += operand; 222 return last; 223 } 224 225 /** 226 * Gets this instance's current value, then decrements it by 1. This method is not thread-safe. 227 * 228 * @return The value associated with the instance before it was decremented. 229 * @since 3.5 230 */ 231 public short getAndDecrement() { 232 final short last = value; 233 value--; 234 return last; 235 } 236 237 /** 238 * Gets this instance's current value, then increments it by 1. This method is not thread-safe. 239 * 240 * @return The value associated with the instance before it was incremented. 241 * @since 3.5 242 */ 243 public short getAndIncrement() { 244 final short last = value; 245 value++; 246 return last; 247 } 248 249 /** 250 * Gets the value as a Short instance. 251 * 252 * @return The value as a Short, never null. 253 * @deprecated Use {@link #get()}. 254 */ 255 @Deprecated 256 @Override 257 public Short getValue() { 258 return Short.valueOf(this.value); 259 } 260 261 /** 262 * Returns a suitable hash code for this mutable. 263 * 264 * @return A suitable hash code. 265 */ 266 @Override 267 public int hashCode() { 268 return value; 269 } 270 271 /** 272 * Increments the value. 273 * 274 * @since 2.2 275 */ 276 public void increment() { 277 value++; 278 } 279 280 /** 281 * Increments this instance's value by 1; this method returns the value associated with the instance 282 * immediately after the increment operation. This method is not thread safe. 283 * 284 * @return The value associated with the instance after it is incremented. 285 * @since 3.5 286 */ 287 public short incrementAndGet() { 288 value++; 289 return value; 290 } 291 292 /** 293 * Returns the value of this MutableShort 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 return value; 300 } 301 302 /** 303 * Returns the value of this MutableShort 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 return value; 310 } 311 312 /** 313 * Sets the value from any Number instance. 314 * 315 * @param value The value to set, not null. 316 * @throws NullPointerException Thrown if the object is null. 317 */ 318 @Override 319 public void setValue(final Number value) { 320 this.value = value.shortValue(); 321 } 322 323 /** 324 * Sets the value. 325 * 326 * @param value The value to set 327 */ 328 public void setValue(final short value) { 329 this.value = value; 330 } 331 332 // byteValue relies on Number implementation 333 /** 334 * Returns the value of this MutableShort as a short. 335 * 336 * @return The numeric value represented by this object after conversion to type short. 337 */ 338 @Override 339 public short shortValue() { 340 return value; 341 } 342 343 /** 344 * Subtracts a value from the value of this instance. 345 * 346 * @param operand The value to subtract, not null. 347 * @throws NullPointerException Thrown if the object is null. 348 * @since 2.2 349 */ 350 public void subtract(final Number operand) { 351 this.value -= operand.shortValue(); 352 } 353 354 /** 355 * Subtracts a value from the value of this instance. 356 * 357 * @param operand The value to subtract, not null. 358 * @since 2.2 359 */ 360 public void subtract(final short operand) { 361 this.value -= operand; 362 } 363 364 /** 365 * Gets this mutable as an instance of Short. 366 * 367 * @return A Short instance containing the value from this mutable, never null. 368 */ 369 public Short toShort() { 370 return Short.valueOf(shortValue()); 371 } 372 373 /** 374 * Returns the String value of this mutable. 375 * 376 * @return The mutable value as a string. 377 */ 378 @Override 379 public String toString() { 380 return String.valueOf(value); 381 } 382 383}