1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.function;
19
20 import java.io.IOException;
21 import java.util.concurrent.atomic.AtomicInteger;
22 import java.util.concurrent.atomic.AtomicLong;
23 import java.util.concurrent.atomic.AtomicReference;
24
25 final class TestUtils {
26
27 static int compareAndSetThrowsIO(final AtomicInteger ref, final int update) throws IOException {
28 return compareAndSetThrowsIO(ref, 0, update);
29 }
30
31 static int compareAndSetThrowsIO(final AtomicInteger ref, final int expected, final int update) throws IOException {
32 if (!ref.compareAndSet(expected, update)) {
33 throw new IOException("Unexpected");
34 }
35 return ref.get();
36 }
37
38 static long compareAndSetThrowsIO(final AtomicLong ref, final long update) throws IOException {
39 return compareAndSetThrowsIO(ref, 0, update);
40 }
41
42 static long compareAndSetThrowsIO(final AtomicLong ref, final long expected, final long update) throws IOException {
43 if (!ref.compareAndSet(expected, update)) {
44 throw new IOException("Unexpected");
45 }
46 return ref.get();
47 }
48
49 static <T> T compareAndSetThrowsIO(final AtomicReference<T> ref, final T update) throws IOException {
50 return compareAndSetThrowsIO(ref, null, update);
51 }
52
53 static <T> T compareAndSetThrowsIO(final AtomicReference<T> ref, final T expected, final T update) throws IOException {
54 if (!ref.compareAndSet(expected, update)) {
55 throw new IOException("Unexpected");
56 }
57 return ref.get();
58 }
59
60 static <T> T compareAndSetThrowsRE(final AtomicReference<T> ref, final T expected, final T update) {
61 if (!ref.compareAndSet(expected, update)) {
62 throw new RuntimeException("Unexpected");
63 }
64 return ref.get();
65 }
66
67 @SuppressWarnings("unchecked")
68 static <T, U> IOBiConsumer<T, U> throwingIOBiConsumer() {
69 return (IOBiConsumer<T, U>) TestConstants.THROWING_IO_BI_CONSUMER;
70 }
71
72 @SuppressWarnings("unchecked")
73 static <T, U, V> IOBiFunction<T, U, V> throwingIOBiFunction() {
74 return (IOBiFunction<T, U, V>) TestConstants.THROWING_IO_BI_FUNCTION;
75 }
76
77 @SuppressWarnings("unchecked")
78 static <T> IOBinaryOperator<T> throwingIOBinaryOperator() {
79 return (IOBinaryOperator<T>) TestConstants.THROWING_IO_BINARY_OPERATOR;
80 }
81
82 @SuppressWarnings("unchecked")
83 static <T> IOComparator<T> throwingIOComparator() {
84 return (IOComparator<T>) TestConstants.THROWING_IO_COMPARATOR;
85 }
86
87 @SuppressWarnings("unchecked")
88 static <T> IOConsumer<T> throwingIOConsumer() {
89 return (IOConsumer<T>) TestConstants.THROWING_IO_CONSUMER;
90 }
91
92 @SuppressWarnings("unchecked")
93 static <T, U> IOFunction<T, U> throwingIOFunction() {
94 return (IOFunction<T, U>) TestConstants.THROWING_IO_FUNCTION;
95 }
96
97 @SuppressWarnings("unchecked")
98 static <T> IOPredicate<T> throwingIOPredicate() {
99 return (IOPredicate<T>) TestConstants.THROWING_IO_PREDICATE;
100 }
101
102 static IORunnable throwingIORunnable() {
103 return TestConstants.THROWING_IO_RUNNABLE;
104 }
105
106 @SuppressWarnings("unchecked")
107 static <T> IOSupplier<T> throwingIOSupplier() {
108 return (IOSupplier<T>) TestConstants.THROWING_IO_SUPPLIER;
109 }
110
111 @SuppressWarnings("unchecked")
112 static <T> IOUnaryOperator<T> throwingIOUnaryOperator() {
113 return (IOUnaryOperator<T>) TestConstants.THROWING_IO_UNARY_OPERATOR;
114 }
115
116 }