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 019/** 020 * A mutable <code>short</code> wrapper. 021 * <p> 022 * Note that as MutableShort does not extend Short, it is not treated by String.format as a Short parameter. 023 * 024 * @see Short 025 * @since 2.1 026 * @version $Id: MutableShort.java 1436770 2013-01-22 07:09:45Z ggregory $ 027 */ 028public class MutableShort extends Number implements Comparable<MutableShort>, Mutable<Number> { 029 030 /** 031 * Required for serialization support. 032 * 033 * @see java.io.Serializable 034 */ 035 private static final long serialVersionUID = -2135791679L; 036 037 /** The mutable value. */ 038 private short value; 039 040 /** 041 * Constructs a new MutableShort with the default value of zero. 042 */ 043 public MutableShort() { 044 super(); 045 } 046 047 /** 048 * Constructs a new MutableShort with the specified value. 049 * 050 * @param value the initial value to store 051 */ 052 public MutableShort(final short value) { 053 super(); 054 this.value = value; 055 } 056 057 /** 058 * Constructs a new MutableShort with the specified value. 059 * 060 * @param value the initial value to store, not null 061 * @throws NullPointerException if the object is null 062 */ 063 public MutableShort(final Number value) { 064 super(); 065 this.value = value.shortValue(); 066 } 067 068 /** 069 * Constructs a new MutableShort parsing the given string. 070 * 071 * @param value the string to parse, not null 072 * @throws NumberFormatException if the string cannot be parsed into a short 073 * @since 2.5 074 */ 075 public MutableShort(final String value) throws NumberFormatException { 076 super(); 077 this.value = Short.parseShort(value); 078 } 079 080 //----------------------------------------------------------------------- 081 /** 082 * Gets the value as a Short instance. 083 * 084 * @return the value as a Short, never null 085 */ 086 @Override 087 public Short getValue() { 088 return Short.valueOf(this.value); 089 } 090 091 /** 092 * Sets the value. 093 * 094 * @param value the value to set 095 */ 096 public void setValue(final short value) { 097 this.value = value; 098 } 099 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 this.value = value.shortValue(); 109 } 110 111 //----------------------------------------------------------------------- 112 /** 113 * Increments the value. 114 * 115 * @since Commons Lang 2.2 116 */ 117 public void increment() { 118 value++; 119 } 120 121 /** 122 * Decrements the value. 123 * 124 * @since Commons Lang 2.2 125 */ 126 public void decrement() { 127 value--; 128 } 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 short operand) { 138 this.value += operand; 139 } 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 this.value += operand.shortValue(); 150 } 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 short operand) { 159 this.value -= operand; 160 } 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 this.value -= operand.shortValue(); 171 } 172 173 //----------------------------------------------------------------------- 174 // byteValue relies on Number implementation 175 /** 176 * Returns the value of this MutableShort as a short. 177 * 178 * @return the numeric value represented by this object after conversion to type short. 179 */ 180 @Override 181 public short shortValue() { 182 return value; 183 } 184 185 /** 186 * Returns the value of this MutableShort as an int. 187 * 188 * @return the numeric value represented by this object after conversion to type int. 189 */ 190 @Override 191 public int intValue() { 192 return value; 193 } 194 195 /** 196 * Returns the value of this MutableShort as a long. 197 * 198 * @return the numeric value represented by this object after conversion to type long. 199 */ 200 @Override 201 public long longValue() { 202 return value; 203 } 204 205 /** 206 * Returns the value of this MutableShort as a float. 207 * 208 * @return the numeric value represented by this object after conversion to type float. 209 */ 210 @Override 211 public float floatValue() { 212 return value; 213 } 214 215 /** 216 * Returns the value of this MutableShort as a double. 217 * 218 * @return the numeric value represented by this object after conversion to type double. 219 */ 220 @Override 221 public double doubleValue() { 222 return value; 223 } 224 225 //----------------------------------------------------------------------- 226 /** 227 * Gets this mutable as an instance of Short. 228 * 229 * @return a Short instance containing the value from this mutable, never null 230 */ 231 public Short toShort() { 232 return Short.valueOf(shortValue()); 233 } 234 235 //----------------------------------------------------------------------- 236 /** 237 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument 238 * is not <code>null</code> and is a <code>MutableShort</code> object that contains the same <code>short</code> 239 * value as this object. 240 * 241 * @param obj the object to compare with, null returns false 242 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 243 */ 244 @Override 245 public boolean equals(final Object obj) { 246 if (obj instanceof MutableShort) { 247 return value == ((MutableShort) obj).shortValue(); 248 } 249 return false; 250 } 251 252 /** 253 * Returns a suitable hash code for this mutable. 254 * 255 * @return a suitable hash code 256 */ 257 @Override 258 public int hashCode() { 259 return value; 260 } 261 262 //----------------------------------------------------------------------- 263 /** 264 * Compares this mutable to another in ascending order. 265 * 266 * @param other the other mutable to compare to, not null 267 * @return negative if this is less, zero if equal, positive if greater 268 */ 269 @Override 270 public int compareTo(final MutableShort other) { 271 final short anotherVal = other.value; 272 return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1); 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 return String.valueOf(value); 284 } 285 286}