This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
/
BaseUInt32Value.java
334 lines (292 loc) · 8.87 KB
/
BaseUInt32Value.java
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.apache.tuweni.units.bigints;
import static java.util.Objects.requireNonNull;
import org.apache.tuweni.bytes.Bytes;
import java.math.BigInteger;
import java.util.function.Function;
/**
* Base class for {@link UInt32Value}.
*
* <p>
* This class is abstract as it is not meant to be used directly, but it has no abstract methods. As mentioned in
* {@link UInt32Value}, this is used to create strongly-typed type aliases of {@link UInt32}. In other words, this allow
* to "tag" numbers with the unit of what they represent for the type-system, which can help clarity, but also forbid
* mixing numbers that are mean to be of different units (the strongly-typed part).
*
* <p>
* This class implements {@link UInt32Value}, but also adds a few operations that take a {@link UInt32} directly, for
* instance {@link #multiply(UInt32)}. The rational is that multiplying a given quantity of something by a "raw" number
* is always meaningful, and return a new quantity of the same thing.
*
* @param <T> The concrete type of the value.
*/
public abstract class BaseUInt32Value<T extends UInt32Value<T>> implements UInt32Value<T> {
private final UInt32 value;
private final Function<UInt32, T> ctor;
/**
* @param value The value to instantiate this {@code UInt32Value} with.
* @param ctor A constructor for the concrete type.
*/
protected BaseUInt32Value(UInt32 value, Function<UInt32, T> ctor) {
requireNonNull(value);
requireNonNull(ctor);
this.value = value;
this.ctor = ctor;
}
/**
* @param value An unsigned value to instantiate this {@code UInt32Value} with.
* @param ctor A constructor for the concrete type.
*/
protected BaseUInt32Value(int value, Function<UInt32, T> ctor) {
requireNonNull(ctor);
this.value = UInt32.valueOf(value);
this.ctor = ctor;
}
/**
* @param value An unsigned value to instantiate this {@code UInt32Value} with.
* @param ctor A constructor for the concrete type.
*/
protected BaseUInt32Value(BigInteger value, Function<UInt32, T> ctor) {
requireNonNull(value);
requireNonNull(ctor);
this.value = UInt32.valueOf(value);
this.ctor = ctor;
}
/**
* Return a copy of this value, or itself if immutable.
*
* <p>
* The default implementation of this method returns a copy using the constructor for the concrete type and the bytes
* returned from {@link #toBytes()}. Most implementations will want to override this method to instead return
* {@code this}.
*
* @return A copy of this value, or itself if immutable.
*/
protected T copy() {
return ctor.apply(value);
}
/**
* Return the zero value for this type.
*
* <p>
* The default implementation of this method returns a value obtained from calling the concrete type constructor with
* an argument of {@link UInt32#ZERO}. Most implementations will want to override this method to instead return a
* static constant.
*
* @return The zero value for this type.
*/
protected T zero() {
return ctor.apply(UInt32.ZERO);
}
@Override
public T add(T value) {
return add(value.toUInt32());
}
/**
* Returns a value that is {@code (this + value)}.
*
* @param value The amount to be added to this value.
* @return {@code this + value}
*/
public T add(UInt32 value) {
if (value.isZero()) {
return copy();
}
return ctor.apply(this.value.add(value));
}
@Override
public T add(int value) {
if (value == 0) {
return copy();
}
return ctor.apply(this.value.add(value));
}
@Override
public T addMod(T value, UInt32 modulus) {
return addMod(value.toUInt32(), modulus);
}
/**
* Returns a value equivalent to {@code ((this + value) mod modulus)}.
*
* @param value The amount to be added to this value.
* @param modulus The modulus.
* @return {@code (this + value) mod modulus}
* @throws ArithmeticException {@code modulus} == 0.
*/
public T addMod(UInt32 value, UInt32 modulus) {
return ctor.apply(this.value.addMod(value, modulus));
}
@Override
public T addMod(long value, UInt32 modulus) {
return ctor.apply(this.value.addMod(value, modulus));
}
@Override
public T addMod(long value, long modulus) {
return ctor.apply(this.value.addMod(value, modulus));
}
@Override
public T subtract(T value) {
return subtract(value.toUInt32());
}
/**
* Returns a value that is {@code (this - value)}.
*
* @param value The amount to be subtracted from this value.
* @return {@code this - value}
*/
public T subtract(UInt32 value) {
if (value.isZero()) {
return copy();
}
return ctor.apply(this.value.subtract(value));
}
@Override
public T subtract(int value) {
if (value == 0) {
return copy();
}
return ctor.apply(this.value.subtract(value));
}
@Override
public T multiply(T value) {
return multiply(value.toUInt32());
}
/**
* Returns a value that is {@code (this * value)}.
*
* @param value The amount to multiply this value by.
* @return {@code this * value}
*/
public T multiply(UInt32 value) {
if (isZero() || value.isZero()) {
return zero();
}
if (value.equals(UInt32.ONE)) {
return copy();
}
return ctor.apply(this.value.multiply(value));
}
@Override
public T multiply(int value) {
if (value == 0 || isZero()) {
return zero();
}
if (value == 1) {
return copy();
}
return ctor.apply(this.value.multiply(value));
}
@Override
public T multiplyMod(T value, UInt32 modulus) {
return multiplyMod(value.toUInt32(), modulus);
}
/**
* Returns a value that is {@code ((this * value) mod modulus)}.
*
* @param value The amount to multiply this value by.
* @param modulus The modulus.
* @return {@code (this * value) mod modulus}
* @throws ArithmeticException {@code value} < 0 or {@code modulus} == 0.
*/
public T multiplyMod(UInt32 value, UInt32 modulus) {
return ctor.apply(this.value.multiplyMod(value, modulus));
}
@Override
public T multiplyMod(int value, UInt32 modulus) {
return ctor.apply(this.value.multiplyMod(value, modulus));
}
@Override
public T multiplyMod(int value, int modulus) {
return ctor.apply(this.value.multiplyMod(value, modulus));
}
@Override
public T divide(T value) {
return divide(value.toUInt32());
}
/**
* Returns a value that is {@code (this / value)}.
*
* @param value The amount to divide this value by.
* @return {@code this / value}
* @throws ArithmeticException {@code value} == 0.
*/
public T divide(UInt32 value) {
return ctor.apply(this.value.divide(value));
}
@Override
public T divide(int value) {
return ctor.apply(this.value.divide(value));
}
@Override
public T pow(UInt32 exponent) {
return ctor.apply(this.value.pow(exponent));
}
@Override
public T pow(long exponent) {
return ctor.apply(this.value.pow(exponent));
}
@Override
public T mod(UInt32 modulus) {
return ctor.apply(this.value.mod(modulus));
}
@Override
public T mod(int modulus) {
return ctor.apply(this.value.mod(modulus));
}
@Override
public int compareTo(T other) {
return compareTo(other.toUInt32());
}
/**
* Compare two {@link UInt32} values.
*
* @param other The value to compare to.
* @return A negative integer, zero, or a positive integer as this value is less than, equal to, or greater than the
* specified value.
*/
public int compareTo(UInt32 other) {
return this.value.compareTo(other);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof UInt32Value)) {
return false;
}
UInt32Value<?> other = (UInt32Value<?>) obj;
return this.value.equals(other.toUInt32());
}
@Override
public int hashCode() {
return value.hashCode();
}
@Override
public String toString() {
return value.toString();
}
@Override
public UInt32 toUInt32() {
return value;
}
@Override
public Bytes toBytes() {
return value.toBytes();
}
@Override
public Bytes toMinimalBytes() {
return value.toMinimalBytes();
}
}