RuneHive-Game
Loading...
Searching...
No Matches
MutableNumber.java
Go to the documentation of this file.
1package com.runehive.util;
2
3import java.util.concurrent.atomic.AtomicInteger;
4
5/**
6 * The container class that contains functions to simplify the modification of a
7 * number.
8 * <p>
9 * <p>
10 * This class is similar in functionality to {@link AtomicInteger} but does not
11 * support atomic operations, and therefore should not be used across multiple
12 * threads.
13 *
14 * @author lare96 <http://github.com/lare96>
15 */
16public final class MutableNumber extends Number implements Comparable<MutableNumber> {
17
18 /**
19 * The constant serial version UID for serialization.
20 */
21 private static final long serialVersionUID = -7475363158492415879L;
22
23 /**
24 * The value present within this counter.
25 */
26 private int value;
27
28 /**
29 * Creates a new {@link MutableNumber} with {@code value}.
30 *
31 * @param value
32 * the value present within this counter.
33 */
34 public MutableNumber(int value) {
35 this.value = value;
36 }
37
38 /**
39 * Creates a new {@link MutableNumber} with a value of {@code 0}.
40 */
41 public MutableNumber() {
42 this(0);
43 }
44
45 @Override
46 public String toString() {
47 return Integer.toString(value);
48 }
49
50 @Override
51 public int hashCode() {
52 return Integer.hashCode(value);
53 }
54
55 @Override
56 public boolean equals(Object obj) {
57 if (this == obj)
58 return true;
59 if (obj == null)
60 return false;
61 if (!(obj instanceof MutableNumber))
62 return false;
63 MutableNumber other = (MutableNumber) obj;
64 return value == other.value;
65 }
66
67 @Override
68 public int compareTo(MutableNumber o) {
69 return Integer.compare(value, o.value);
70 }
71
72 /**
73 * {@inheritDoc}
74 * <p>
75 * This function equates to the {@link MutableNumber#get()} function.
76 */
77 @Override
78 public int intValue() {
79 return value;
80 }
81
82 @Override
83 public long longValue() {
84 return (long) value;
85 }
86
87 @Override
88 public float floatValue() {
89 return (float) value;
90 }
91
92 @Override
93 public double doubleValue() {
94 return (double) value;
95 }
96
97 /**
98 * Returns the value within this counter and then increments it by
99 * {@code amount} to a maximum of {@code maximum}.
100 *
101 * @param amount
102 * the amount to append it by.
103 * @param maximum
104 * the maximum amount it will be incremented to.
105 * @return the value before it is incremented.
106 */
107 public int getAndIncrement(int amount, int maximum) {
108 int val = value;
109 value += amount;
110 if (value > maximum)
111 value = maximum;
112 return val;
113 }
114
115 /**
116 * Returns the value within this counter and then increments it by
117 * {@code amount}.
118 *
119 * @param amount
120 * the amount to append it by.
121 * @return the value before it is incremented.
122 */
123 public int getAndIncrement(int amount) {
124 return getAndIncrement(amount, Integer.MAX_VALUE);
125 }
126
127 /**
128 * Returns the value within this counter and then increments it by an amount
129 * of {@code 1}.
130 *
131 * @return the value before it is incremented.
132 */
133 public int getAndIncrement() {
134 return getAndIncrement(1);
135 }
136
137 /**
138 * Increments the value within this counter by {@code amount} to a maximum
139 * of {@code maximum} and then returns it.
140 *
141 * @param amount
142 * the amount to append it by.
143 * @param maximum
144 * the maximum amount it will be incremented to.
145 * @return the value after it is incremented.
146 */
147 public int incrementAndGet(int amount, int maximum) {
148 value += amount;
149 if (value > maximum)
150 value = maximum;
151 return value;
152 }
153
154 /**
155 * Increments the value within this counter by {@code amount} and then
156 * returns it.
157 *
158 * @param amount
159 * the amount to append it by.
160 * @return the value after it is incremented.
161 */
162 public int incrementAndGet(int amount) {
163 return incrementAndGet(amount, Integer.MAX_VALUE);
164 }
165
166 /**
167 * Increments the value within this counter by {@code 1} and then returns
168 * it.
169 *
170 * @return the value after it is incremented.
171 */
172 public int incrementAndGet() {
173 return incrementAndGet(1);
174 }
175
176 /**
177 * Increments the value within this counter by {@code 1} and then returns
178 * it.
179 *
180 * @return the value after it is incremented.
181 */
184 return this;
185 }
186
187 /**
188 * Returns the value within this counter and then decrements it by
189 * {@code amount} to a minimum of {@code minimum}.
190 *
191 * @param amount
192 * the amount to decrement it by.
193 * @param minimum
194 * the minimum amount it will be decremented to.
195 * @return the value before it is decremented.
196 */
197 public int getAndDecrement(int amount, int minimum) {
198 int val = value;
199 value -= amount;
200 if (value < minimum)
201 value = minimum;
202 return val;
203 }
204
205 /**
206 * Returns the value within this counter and then decrements it by
207 * {@code amount}.
208 *
209 * @param amount
210 * the amount to decrement it by.
211 * @return the value before it is decremented.
212 */
213 public int getAndDecrement(int amount) {
214 return getAndDecrement(amount, Integer.MIN_VALUE);
215 }
216
217 /**
218 * Returns the value within this counter and then decrements it by an amount
219 * of {@code 1}.
220 *
221 * @return the value before it is decremented.
222 */
223 public int getAndDecrement() {
224 return getAndDecrement(1);
225 }
226
227 /**
228 * Decrements the value within this counter by {@code amount} to a minimum
229 * of {@code minimum} and then returns it.
230 *
231 * @param amount
232 * the amount to decrement it by.
233 * @param minimum
234 * the minimum amount it will be decremented to.
235 * @return the value after it is decremented.
236 */
237 public int decrementAndGet(int amount, int minimum) {
238 value -= amount;
239 if (value < minimum)
240 value = minimum;
241 return value;
242 }
243
244 /**
245 * Decrements the value within this counter by {@code amount} and then
246 * returns it.
247 *
248 * @param amount
249 * the amount to decrement it by.
250 * @return the value after it is decremented.
251 */
252 public int decrementAndGet(int amount) {
253 return decrementAndGet(amount, Integer.MIN_VALUE);
254 }
255
256 /**
257 * Decrements the value within this counter by {@code 1} and then returns
258 * it.
259 *
260 * @return the value after it is decremented.
261 */
262 public int decrementAndGet() {
263 return decrementAndGet(1);
264 }
265
266 /**
267 * Gets the value present within this counter. This function equates to the
268 * inherited {@link MutableNumber#intValue()} function.
269 *
270 * @return the value present.
271 */
272 public int get() {
273 return value;
274 }
275
276 /**
277 * Sets the value within this container to {@code rarity}.
278 *
279 * @param value
280 * the new value to set.
281 */
282 public void set(int value) {
283 this.value = value;
284 }
285}
int getAndDecrement(int amount, int minimum)
Returns the value within this counter and then decrements it by amount to a minimum of minimum.
int getAndDecrement(int amount)
Returns the value within this counter and then decrements it by amount.
int value
The value present within this counter.
int incrementAndGet(int amount, int maximum)
Increments the value within this counter by amount to a maximum of maximum and then returns it.
int getAndDecrement()
Returns the value within this counter and then decrements it by an amount of 1.
int getAndIncrement(int amount, int maximum)
Returns the value within this counter and then increments it by amount to a maximum of maximum.
MutableNumber increment()
Increments the value within this counter by 1 and then returns it.
int incrementAndGet()
Increments the value within this counter by 1 and then returns it.
int getAndIncrement(int amount)
Returns the value within this counter and then increments it by amount.
int decrementAndGet(int amount, int minimum)
Decrements the value within this counter by amount to a minimum of minimum and then returns it.
int decrementAndGet(int amount)
Decrements the value within this counter by amount and then returns it.
static final long serialVersionUID
The constant serial version UID for serialization.
MutableNumber(int value)
Creates a new MutableNumber with value.
int incrementAndGet(int amount)
Increments the value within this counter by amount and then returns it.
int getAndIncrement()
Returns the value within this counter and then increments it by an amount of 1.
int decrementAndGet()
Decrements the value within this counter by 1 and then returns it.
MutableNumber()
Creates a new MutableNumber with a value of 0.