-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exceptional.java
1036 lines (933 loc) · 29.9 KB
/
Exceptional.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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* #%L
* io.earcam.unexceptional
* %%
* Copyright (C) 2016 - 2017 earcam
* %%
* SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
*
* You <b>must</b> choose to accept, in full - any individual or combination of
* the following licenses:
* <ul>
* <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
* <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
* <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
* <li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
* </ul>
* #L%
*/
package io.earcam.unexceptional;
import static java.util.Collections.unmodifiableMap;
import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLStreamHandler;
import java.security.GeneralSecurityException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntBiFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
/**
* <p>
* Static utility for easy conversion of checked exceptions, and invocation of methods declaring checked exceptions.
* </p>
*
* <p>
* There are many cases where you <i>know</i>, within a given context that a checked exception will not be raised, but
* if it is, then it's certainly <i>game over</i>. An example being URLs in server configuration.
* </p>
*
* <p>
* The functionality centres around {@link java.util.function} given their beautifully broad application.
* </p>
*
* <p>
* In addition, common standard library <i>annoyances</i> are included; e.g. {@link URL}, {@link URI}, and highly
* concise (re-read as terse one-liner - arguably laconic over expressive) for common IO streams usage.
* </p>
*
* <p>
* When dealing with {@link Stream} from an IO source (e.g. the file system), it may be preferable (to pay a
* performance cost due to wrapping) and use {@link EmeticStream} which mirrors {@link Stream} functionality
* but with checked equivalent types.
* </p>
*
* <h1>Exceptional</h1>
* <b>/ɪkˈsɛpʃ(ə)n(ə)l,ɛkˈsɛpʃ(ə)n(ə)l/</b>
* <p>
* <i>adjective:</i>
* </p>
* <p>
* 1. unusual; not typical.
* </p>
*/
@SuppressWarnings("serial")
@ParametersAreNonnullByDefault
@Immutable
public final class Exceptional implements Serializable {
private static final Map<Class<? extends Throwable>, Function<Throwable, RuntimeException>> UNCHECK_MAP;
static {
Map<Class<? extends Throwable>, Function<Throwable, RuntimeException>> map = new HashMap<>();
// @formatter:off
map.put(ReflectiveOperationException.class, e -> new UncheckedReflectiveException((ReflectiveOperationException)e));
map.put( GeneralSecurityException.class, e -> new UncheckedSecurityException((GeneralSecurityException)e));
map.put( InterruptedException.class, e -> new UncheckedInterruptException((InterruptedException)e));
map.put( URISyntaxException.class, e -> new UncheckedUriSyntaxException((URISyntaxException)e));
map.put( RuntimeException.class, RuntimeException.class::cast);
map.put( IOException.class, e -> new UncheckedIOException((IOException)e));
// @formatter:on
UNCHECK_MAP = unmodifiableMap(map);
}
/**
* An {@link UncaughtExceptionHandler} that simply rethrows,
* wrapping in an appropriate unchecked if necessary
*
* @since 0.2.0
*/
public static final UncaughtExceptionHandler RETHROWING = (t, e) -> Exceptional.rethrow(e);
/**
* An {@link UncaughtExceptionHandler} that simply swallows <i>all</i> exceptions,
* except subclasses of {@link Error}, which are rethrown.
*
* @since 0.2.0
*/
public static final UncaughtExceptionHandler SWALLOWING = (t, e) -> Exceptional.swallow(e);
private Exceptional()
{
throw new IllegalStateException("Why on earth would you want to instantiate this?");
}
/**
* @param earl the text URL
* @return a {@link URL} representation
*
* @since 0.2.0
*/
public static URL url(CharSequence earl)
{
try {
return new URL(earl.toString());
} catch(MalformedURLException e) {
throw uncheck(e);
}
}
/**
* @param protocol e.g. https, ftp, sctp
* @param host hostname or IP
* @param port 0 to 65536
* @param path the "file" portion of the URL
* @param handler optional URLStreamHandler
* @return a {@link URL} representation
*
* @since 0.2.0
*/
public static URL url(String protocol, String host, int port, String path, @Nullable URLStreamHandler handler)
{
try {
return new URL(protocol, host, port, path, handler);
} catch(MalformedURLException e) {
throw uncheck(e);
}
}
/**
* @param ʊri the text URI (as Earl is to URL, so ʊri (as in Uri Geller) is to URI)
* @return a {@link URI} representation
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S00117") // utf8 in a parameter name is fine by me
public static URI uri(CharSequence ʊri)
{
try {
return new URI(ʊri.toString());
} catch(URISyntaxException e) {
throw uncheck(e);
}
}
/**
* Invokes {@link URL#toURI()}, catching any {@link URISyntaxException} and rethrowing
* wrapped as {@link UncheckedUriSyntaxException}
*
* @param earl the {@link URL} to convert
* @return the {@link URI} form of the <code>earl</code> argument
*
* @since 0.3.0
*/
public static URI uri(URL earl)
{
try {
return earl.toURI();
} catch(URISyntaxException e) {
throw uncheck(e);
}
}
/**
* Will regurgitate any {@link Error} - otherwise will dutifully and silently <i>swallow</i> whole.
* Gulping of {@link InterruptedException}s will result in the current {@link Thread}'s interrupt
* flag being reset.
*
* <b>Caution</b>: used carelessly/incorrectly this foul method will inevitably lead to a frustrating
* debugging session resulting in plenty of "doh"/"wtf" but never a "eureka" moment. Shooting of
* messenger is in breach of license terms.
*
* @param caught the caught unmentionable
*
* @since 0.2.0
*/
public static void swallow(Throwable caught)
{
resetIfInterrupt(caught);
if(caught instanceof Error) {
rethrow(caught);
}
}
/**
* Catching an {@link InterruptedException} clears the interrupt flag,
* this merely resets the flag IFF the <code>thrown</code> parameter is
* an instance of {@link InterruptedException}.
*
* @param thrown possible {@link InterruptedException}
*
* @since 0.2.0
*/
public static void resetIfInterrupt(Throwable thrown)
{
if(thrown instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
}
/**
* Directly rethrows {@link Error}s or {@link RuntimeException}s, wraps
* checked exceptions appropriately
*
* @param thrown the caught throwable to be rethrown as unchecked
* @return actually nothing, this just allows you to write {@code throw Exceptional.rethrow(e)} for methods than
* have return values.
*
* @see #throwAsUnchecked(Throwable)
*
* @since 0.2.0
*/
public static RuntimeException rethrow(Throwable thrown)
{
if(thrown instanceof Error) {
throw (Error) thrown;
}
throw uncheck(thrown);
}
/**
* Invokes {@link CheckedRunnable#run()} catching any checked
* {@link Exception}s rethrowing them as unchecked.
*
* @param runnable the checked runnable to run
*
* @since 0.2.0
*/
public static void run(CheckedRunnable runnable)
{
try {
runnable.run();
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
rethrow(thrown);
}
}
/**
* Convert a {@link CheckedRunnable} into a {@link Runnable}
*
* @param runnable the checked runnable to wrap
* @return an unchecked wrapper around the <code>runnable</code> argument
*
* @since 0.2.0
*/
public static Runnable uncheckRunnable(CheckedRunnable runnable)
{
return () -> run(runnable);
}
/**
* Invokes {@link Callable#call()} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> the return type of the {@link Callable}
*
* @param callable the {@link Callable} to execute
* @return the result of calling the {@link Callable}
*
* @since 0.2.0
*/
public static <T> T call(Callable<T> callable)
{
try {
return callable.call();
} catch(Exception thrown) { // NO SONAR
throw uncheck(thrown);
}
}
/**
* <p>
* Converts {@link Throwable}s to {@link RuntimeException}s.
* </p>
* <p>
* If the supplied {@link Throwable} is already a {@link RuntimeException}, then it's simply cast and returned.
* </p>
* <p>
* {@link Error} subclasses will be wrapped in an {@link UncheckedException}.
* </p>
* <p>
* The interrupt flag will be reset IFF {@code caught instanceof InterruptedException}
* </p>
*
* @param caught any {@link Throwable}
* @return a {@link RuntimeException}, typically a subclass of {@link UncheckedException} or an
* {@link UncheckedIOException}
* @see #rethrow(Throwable)
*
* @since 0.2.0
*/
public static RuntimeException uncheck(Throwable caught)
{
return UNCHECK_MAP.entrySet().stream()
.filter(e -> e.getKey().isInstance(caught))
.map(e -> e.getValue().apply(caught))
.findFirst()
.orElseGet(() -> new UncheckedException(caught));
}
/**
* Converts {@link CheckedConsumer} to {@link Consumer}
*
* @param <T> the consumed type
*
* @param consumer a consumer that declares checked exception(s)
* @return a vanilla {@link java.util.function.Consumer}
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> Consumer<T> uncheckConsumer(CheckedConsumer<T> consumer)
{
return (Consumer<T> & Serializable) t -> Exceptional.accept(consumer, t);
}
/**
* Invokes {@link CheckedConsumer#accept(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> the consumed type
*
* @param consumer the consumer of the {@code value}
* @param value the value to be consumed
*
* @since 0.2.0
*/
public static <T> void accept(CheckedConsumer<T> consumer, T value)
{
try {
consumer.accept(value);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
rethrow(thrown);
}
}
/**
* Convert a {@link CheckedBiConsumer} into a {@link BiConsumer}.
*
* @param <T> first argument type
* @param <U> last argument type
*
* @param consumer the checked consumer
* @return an unchecked consumer wrapping the {@code consumer} argument
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T, U> BiConsumer<T, U> uncheckBiConsumer(CheckedBiConsumer<T, U> consumer)
{
return (BiConsumer<T, U> & Serializable) (t, u) -> Exceptional.accept(consumer, t, u);
}
/**
* Invokes {@link CheckedConsumer#accept(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> first argument type
* @param <U> last argument type
*
* @param consumer the consumer of the {@code value}
* @param t first argument to be consumed
* @param u last argument to be consumed
*
* @since 0.2.0
*/
public static <T, U> void accept(CheckedBiConsumer<T, U> consumer, T t, U u)
{
try {
consumer.accept(t, u);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
rethrow(thrown);
}
}
/**
* Converts a {@link CheckedFunction} into a {@link Function}.
*
* @param <T> argument type
* @param <R> return type
*
* @param function the checked function
* @return an unchecked function wrapping the {@code function} argument.
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T, R> Function<T, R> uncheckFunction(CheckedFunction<T, R> function)
{
return (Function<T, R> & Serializable) t -> Exceptional.apply(function, t);
}
/**
* Invokes {@link CheckedFunction#apply(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> argument type
* @param <R> return type
*
* @param function the checked function to invoke with the {@code argument}
* @param argument the argument to apply to the function
* @return the result of applying {@code argument} to {@code function}
*
* @since 0.2.0
*/
public static <T, R> R apply(CheckedFunction<T, R> function, T argument)
{
try {
return function.apply(argument);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Convert a {@link CheckedBiFunction} into a {@link BiFunction}.
*
* @param <T> first argument type
* @param <U> last argument type
* @param <R> return type
*
* @param function the checked bi-function
* @return an unchecked bi-function wrapping the {@code function} argument.
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T, U, R> BiFunction<T, U, R> uncheckBiFunction(CheckedBiFunction<T, U, R> function)
{
return (BiFunction<T, U, R> & Serializable) (t, u) -> Exceptional.apply(function, t, u);
}
/**
* Invokes {@link CheckedFunction#apply(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> first argument type
* @param <U> last argument type
* @param <R> return type
*
* @param function the checked function to invoke with the {@code argument}
* @param t the first argument to apply to the function
* @param u the second argument to apply to the function
* @return the result of applying {@code function} to the arguments {@code t} and {@code u}
*
* @since 0.2.0
*/
public static <T, U, R> R apply(CheckedBiFunction<T, U, R> function, T t, U u)
{
try {
return function.apply(t, u);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Converts a {@link CheckedBinaryOperator} into a {@link BinaryOperator}.
*
* @param <T> operator type
*
* @param operator the checked binary operator
* @return and unchecked wrapper around the {@code operator} argument
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> BinaryOperator<T> uncheckBinaryOperator(CheckedBinaryOperator<T> operator)
{
return (BinaryOperator<T> & Serializable) (a, b) -> Exceptional.apply(operator, a, b);
}
/**
* Converts a {@link CheckedToDoubleFunction} into a {@link ToDoubleFunction}.
*
* @param <T> argument type
*
* @param function the checked to-double-function
* @return an unchecked to-double-function wrapping the {@code function} argument.
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> ToDoubleFunction<T> uncheckToDoubleFunction(CheckedToDoubleFunction<T> function)
{
return (ToDoubleFunction<T> & Serializable) t -> Exceptional.applyAsDouble(function, t);
}
/**
* Invokes {@link CheckedToDoubleFunction#applyAsDouble(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> argument type
*
* @param function the checked to-double function
* @param t the function argument
* @return the double result of applying the {@code function} to argument {@code t}
*
* @since 0.2.0
*/
public static <T> double applyAsDouble(CheckedToDoubleFunction<T> function, T t)
{
try {
return function.applyAsDouble(t);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Converts a {@link CheckedToIntFunction} into a {@link ToIntFunction}.
*
* @param <T> argument type
*
* @param function the checked to-int-function
* @return an unchecked to-int-function wrapping the {@code function} argument.
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> ToIntFunction<T> uncheckToIntFunction(CheckedToIntFunction<T> function)
{
return (ToIntFunction<T> & Serializable) t -> Exceptional.applyAsInt(function, t);
}
/**
* Invokes {@link CheckedToIntFunction#applyAsInt(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> argument type
*
* @param function the checked to-int function
* @param t the function argument
* @return the int result of applying the {@code function} to argument {@code t}
*
* @since 0.2.0
*/
public static <T> int applyAsInt(CheckedToIntFunction<T> function, T t)
{
try {
return function.applyAsInt(t);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Converts a {@link CheckedToLongFunction} into a {@link ToLongFunction}.
*
* @param <T> argument type
*
* @param function the checked to-long-function
* @return an unchecked to-long-function wrapping the {@code function} argument.
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> ToLongFunction<T> uncheckToLongFunction(CheckedToLongFunction<T> function)
{
return (ToLongFunction<T> & Serializable) t -> Exceptional.applyAsLong(function, t);
}
/**
* Invokes {@link CheckedToLongFunction#applyAsLong(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> argument type
*
* @param function the checked to-long function
* @param t the function argument
* @return the long result of applying the {@code function} to argument {@code t}
*
* @since 0.2.0
*/
public static <T> long applyAsLong(CheckedToLongFunction<T> function, T t)
{
try {
return function.applyAsLong(t);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Convert a {@link CheckedSupplier} into an unchecked {@link Supplier}.
*
* @param <T> supplied type
*
* @param supplier the checked supplier to wrap
* @return an unchecked supplier wrapping the {@code supplier} argument
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> Supplier<T> uncheckSupplier(CheckedSupplier<T> supplier)
{
return (Supplier<T> & Serializable) () -> Exceptional.get(supplier);
}
/**
* Invokes {@link CheckedSupplier#get()} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> supplied type
*
* @param supplier the checked supplier
* @return the result as supplied from the {@code supplier} argument
*
* @since 0.2.0
*/
public static <T> T get(CheckedSupplier<T> supplier)
{
try {
return supplier.get();
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Converts a {@link CheckedPredicate} into a {@link Predicate}.
*
* @param <T> tested type
*
* @param predicate the checked predicate to wrap
* @return an unchecked predicate wrapping the {@code predicate} argument
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> Predicate<T> uncheckPredicate(CheckedPredicate<T> predicate)
{
return (Predicate<T> & Serializable) t -> Exceptional.test(predicate, t);
}
/**
* Invokes {@link CheckedPredicate#test(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> tested type
*
* @param predicate the checked predicate to test on the {@code value}
* @param value the value to be tested
* @return true IFF value passes predicate's test
*
* @since 0.2.0
*/
public static <T> boolean test(CheckedPredicate<T> predicate, T value)
{
try {
return predicate.test(value);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* Converts a {@link CheckedComparator} into an unchecked {@link Comparator}.
*
* @param <T> compared type
*
* @param comparator the checked comparator to wrap
* @return an unchecked comparator wrapping the {@code comparator} argument
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T> Comparator<T> uncheckComparator(CheckedComparator<T> comparator)
{
return (Comparator<T> & Serializable) (a, b) -> Exceptional.applyAsInt(comparator, a, b);
}
/**
* Converts a {@link CheckedToIntBiFunction} into an unchecked {@link ToIntBiFunction}.
*
* @param <T> first argument type
* @param <U> last argument type
*
* @param function the checked bi-function
* @return an unchecked bi-function
*/
@SuppressWarnings("squid:S1905") // SonarQube false positives
public static <T, U> ToIntBiFunction<T, U> uncheckToIntBiFunction(CheckedToIntBiFunction<T, U> function)
{
return (ToIntBiFunction<T, U> & Serializable) (a, b) -> Exceptional.applyAsInt(function, a, b);
}
/**
* Invokes {@link CheckedFunction#apply(Object)} catching any checked
* {@link Exception}s rethrowing as unchecked.
*
* @param <T> first argument type
* @param <U> last argument type
*
* @param function the checked function to invoke with the {@code argument}
* @param t the first argument to apply to the function
* @param u the second argument to apply to the function
* @return the result of applying {@code function} to the arguments {@code t} and {@code u}
*
* @since 0.2.0
*/
@SuppressWarnings("squid:S00112") // necessary to declare Throwable
public static <T, U> int applyAsInt(CheckedToIntBiFunction<T, U> function, T t, U u)
{
try {
return function.applyAsInt(t, u);
} catch(@SuppressWarnings("squid:S00112") Throwable thrown) {
throw rethrow(thrown);
}
}
/**
* <p>
* This fugly method relies on erasure to trick the compiler, allowing you to throw any checked
* exception without declaring so on the surrounding method. You are almost certainly better off
* using {@link #rethrow(Throwable)}.
* </p>
* <p>
* <b>Note</b>: this may well become an obsolete hack in future versions of Java if generics change.
* </p>
*
* @param throwable the {@link Throwable} to be thrown
* @return this alleged return will never be received, but useful in that
* you may write <code>throws {@link Exceptional#throwAsUnchecked(Throwable)};</code>
* to placate the compiler WRT non-void method returns, and ensure Static Code
* Analysis doesn't accuse you of swallowing the exception.
*
* @since 0.2.0
*
* @see #rethrow(Throwable)
*/
public static RuntimeException throwAsUnchecked(Throwable throwable)
{
resetIfInterrupt(throwable);
throw Exceptional.<RuntimeException> eraseAndThrow(throwable);
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> RuntimeException eraseAndThrow(Throwable throwable) throws T
{
throw (T) throwable;
}
/**
* <p>
* Attempts to unwrap invocation and unchecked exceptions to their underlying cause.
* Unwrapping is recursively applied. The original wrapper exception is added to the suppressed
* throwables of the returned unwrapped throwable.
* </p>
*
* <p>
* Unwraps: {@link InvocationTargetException}, {@link UndeclaredThrowableException}, {@link UncheckedIOException}
* and {@link UncheckedException}
* </p>
*
* @param throwable to be unwrapped
* @return the root cause, or original throwable if not unwrapped
*
* @since 0.2.0
*
*/
public static Throwable unwrap(Throwable throwable)
{
Throwable unwrapped = unwrapping(throwable);
if(unwrapped != throwable) {
unwrapped.addSuppressed(throwable);
}
return unwrapped;
}
private static Throwable unwrapping(Throwable throwable)
{
Set<Throwable> seen = new HashSet<>();
while(isUnwrappable(throwable) && !seen.contains(throwable)) {
seen.add(throwable);
throwable = throwable.getCause();
}
return throwable;
}
private static boolean isUnwrappable(Throwable throwable)
{
return throwable.getCause() != null
&& (throwable instanceof InvocationTargetException
|| throwable instanceof UndeclaredThrowableException
|| throwable instanceof UncheckedIOException
|| throwable instanceof UncheckedException);
}
/**
* <p>
* Recursively find the wrapped throwable
* </p>
*
* <b>NOTE</b>: this method now just delegates to {@link #unwrap(Throwable)} and is therefore superfluous,
* as such it will be removed in the next major release without impact to client code; given
* this, it is not marked {@link Deprecated} to avoid unnecessary upcasting to {@link Throwable}.
*
* @param throwable to be unwrapped
* @return the value of {@link UndeclaredThrowableException#getUndeclaredThrowable()}
*
* @since 0.2.0
*
* @see #unwrap(Throwable)
*/
public static Throwable unwrap(UndeclaredThrowableException throwable)
{
return unwrap((Throwable) throwable);
}
/**
* <p>
* Recursively find the wrapped throwable
* </p>
*
* <b>NOTE</b>: this method now just delegates to {@link #unwrap(Throwable)} and is therefore superfluous,
* as such it will be removed in the next major release without impact to client code; given
* this, it is not marked {@link Deprecated} to avoid unnecessary upcasting to {@link Throwable}.
*
* @param throwable to be unwrapped
* @return the value of {@link InvocationTargetException#getTargetException()}
*
* @since 0.2.0
*/
public static Throwable unwrap(InvocationTargetException throwable)
{
return unwrap((Throwable) throwable);
}
/**
* Ultra-shorthand for {@link AutoCloseable}/{@link java.io.Closeable}, obvious use for {@link java.io.InputStream}
*
* @param <C> {@link AutoCloseable} type
* @param <T> {@code create} function argument type
* @param <R> the result type
*
* @param create a function applying {@code t} to produce an {@link AutoCloseable} of type {@code <C>}
* @param t the argument to apply to the {@code create} function
* @param convert a function applied to the {@link AutoCloseable} to produce the result
* @return the result of applying the {@code convert} function
*
* @since 0.2.0
*
* @deprecated functionality moved to {@link Closing}
* @see Closing
*/
@Deprecated
public static <C extends AutoCloseable, T, R> R closeAfterApplying(CheckedFunction<T, C> create, T t, CheckedFunction<C, R> convert)
{
return Closing.closeAfterApplying(create, t, convert);
}
/**
* Applies the function to the closeable, returning the result and closing the closable - checked exceptions are
* rethrown as unchecked.
*
* @param <C> the auto-closeable type, to be created, consumed and closed
* @param <R> the result type
*
* @param closeable the closeable subject of the {@code convert} function
* @param convert the function consuming the closeable and supplying the result
* @return the result of applying {@code convert} function to the {@code closeable} argument
*
* @since 0.2.0
*
* @deprecated functionality moved to {@link Closing#closeAfterApplying(AutoCloseable, CheckedFunction)}
* @see Closing
*/
@Deprecated
public static <C extends AutoCloseable, R> R closeAfterApplying(C closeable, CheckedFunction<C, R> convert)
{
return Closing.closeAfterApplying(closeable, convert);
}
/**
* Applies the {@code create} function to {@code t}, resulting in a {@link AutoCloseable} which is closed after
* being consumed.
* Checked exceptions are rethrown as unchecked.
*
* @param <C> the auto-closeable type, to be created, consumed and closed
* @param <T> the function's argument type, used to create the auto-closeable
*
* @param create the function creating the {@link AutoCloseable}
* @param t the argument that the {@code create} function is applied to
* @param consume the consumer of the {@link AutoCloseable}
*
* @since 0.2.0
*
* @deprecated functionality moved to {@link Closing#closeAfterAccepting(CheckedFunction, Object, CheckedConsumer)}
* @see Closing
*/
@Deprecated
public static <C extends AutoCloseable, T> void closeAfterAccepting(CheckedFunction<T, C> create, T t, CheckedConsumer<C> consume)
{
Closing.closeAfterAccepting(create, t, consume);
}
/**
* Consumes the {@code closeable} before closing. Checked exceptions are rethrown as unchecked.
*
* @param <C> the auto-closeable type
*
* @param closeable the closeable to be consumed and closed
* @param consume the consumer of the {@link AutoCloseable}
*
* @since 0.2.0
*
* @deprecated functionality moved to {@link Closing#closeAfterAccepting(AutoCloseable, CheckedConsumer)}
* @see Closing
*/
@Deprecated
public static <C extends AutoCloseable> void closeAfterAccepting(C closeable, CheckedConsumer<C> consume)
{
Closing.closeAfterAccepting(closeable, consume);
}