-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpt.java
758 lines (671 loc) · 25.9 KB
/
Opt.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
import java.util.*;
import java.util.function.*;
import java.util.stream.Stream;
import static java.util.Objects.requireNonNull;
/**
* A container object which may or may not contain a non-null value.
*
* @param <T> the type of the value
* @author <a href="https://github.com/philou404">philou404</a>
* @version 1.1
*/
public sealed interface Opt<T> {
String THE_VALUE_CAN_T_BE_NULL = "The value can't be null";
String NO_VALUE_PRESENT = "No value present";
/**
* Returns an {@code Opt} with the specified non-null value.
*
* @param value the value to be present, must not be null
* @param <T> the type of the value
* @return an {@code Opt} with the value present
* @throws NullPointerException if the value is null
* @since 1.1
*/
static <T> Opt<T> of(T value) {
return new Some<>(requireNonNull(value, THE_VALUE_CAN_T_BE_NULL));
}
/**
* Flattens a stream of {@code Opt} objects to a stream of values.
*
* @param opts a stream of {@code Opt} objects
* @param <T> the type of the values
* @return a stream containing all the present values
* @throws NullPointerException if input is null
* @since 1.1
*/
static <T> Stream<T> flatten(Stream<Opt<T>> opts) {
return requireNonNull(opts, THE_VALUE_CAN_T_BE_NULL).flatMap(Opt::stream);
}
/**
* Map the provided {@code Optional} to an {@code Opt} with the specified non-null value.<br>
* <p>
* If the provided {@code Optional} is non-null, it will be mapped to an {@code Opt}
* with the contained value (if present), or {@code none} if the {@code Optional} is empty.
*
* @param value the {@code Optional} to be mapped, must not be null
* @param <T> the type of the value inside the {@code Optional}
* @return an {@code Opt} containing the value if present, or {@code Opt.none()} if the {@code Optional} is empty
* @throws NullPointerException if the {@code Optional} is null
* @since 1.1
*/
static <T> Opt<T> of(Optional<T> value) {
return requireNonNull(value, THE_VALUE_CAN_T_BE_NULL)
.map(Opt::of)
.orElseGet(Opt::none);
}
/**
* Returns an {@code Opt} describing the specified value, if non-null,
* otherwise returns an empty {@code Opt}.
*
* @param value the possibly-null value to describe
* @param <T> the type of the value
* @return an {@code Opt} with a present value if the specified value is non-null,
* otherwise an empty {@code Opt}
* @since 1.1
*/
static <T> Opt<T> ofNullable(T value) {
return (value == null) ? none() : new Some<>(value);
}
/**
* Returns an empty {@code Opt} instance.
*
* @param <T> the type of the non-existent value
* @return an empty {@code Opt}
* @since 1.1
*/
@SuppressWarnings("unchecked")
static <T> Opt<T> none() {
return (Opt<T>) None.INSTANCE;
}
/**
* Sequences a stream of {@code Opt} objects into an {@code Opt} containing a stream of values.
* If any {@code Opt} in the stream is empty, returns an empty {@code Opt}.
*
* @param opts a stream of {@code Opt} objects
* @param <T> the type of the values
* @return an {@code Opt} containing a stream of values if all are present, otherwise empty
* @throws NullPointerException if input is null
* @since 1.1
*/
static <T> Opt<Stream<T>> sequence(Stream<Opt<T>> opts) {
var list = requireNonNull(opts, THE_VALUE_CAN_T_BE_NULL).toList();
return (list.stream().allMatch(Opt::isPresent)) ? Opt.of(list.stream().map(Opt::get)) : none();
}
/**
* Flattens a nested {@code Opt} into a single level {@code Opt}.
*
* @param nested the nested {@code Opt}
* @param <T> the type of the inner value
* @return the flattened {@code Opt}
* @throws NullPointerException if input is null
* @since 1.1
*/
static <T> Opt<T> flatten(Opt<Opt<T>> nested) {
return requireNonNull(nested, THE_VALUE_CAN_T_BE_NULL).flatMap(Function.identity());
}
/**
* Returns a sequential {@code Iterator} containing the value if present,
* otherwise an empty {@code Iterator}.
*
* @return a {@code Iterator} of the value, or empty if not present
* @since 1.1
*/
Iterator<T> iterator();
/**
* Accepts a visitor to process this {@code Opt} instance.
*
* @param visitor the visitor instance
* @param <R> the type of the result produced by the visitor
* @return the result produced by the visitor
* @throws NullPointerException if input is null
* @since 1.1
*/
<R> R accept(OptVisitor<T, R> visitor);
/**
* Applies the function contained in {@code optFn} to this value if both are present.
*
* @param optFn an {@code Opt} containing a function to apply
* @param <U> the type of the result after applying the function
* @return an {@code Opt} containing the result, or empty if either is empty
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U> Opt<U> ap(Opt<Function<? super T, ? extends U>> optFn) {
return (this.isPresent() && requireNonNull(optFn, THE_VALUE_CAN_T_BE_NULL).isPresent()) ? Opt.ofNullable(optFn.get().apply(this.get())) : Opt.none();
}
/**
* Combines this {@code Opt} with another {@code Opt} using the provided function.
* If both values are present, the function is applied to produce a new result.
* Otherwise, an empty {@code Opt} is returned.
*
* @param other the other {@code Opt} to combine with
* @param zipper the function that combines both values
* @param <U> the type of the other value
* @param <R> the type of the result
* @return an {@code Opt} containing the combined result if both values are present, otherwise an empty {@code Opt}
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U, R> Opt<R> zip(Opt<U> other, BiFunction<? super T, ? super U, ? extends R> zipper) {
return (this.isPresent() && requireNonNull(other, THE_VALUE_CAN_T_BE_NULL).isPresent()) ? Opt.ofNullable(zipper.apply(this.get(), other.get())) : Opt.none();
}
/**
* If no value is present, executes the provided {@code action}.
*
* @param action the action to be executed if no value is present
* @return this {@code Opt}
* @throws NullPointerException if input is null
* @since 1.1
*/
default Opt<T> ifEmpty(Runnable action) {
if (isEmpty()) {
requireNonNull(action).run();
}
return this;
}
/**
* Converts this {@code Opt} to a {@code List}.
* If a value is present, returns a single-element list containing the value.
* Otherwise, returns an empty list.
*
* @return a list containing the value if present, otherwise an empty list.
* @since 1.1
*/
default List<T> toList() {
return isPresent() ? List.of(get()) : List.of();
}
/**
* Converts this {@code Opt} to a {@code Set}.
* If a value is present, returns a single-element set containing the value.
* Otherwise, returns an empty set.
*
* @return a set containing the value if present, otherwise an empty set.
* @since 1.1
*/
default Set<T> toSet() {
return isPresent() ? Set.of(get()) : Set.of();
}
/**
* Converts this {@code Opt} to a {@code Map} with a single key-value pair.
* If a value is present, applies the given key and value mapping functions to the value.
* Otherwise, returns an empty map.
*
* @param keyMapper a function to generate a key from the value.
* @param valueMapper a function to generate a value from the value.
* @param <K> the key type.
* @param <V> the value type.
* @return a map containing a single entry if a value is present, otherwise an empty map.
* @throws NullPointerException if the mapping functions return {@code null}.
* @throws NullPointerException if input is null
* @since 1.1
*/
default <K, V> Map<K, V> toMap(Function<T, K> keyMapper, Function<T, V> valueMapper) {
return isPresent() ? Map.of(requireNonNull(keyMapper).apply(get()), valueMapper.apply(get())) : Map.of();
}
/**
* Transforms this {@code Opt} using a provided function.
* The function receives this {@code Opt} as input and returns a new value.
*
* @param transformer the transformation function.
* @param <U> the type of the transformed value.
* @return an {@code Opt} containing the transformed value, or an empty {@code Opt} if the transformation returns {@code null}.
* @throws NullPointerException if the transformer function is {@code null}.
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U> Opt<U> transform(Function<? super Opt<T>, ? extends U> transformer) {
return Opt.ofNullable(requireNonNull(transformer).apply(this));
}
/**
* If a value is present, applies the given function and returns this {@code Opt}.
* Otherwise, does nothing.
*
* @param action the action to apply if a value is present
* @return this {@code Opt}
* @throws NullPointerException if input is null
* @since 1.3
*/
default Opt<T> andThen(Consumer<? super T> action) {
if (isPresent()) {
requireNonNull(action).accept(get());
}
return this;
}
/**
* Applies the given function if a value is present, otherwise applies the fallback function.
* This is similar to {@code flatMap()}, but it provides an alternative in case of {@code None}.
*
* @param mapper the function to apply if a value is present.
* @param fallback the function to apply if no value is present.
* @param <U> the type of the result.
* @return the result of applying either the mapper function or the fallback function.
* @throws NullPointerException if either function is {@code null}.
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U> Opt<U> flatMapOrElse(Function<? super T, Opt<U>> mapper, Supplier<Opt<U>> fallback) {
return isPresent() ? requireNonNull(mapper).apply(get()) : requireNonNull(fallback).get();
}
/**
* Returns a sequential {@code Stream} containing the value if present,
* otherwise an empty {@code Stream}.
*
* @return a {@code Stream} of the value, or empty if not present
* @since 1.1
*/
Stream<T> stream();
/**
* Returns {@code true} if there is a value present, otherwise {@code false}.
*
* @return {@code true} if a value is present, otherwise {@code false}
* @since 1.1
*/
boolean isPresent();
/**
* Returns {@code true} if there is no value present.
*
* @return {@code true} if a value is not present, otherwise {@code false}
* @since 1.1
*/
default boolean isEmpty() {
return !isPresent();
}
/**
* Returns the contained value if present.
*
* @return the non-null value held by this {@code Opt}
* @throws NoSuchElementException if there is no value present
* @since 1.1
*/
T get();
/**
* Returns the value if present, otherwise returns {@code other}.
*
* @param other the value to be returned if there is no value present
* @return the value, if present, otherwise {@code other}
* @since 1.1
*/
T orElse(T other);
/**
* Returns the value if present, otherwise invokes {@code supplier} and returns the result.
*
* @param supplier the supplier whose result is returned if no value is present
* @return the value if present, otherwise the result from {@code supplier}
* @throws NullPointerException if input is null
* @since 1.1
*/
T orElseGet(Supplier<? extends T> supplier);
/**
* Returns the result of applying {@code mapper} to the contained value if present,
* otherwise returns the result produced by {@code defaultSupplier}.
*
* @param defaultSupplier the supplier providing the default value
* @param mapper the function to apply to the value if present
* @param <U> the type of the result
* @return the mapped value if present, otherwise the default value
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U> U mapOrElse(Supplier<? extends U> defaultSupplier, Function<? super T, ? extends U> mapper) {
return isPresent() ? requireNonNull(mapper).apply(get()) : requireNonNull(defaultSupplier).get();
}
/**
* Returns {@code other} if a value is present, otherwise returns an empty {@code Opt}.
*
* @param other the alternative {@code Opt} to return if this is present
* @param <U> the type of the value in the alternative {@code Opt}
* @return {@code other} if a value is present, otherwise an empty {@code Opt}
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U> Opt<U> and(Opt<U> other) {
return isPresent() ? requireNonNull(other) : Opt.none();
}
/**
* Returns {@code this} if a value is present and {@code other} is empty,
* returns {@code other} if this is empty and a value is present in {@code other},
* otherwise returns an empty {@code Opt}.
*
* @param other the other {@code Opt} to compare with
* @return the exclusive value between {@code this} and {@code other}, or empty if both are present or both are empty
* @throws NullPointerException if input is null
* @since 1.1
*/
default Opt<T> xor(Opt<T> other) {
if (isPresent() && requireNonNull(other).isEmpty()) {
return this;
} else if (isEmpty() && other.isPresent()) {
return other;
} else {
return Opt.none();
}
}
/**
* Returns the contained value if present, otherwise returns {@code null}.
*
* @return the value if present, otherwise {@code null}
* @since 1.1
*/
default T orNull() {
return isPresent() ? get() : null;
}
/**
* Returns an {@code Optional} describing the value if present, otherwise an empty {@code Optional}.
*
* @return an {@code Optional} with a present value if this {@code Opt} is non-empty, otherwise an empty {@code Optional}
* @since 1.1
*/
default Optional<T> toOptional() {
return isPresent() ? Optional.of(get()) : Optional.empty();
}
/**
* Returns {@code true} if a value is present and the predicate returns {@code true} for it.
*
* @param predicate the predicate to apply to the value
* @return {@code true} if the value is present and matches the predicate, otherwise {@code false}
* @throws NullPointerException if input is null
* @since 1.1
*/
default boolean exists(Predicate<? super T> predicate) {
return isPresent() && requireNonNull(predicate).test(get());
}
/**
* Returns the contained value if present, otherwise throws a {@code NoSuchElementException}
* with the provided message.
*
* @param message the exception message to use if no value is present
* @return the value if present
* @throws NoSuchElementException if no value is present
* @throws NullPointerException if message is null
* @since 1.1
*/
default T expect(String message) {
if (isPresent()) {
return get();
}
throw new NoSuchElementException(requireNonNull(message));
}
/**
* Returns {@code true} if the contained value is equal to {@code value}.
*
* @param value the value to compare with the contained value
* @return {@code true} if the contained value equals {@code value}, otherwise {@code false}
* @since 1.1
*/
default boolean contains(T value) {
return isPresent() && Objects.equals(get(), value);
}
/**
* Returns the contained value if present, otherwise throws an exception produced by {@code exceptionSupplier}.
*
* @param exceptionSupplier the supplier of the exception to be thrown
* @param <X> the type of the exception to be thrown
* @return the value if present
* @throws X if no value is present
* @since 1.1
*/
<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X;
/**
* Applies the provided {@code mapper} function to the contained value if present,
* and returns an {@code Opt} containing the result.
*
* @param mapper the function to apply to the value if present
* @param <U> the type of the result of the mapping
* @return an {@code Opt} containing the result of applying the mapper, or an empty {@code Opt} if no value is present
* @throws NullPointerException if input is null
* @since 1.1
*/
<U> Opt<U> map(Function<? super T, ? extends U> mapper);
/**
* Applies the provided {@code mapper} function to the contained value if present,
* and returns the result directly.
*
* @param mapper the function to apply to the value if present
* @param <U> the type of the result
* @return the result of applying the mapper, or an empty {@code Opt} if no value is present
* @throws NullPointerException if input is null
* @since 1.1
*/
<U> Opt<U> flatMap(Function<? super T, Opt<U>> mapper);
/**
* Returns an {@code Opt} describing the value if it matches the given predicate,
* otherwise returns an empty {@code Opt}.
*
* @param predicate the predicate to apply to the value, if present
* @return {@code this} if the value matches the predicate, otherwise an empty {@code Opt}
* @throws NullPointerException if input is null
* @since 1.1
*/
Opt<T> filter(Predicate<? super T> predicate);
/**
* If a value is present and matches the predicate, return this {@code Opt}.
* Otherwise, return the alternative {@code Opt}.
*
* @param predicate the predicate to test the value
* @param fallback the alternative {@code Opt} if predicate fails
* @return this {@code Opt} if predicate passes, otherwise {@code fallback}
* @throws NullPointerException if input is null
* @since 1.1
*/
default Opt<T> filterOrElse(Predicate<? super T> predicate, Supplier<Opt<T>> fallback) {
return isPresent() && requireNonNull(predicate).test(get()) ? this : requireNonNull(fallback).get();
}
/**
* Returns an {@code Opt} with the value filtered out if it satisfies the given predicate.
*
* @param predicate the predicate to test the value against
* @return this {@code Opt} if the value does not satisfy the predicate,
* otherwise an empty {@code Opt}
* @throws NullPointerException if input is null
* @since 1.1
*/
default Opt<T> filterNot(Predicate<? super T> predicate) {
return filter(requireNonNull(predicate).negate());
}
/**
* Reduces this {@code Opt} to a single value by applying the {@code mapper} function to the contained value
* if present, or returns {@code defaultValue} if not.
*
* @param defaultValue the value to return if no value is present
* @param mapper the mapping function to apply to the value, if present
* @param <U> the type of the result
* @return the result of the mapping function if a value is present, otherwise {@code defaultValue}
* @throws NullPointerException if input is null
* @since 1.1
*/
default <U> U fold(U defaultValue, Function<? super T, ? extends U> mapper) {
return isPresent() ? requireNonNull(mapper).apply(get()) : requireNonNull(defaultValue);
}
/**
* Returns this {@code Opt} if a value is present, otherwise returns {@code alternative}.
*
* @param alternative the alternative {@code Opt} to return if no value is present
* @return this {@code Opt} if a value is present, otherwise {@code alternative}
* @since 1.1
*/
Opt<T> or(Opt<T> alternative);
/**
* If a value is present, performs the given action with the value,
* otherwise does nothing.
*
* @param consumer the action to be performed, if a value is present
* @since 1.1
*/
void ifPresent(Consumer<? super T> consumer);
/**
* If a value is present, performs the given action with the value,
* otherwise runs the provided alternative.
*
* @param consumer the action to be performed if a value is present
* @param alternative the runnable to run if no value is present
* @throws NullPointerException if input is null
* @since 1.1
*/
void ifPresentOrElse(Consumer<? super T> consumer, Runnable alternative);
// Inner classes
/**
* Functional pattern matching.
* Executes {@code some} if a value is present, otherwise executes {@code none}.
*
* @param some a function to apply if a value is present
* @param none a supplier t o invoke if no value is present
* @param <R> the type of the result
* @return the result of applying {@code some} or {@code none}
* @throws NullPointerException if input is null
* @since 1.1
*/
default <R> R match(Function<? super T, ? extends R> some, Supplier<? extends R> none) {
return switch (this) {
case Some<T> s -> requireNonNull(some).apply(s.value());
case None<T> n -> requireNonNull(none).get();
};
}
/**
* Represents the presence of a value.
*
* @param <T> the type of the contained value
* @since 1.1
*/
record Some<T>(T value) implements Opt<T> {
public Some(T value) {
this.value = requireNonNull(value, THE_VALUE_CAN_T_BE_NULL);
}
@Override
public Iterator<T> iterator() {
return List.of(value).iterator();
}
@Override
public <R> R accept(OptVisitor<T, R> visitor) {
return requireNonNull(visitor).visit(this);
}
@Override
public Stream<T> stream() {
return Stream.of(value);
}
@Override
public boolean isPresent() {
return true;
}
@Override
public T get() {
return value;
}
@Override
public T orElse(T other) {
return value;
}
@Override
public T orElseGet(Supplier<? extends T> supplier) {
return value;
}
@Override
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
return value;
}
@Override
public <U> Opt<U> map(Function<? super T, ? extends U> mapper) {
return Opt.ofNullable(requireNonNull(mapper).apply(value));
}
@Override
public <U> Opt<U> flatMap(Function<? super T, Opt<U>> mapper) {
return requireNonNull(mapper).apply(value);
}
@Override
public Opt<T> filter(Predicate<? super T> predicate) {
return requireNonNull(predicate).test(value) ? this : Opt.none();
}
@Override
public Opt<T> or(Opt<T> alternative) {
return this;
}
@Override
public void ifPresent(Consumer<? super T> consumer) {
requireNonNull(consumer).accept(value);
}
@Override
public void ifPresentOrElse(Consumer<? super T> consumer, Runnable alternative) {
requireNonNull(consumer).accept(value);
}
@Override
public String toString() {
return "Some(%s)".formatted(value);
}
}
/**
* Represents the absence of a value.
*
* @param <T> the type of the non-existent value
* @since 1.1
*/
record None<T>() implements Opt<T> {
private static final None<?> INSTANCE = new None<>();
@Override
public Iterator<T> iterator() {
return Collections.emptyIterator();
}
@Override
public <R> R accept(OptVisitor<T, R> visitor) {
return requireNonNull(visitor).visit(this);
}
@Override
public Stream<T> stream() {
return Stream.empty();
}
@Override
public boolean isPresent() {
return false;
}
@Override
public T get() {
throw new NoSuchElementException(NO_VALUE_PRESENT);
}
@Override
public T orElse(T other) {
return other;
}
@Override
public T orElseGet(Supplier<? extends T> supplier) {
return requireNonNull(supplier).get();
}
@Override
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
throw requireNonNull(exceptionSupplier).get();
}
@Override
public <U> Opt<U> map(Function<? super T, ? extends U> mapper) {
return Opt.none();
}
@Override
public <U> Opt<U> flatMap(Function<? super T, Opt<U>> mapper) {
return Opt.none();
}
@Override
public Opt<T> filter(Predicate<? super T> predicate) {
return this;
}
@Override
public Opt<T> or(Opt<T> alternative) {
return alternative;
}
@Override
public void ifPresent(Consumer<? super T> consumer) {
// Does nothing, following the behavior of Optional
}
@Override
public void ifPresentOrElse(Consumer<? super T> consumer, Runnable alternative) {
requireNonNull(alternative).run();
}
@Override
public boolean equals(Object obj) {
return obj instanceof None;
}
@Override
public int hashCode() {
return 0;
}
@Override
public String toString() {
return "None";
}
}
}