forked from diffplug/durian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Box.java
618 lines (523 loc) · 14 KB
/
Box.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright 2016 DiffPlug
*
* Licensed 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 com.diffplug.common.base;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.LongConsumer;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
/**
* Provides get/set access to a mutable non-null value.
*/
public interface Box<T> extends Supplier<T>, Consumer<T> {
/** Sets the value which will later be returned by get(). */
void set(T value);
/**
* Delegates to set().
*
* @deprecated Provided to satisfy the {@link Consumer} interface; use {@link #set} instead.
*/
@Deprecated
default void accept(T value) {
set(value);
}
/**
* Performs a set() on the result of a get().
*
* Some implementations may provide atomic semantics,
* but it's not required.
*/
default T modify(Function<? super T, ? extends T> mutator) {
T modified = mutator.apply(get());
set(modified);
return modified;
}
/**
* Maps one {@code Box} to another {@code Box}, preserving any
* {@link #modify(Function)} guarantees of the underlying Box.
*/
default <R> Box<R> map(Converter<T, R> converter) {
return new Mapped<>(this, converter);
}
static final class Mapped<T, R> implements Box<R> {
private final Box<T> delegate;
private final Converter<T, R> converter;
public Mapped(Box<T> delegate,
Converter<T, R> converter) {
this.delegate = delegate;
this.converter = converter;
}
@Override
public R get() {
return converter.convertNonNull(delegate.get());
}
@Override
public void set(R value) {
delegate.set(converter.revertNonNull(value));
}
/** Shortcut for doing a set() on the result of a get(). */
@Override
public R modify(Function<? super R, ? extends R> mutator) {
Box.Nullable<R> result = Box.Nullable.of(null);
delegate.modify(input -> {
R unmappedResult = mutator.apply(converter.convertNonNull(input));
result.set(unmappedResult);
return converter.revertNonNull(unmappedResult);
});
return result.get();
}
@Override
public String toString() {
return "[" + delegate + " mapped to " + get() + " by " + converter + "]";
}
}
/**
* Creates a `Box` holding the given value in a `volatile` field.
*
* Every call to {@link #set(Object)} confirms that the argument
* is actually non-null, and the value is stored in a volatile variable.
*/
public static <T> Box<T> ofVolatile(T value) {
return new Volatile<>(value);
}
static final class Volatile<T> implements Box<T> {
private volatile T obj;
private Volatile(T init) {
set(init);
}
@Override
public T get() {
return obj;
}
@Override
public void set(T obj) {
this.obj = Objects.requireNonNull(obj);
}
@Override
public String toString() {
return "Box.ofVolatile[" + get() + "]";
}
}
/**
* Creates a `Box` holding the given value in a non-`volatile` field.
*
* The value is stored in standard non-volatile
* field, and non-null-ness is not checked on
* every call to set.
*/
public static <T> Box<T> of(T value) {
return new Default<>(value);
}
static final class Default<T> implements Box<T> {
private T obj;
private Default(T init) {
set(init);
}
@Override
public T get() {
return obj;
}
@Override
public void set(T obj) {
this.obj = Objects.requireNonNull(obj);
}
@Override
public String toString() {
return "Box.of[" + get() + "]";
}
}
/** Creates a `Box` from a `Supplier` and a `Consumer`. */
public static <T> Box<T> from(Supplier<T> getter, Consumer<T> setter) {
Objects.requireNonNull(getter);
Objects.requireNonNull(setter);
return new Box<T>() {
@Override
public T get() {
return Objects.requireNonNull(getter.get());
}
@Override
public void set(T value) {
setter.accept(Objects.requireNonNull(value));
}
@Override
public String toString() {
return "Box.from[" + get() + "]";
}
};
}
/** Provides get/set access to a mutable nullable value. */
public interface Nullable<T> extends Supplier<T>, Consumer<T> {
/** Sets the value which will later be returned by get(). */
void set(@javax.annotation.Nullable T value);
/**
* Delegates to set().
*
* @deprecated Provided to satisfy the {@code Function} interface; use {@link #set} instead.
*/
@Deprecated
default void accept(@javax.annotation.Nullable T value) {
set(value);
}
/** Shortcut for doing a set() on the result of a get(). */
default T modify(Function<? super T, ? extends T> mutator) {
T modified = mutator.apply(get());
set(modified);
return modified;
}
/**
* Maps one {@code Box} to another {@code Box}, preserving any
* {@link #modify(Function)} guarantees of the underlying Box.
*/
default <R> Nullable<R> map(ConverterNullable<T, R> converter) {
return new Nullable.Mapped<>(this, converter);
}
static final class Mapped<T, R> implements Nullable<R> {
private final Nullable<T> delegate;
private final ConverterNullable<T, R> converter;
public Mapped(Nullable<T> delegate, ConverterNullable<T, R> converter) {
this.delegate = delegate;
this.converter = converter;
}
@Override
public R get() {
return converter.convert(delegate.get());
}
@Override
public void set(R value) {
delegate.set(converter.revert(value));
}
/** Shortcut for doing a set() on the result of a get(). */
@Override
public R modify(Function<? super R, ? extends R> mutator) {
Box.Nullable<R> result = Box.Nullable.of(null);
delegate.modify(input -> {
R unmappedResult = mutator.apply(converter.convert(input));
result.set(unmappedResult);
return converter.revert(unmappedResult);
});
return result.get();
}
@Override
public String toString() {
return "[" + delegate + " mapped to " + get() + " by " + converter + "]";
}
}
/** Creates a `Box.Nullable` holding the given possibly-null value in a `volatile` field. */
public static <T> Nullable<T> ofVolatile(@javax.annotation.Nullable T init) {
return new Volatile<>(init);
}
/** Creates a `Box.Nullable` holding a null value in a `volatile` field. */
public static <T> Nullable<T> ofVolatileNull() {
return ofVolatile(null);
}
static class Volatile<T> implements Box.Nullable<T> {
private volatile T obj;
private Volatile(T init) {
set(init);
}
@Override
public T get() {
return obj;
}
@Override
public void set(@javax.annotation.Nullable T obj) {
this.obj = obj;
}
@Override
public String toString() {
return "Box.Nullable.ofVolatile[" + get() + "]";
}
}
/** Creates a `Box.Nullable` holding the given possibly-null value in a non-`volatile` field. */
public static <T> Nullable<T> of(@javax.annotation.Nullable T init) {
return new Default<>(init);
}
/** Creates a `Box.Nullable` holding null value in a non-`volatile` field. */
public static <T> Nullable<T> ofNull() {
return of(null);
}
static class Default<T> implements Box.Nullable<T> {
private T obj;
private Default(T init) {
this.obj = init;
}
@Override
public T get() {
return obj;
}
@Override
public void set(T obj) {
this.obj = obj;
}
@Override
public String toString() {
return "Box.Nullable.of[" + get() + "]";
}
}
/** Creates a `Box.Nullable` from a `Supplier` and a `Consumer`. */
public static <T> Nullable<T> from(Supplier<T> getter, Consumer<T> setter) {
Objects.requireNonNull(getter);
Objects.requireNonNull(setter);
return new Nullable<T>() {
@Override
public T get() {
return getter.get();
}
@Override
public void set(T value) {
setter.accept(value);
}
@Override
public String toString() {
return "Box.Nullable.from[" + get() + "]";
}
};
}
}
/** A `Box` for primitive doubles. */
public interface Dbl extends DoubleSupplier, DoubleConsumer, Box<Double> {
/** Sets the value which will later be returned by get(). */
void set(double value);
@Override
double getAsDouble();
/**
* Delegates to {@link #getAsDouble()}.
*
* @deprecated Provided to satisfy {@code Box<Double>}; use {@link #getAsDouble()} instead.
* */
@Override
@Deprecated
default Double get() {
return getAsDouble();
}
/**
* Delegates to {@link #set(double)}.
*
* @deprecated Provided to satisfy {@code Box<Double>}; use {@link #set(double)} instead.
*/
@Override
@Deprecated
default void set(Double value) {
set(value.doubleValue());
}
/**
* Delegates to {@link #set(double)}.
*
* @deprecated Provided to satisfy the {@link DoubleConsumer}; use {@link #set(double)} instead.
*/
@Deprecated
@Override
default void accept(double value) {
set(value);
}
/** Creates a `Box.Dbl` holding the given value in a non-`volatile` field. */
public static Dbl of(double value) {
return new Default(value);
}
static class Default implements Box.Dbl {
private double obj;
private Default(double init) {
set(init);
}
@Override
public double getAsDouble() {
return obj;
}
@Override
public void set(double obj) {
this.obj = obj;
}
@Override
public String toString() {
return "Box.Dbl.of[" + getAsDouble() + "]";
}
}
/** Creates a `Box.Dbl` from a `DoubleSupplier` and a `DoubleConsumer`. */
public static Dbl from(DoubleSupplier getter, DoubleConsumer setter) {
return new Dbl() {
@Override
public double getAsDouble() {
return getter.getAsDouble();
}
@Override
public void set(double value) {
setter.accept(value);
}
@Override
public String toString() {
return "Box.Dbl.from[" + get() + "]";
}
};
}
}
/** A `Box` for primitive ints. */
public interface Int extends IntSupplier, IntConsumer, Box<Integer> {
/** Sets the value which will later be returned by {@link #getAsInt()}. */
void set(int value);
@Override
int getAsInt();
/**
* Delegates to {@link #getAsInt()}.
*
* @deprecated Provided to satisfy {@code Box<Integer>}; use {@link #getAsInt()} instead.
* */
@Override
@Deprecated
default Integer get() {
return getAsInt();
}
/**
* Delegates to {@link #set(int)}.
*
* @deprecated Provided to satisfy {@code Box<Integer>}; use {@link #set(int)} instead.
*/
@Override
@Deprecated
default void set(Integer value) {
set(value.intValue());
}
/**
* Delegates to {@link #set}.
*
* @deprecated Provided to satisfy the {@link IntConsumer} interface; use {@link #set(int)} instead.
*/
@Deprecated
@Override
default void accept(int value) {
set(value);
}
/** Creates a `Box.Int` holding the given value in a non-`volatile` field. */
public static Int of(int value) {
return new Default(value);
}
static class Default implements Box.Int {
private int obj;
private Default(int init) {
set(init);
}
@Override
public int getAsInt() {
return obj;
}
@Override
public void set(int obj) {
this.obj = obj;
}
@Override
public String toString() {
return "Box.Int.of[" + get() + "]";
}
}
/** Creates a `Box.Int` from a `IntSupplier` and a `IntConsumer`. */
public static Int from(IntSupplier getter, IntConsumer setter) {
return new Int() {
@Override
public int getAsInt() {
return getter.getAsInt();
}
@Override
public void set(int value) {
setter.accept(value);
}
@Override
public String toString() {
return "Box.Int.from[" + get() + "]";
}
};
}
}
/** A `Box` for primitive longs. */
public interface Lng extends LongSupplier, LongConsumer, Box<Long> {
/** Sets the value which will later be returned by {@link #getAsLong()}. */
void set(long value);
@Override
long getAsLong();
/**
* Auto-boxed getter.
*
* @deprecated Provided to satisfy {@code Box<Long>} interface; use {@link #getAsLong()} instead.
* */
@Override
@Deprecated
default Long get() {
return getAsLong();
}
/**
* Delegates to {@link #set(long)}.
*
* @deprecated Provided to satisfy {@code Box<Long>} interface; use {@link #set(long)} instead.
*/
@Override
@Deprecated
default void set(Long value) {
set(value.longValue());
}
/**
* Delegates to {@link #set(long)}.
*
* @deprecated Provided to satisfy {@link LongConsumer} interface; use {@link #set(long)} instead.
*/
@Deprecated
@Override
default void accept(long value) {
set(value);
}
/** Creates a `Box.Long` holding the given value in a non-`volatile` field. */
public static Lng of(long value) {
return new Default(value);
}
static class Default implements Box.Lng {
private long obj;
private Default(long init) {
set(init);
}
@Override
public long getAsLong() {
return obj;
}
@Override
public void set(long obj) {
this.obj = obj;
}
@Override
public String toString() {
return "Box.Long.of[" + get() + "]";
}
}
/** Creates a `Box.Long` from a `LongSupplier` and a `LongConsumer`. */
public static Lng from(LongSupplier getter, LongConsumer setter) {
return new Lng() {
@Override
public long getAsLong() {
return getter.getAsLong();
}
@Override
public void set(long value) {
setter.accept(value);
}
@Override
public String toString() {
return "Box.Long.from[" + get() + "]";
}
};
}
}
}