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 */ 017 package 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 1160571 2011-08-23 07:36:08Z bayard $ 027 */ 028 public 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(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(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(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 public Short getValue() { 087 return Short.valueOf(this.value); 088 } 089 090 /** 091 * Sets the value. 092 * 093 * @param value the value to set 094 */ 095 public void setValue(short value) { 096 this.value = value; 097 } 098 099 /** 100 * Sets the value from any Number instance. 101 * 102 * @param value the value to set, not null 103 * @throws NullPointerException if the object is null 104 */ 105 public void setValue(Number value) { 106 this.value = value.shortValue(); 107 } 108 109 //----------------------------------------------------------------------- 110 /** 111 * Increments the value. 112 * 113 * @since Commons Lang 2.2 114 */ 115 public void increment() { 116 value++; 117 } 118 119 /** 120 * Decrements the value. 121 * 122 * @since Commons Lang 2.2 123 */ 124 public void decrement() { 125 value--; 126 } 127 128 //----------------------------------------------------------------------- 129 /** 130 * Adds a value to the value of this instance. 131 * 132 * @param operand the value to add, not null 133 * @since Commons Lang 2.2 134 */ 135 public void add(short operand) { 136 this.value += operand; 137 } 138 139 /** 140 * Adds a value to the value of this instance. 141 * 142 * @param operand the value to add, not null 143 * @throws NullPointerException if the object is null 144 * @since Commons Lang 2.2 145 */ 146 public void add(Number operand) { 147 this.value += operand.shortValue(); 148 } 149 150 /** 151 * Subtracts a value from the value of this instance. 152 * 153 * @param operand the value to subtract, not null 154 * @since Commons Lang 2.2 155 */ 156 public void subtract(short operand) { 157 this.value -= operand; 158 } 159 160 /** 161 * Subtracts a value from the value of this instance. 162 * 163 * @param operand the value to subtract, not null 164 * @throws NullPointerException if the object is null 165 * @since Commons Lang 2.2 166 */ 167 public void subtract(Number operand) { 168 this.value -= operand.shortValue(); 169 } 170 171 //----------------------------------------------------------------------- 172 // byteValue relies on Number implementation 173 /** 174 * Returns the value of this MutableShort as a short. 175 * 176 * @return the numeric value represented by this object after conversion to type short. 177 */ 178 @Override 179 public short shortValue() { 180 return value; 181 } 182 183 /** 184 * Returns the value of this MutableShort as an int. 185 * 186 * @return the numeric value represented by this object after conversion to type int. 187 */ 188 @Override 189 public int intValue() { 190 return value; 191 } 192 193 /** 194 * Returns the value of this MutableShort as a long. 195 * 196 * @return the numeric value represented by this object after conversion to type long. 197 */ 198 @Override 199 public long longValue() { 200 return value; 201 } 202 203 /** 204 * Returns the value of this MutableShort as a float. 205 * 206 * @return the numeric value represented by this object after conversion to type float. 207 */ 208 @Override 209 public float floatValue() { 210 return value; 211 } 212 213 /** 214 * Returns the value of this MutableShort as a double. 215 * 216 * @return the numeric value represented by this object after conversion to type double. 217 */ 218 @Override 219 public double doubleValue() { 220 return value; 221 } 222 223 //----------------------------------------------------------------------- 224 /** 225 * Gets this mutable as an instance of Short. 226 * 227 * @return a Short instance containing the value from this mutable, never null 228 */ 229 public Short toShort() { 230 return Short.valueOf(shortValue()); 231 } 232 233 //----------------------------------------------------------------------- 234 /** 235 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument 236 * is not <code>null</code> and is a <code>MutableShort</code> object that contains the same <code>short</code> 237 * value as this object. 238 * 239 * @param obj the object to compare with, null returns false 240 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 241 */ 242 @Override 243 public boolean equals(Object obj) { 244 if (obj instanceof MutableShort) { 245 return value == ((MutableShort) obj).shortValue(); 246 } 247 return false; 248 } 249 250 /** 251 * Returns a suitable hash code for this mutable. 252 * 253 * @return a suitable hash code 254 */ 255 @Override 256 public int hashCode() { 257 return value; 258 } 259 260 //----------------------------------------------------------------------- 261 /** 262 * Compares this mutable to another in ascending order. 263 * 264 * @param other the other mutable to compare to, not null 265 * @return negative if this is less, zero if equal, positive if greater 266 */ 267 public int compareTo(MutableShort other) { 268 short anotherVal = other.value; 269 return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1); 270 } 271 272 //----------------------------------------------------------------------- 273 /** 274 * Returns the String value of this mutable. 275 * 276 * @return the mutable value as a string 277 */ 278 @Override 279 public String toString() { 280 return String.valueOf(value); 281 } 282 283 }