Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1059caeed6e455f7bf231cb762e0015bcfb9b07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*******************************************************************************
 * Copyright (c) 2007, 2015 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.reference;

import java.io.Serializable;
import org.eclipse.jpt.common.utility.internal.BitTools;
import org.eclipse.jpt.common.utility.predicate.IntPredicate;
import org.eclipse.jpt.common.utility.reference.IntReference;
import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;

/**
 * This class can be used wherever a mutable integer object is needed.
 * It is a cross between an <code>int</code> and an {@link Integer}.
 * It can be stored in a standard container (e.g. {@link java.util.Collection})
 * but can be modified. It is also useful passing a value that can be changed
 * by the recipient.
 * 
 * @see SynchronizedInt
 */
public final class SimpleIntReference
	implements ModifiableIntReference, Cloneable, Serializable
{
	/** Backing <code>int</code>. */
	private volatile int value = 0;

	private static final long serialVersionUID = 1L;


	// ********** constructors **********

	/**
	 * Construct an <code>int</code> reference with the specified initial value.
	 */
	public SimpleIntReference(int count) {
		super();
		this.value = count;
	}

	/**
	 * Construct an <code>int</code> reference with an initial value of zero.
	 */
	public SimpleIntReference() {
		this(0);
	}


	// ********** IntReference **********

	public int getValue() {
		return this.value;
	}

	public boolean equals(int i) {
		return this.value == i;
	}

	public boolean notEqual(int i) {
		return this.value != i;
	}

	public boolean isZero() {
		return this.value == 0;
	}

	public boolean isNotZero() {
		return this.value != 0;
	}

	public boolean isGreaterThan(int i) {
		return this.value > i;
	}

	public boolean isGreaterThanOrEqual(int i) {
		return this.value >= i;
	}

	public boolean isLessThan(int i) {
		return this.value < i;
	}

	public boolean isLessThanOrEqual(int i) {
		return this.value <= i;
	}

	public boolean isPositive() {
		return this.value > 0;
	}

	public boolean isNotPositive() {
		return this.value <= 0;
	}

	public boolean isNegative() {
		return this.value < 0;
	}

	public boolean isNotNegative() {
		return this.value >= 0;
	}

	public boolean isMemberOf(IntPredicate predicate) {
		return predicate.evaluate(this.value);
	}

	public boolean isNotMemberOf(IntPredicate predicate) {
		return ! predicate.evaluate(this.value);
	}


	// ********** ModifiableIntReference **********

	public int setValue(int value) {
		int old = this.value;
		this.value = value;
		return old;
	}

	public int setZero() {
		return this.setValue(0);
	}

	public int increment() {
		return ++this.value;
	}

	public int incrementExact() {
		this.value = Math.incrementExact(this.value);
		return this.value;
	}

	public int decrement() {
		return --this.value;
	}

	public int decrementExact() {
		this.value = Math.decrementExact(this.value);
		return this.value;
	}

	public int halve() {
		this.value = BitTools.half(this.value);
		return this.value;
	}

	public int twice() {
		this.value = BitTools.twice(this.value);
		return this.value;
	}

	public int twiceExact() {
		this.value = Math.multiplyExact(this.value, 2);
		return this.value;
	}

	public int abs() {
		this.value = Math.abs(this.value);
		return this.value;
	}

	public int negate() {
		this.value = -this.value;
		return this.value;
	}

	public int negateExact() {
		this.value = Math.negateExact(this.value);
		return this.value;
	}

	public int add(int i) {
		this.value += i;
		return this.value;
	}

	public int addExact(int i) {
		this.value = Math.addExact(this.value, i);
		return this.value;
	}

	public int subtract(int i) {
		this.value -= i;
		return this.value;
	}

	public int subtractExact(int i) {
		this.value = Math.subtractExact(this.value, i);
		return this.value;
	}

	public int multiply(int i) {
		this.value = this.value * i;
		return this.value;
	}

	public int multiplyExact(int i) {
		this.value = Math.multiplyExact(this.value, i);
		return this.value;
	}

	public int divide(int i) {
		this.value = this.value / i;
		return this.value;
	}

	public int floorDivide(int i) {
		this.value = Math.floorDiv(this.value, i);
		return this.value;
	}

	public int remainder(int i) {
		this.value = this.value % i;
		return this.value;
	}

	public int floorRemainder(int i) {
		this.value = Math.floorMod(this.value, i);
		return this.value;
	}

	public int min(int i) {
		this.value = Math.min(this.value, i);
		return this.value;
	}

	public int max(int i) {
		this.value = Math.max(this.value, i);
		return this.value;
	}

	public boolean commit(int newValue, int expectedValue) {
		if (this.value == expectedValue) {
			this.value = newValue;
			return true;
		}
		return false;
	}

	public int swap(ModifiableIntReference other) {
		if (other == this) {
			return this.value;
		}
		int otherValue = other.getValue();
		if (otherValue == this.value) {
			return this.value;
		}
		other.setValue(this.value);
		this.value = otherValue;
		return otherValue;
	}


	// ********** Comparable **********

	public int compareTo(IntReference other) {
		int otherValue = other.getValue();
		return (this.value < otherValue) ? -1 : ((this.value == otherValue) ? 0 : 1);
	}


	// ********** standard methods **********

	@Override
	public SimpleIntReference clone() {
		try {
			return (SimpleIntReference) super.clone();
		} catch (CloneNotSupportedException ex) {
			throw new InternalError();
		}
	}

	/**
	 * Object identity is critical to int references.
	 * There is no reason for two different int references to be
	 * <em>equal</em>.
	 * 
	 * @see #equals(int)
	 */
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	/**
	 * @see #equals(Object)
	 */
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	@Override
	public String toString() {
		return '[' + String.valueOf(this.value) + ']';
	}
}

Back to the top