View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io.function;
19  
20  import java.io.IOException;
21  import java.util.concurrent.atomic.AtomicBoolean;
22  import java.util.concurrent.atomic.AtomicInteger;
23  import java.util.concurrent.atomic.AtomicLong;
24  import java.util.concurrent.atomic.AtomicReference;
25  
26  final class TestUtils {
27  
28      static boolean compareAndSetThrowsIO(final AtomicBoolean ref, final boolean update) throws IOException {
29          return compareAndSetThrowsIO(ref, false, update);
30      }
31  
32      static boolean compareAndSetThrowsIO(final AtomicBoolean ref, final boolean expected, final boolean update) throws IOException {
33          if (!ref.compareAndSet(expected, update)) {
34              throw new IOException("Unexpected");
35          }
36          return ref.get(); // same as update
37      }
38  
39      static int compareAndSetThrowsIO(final AtomicInteger ref, final int update) throws IOException {
40          return compareAndSetThrowsIO(ref, 0, update);
41      }
42  
43      static int compareAndSetThrowsIO(final AtomicInteger ref, final int expected, final int update) throws IOException {
44          if (!ref.compareAndSet(expected, update)) {
45              throw new IOException("Unexpected");
46          }
47          return ref.get(); // same as update
48      }
49  
50      static long compareAndSetThrowsIO(final AtomicLong ref, final long update) throws IOException {
51          return compareAndSetThrowsIO(ref, 0, update);
52      }
53  
54      static long compareAndSetThrowsIO(final AtomicLong ref, final long expected, final long update) throws IOException {
55          if (!ref.compareAndSet(expected, update)) {
56              throw new IOException("Unexpected");
57          }
58          return ref.get(); // same as update
59      }
60  
61      static <T> T compareAndSetThrowsIO(final AtomicReference<T> ref, final T update) throws IOException {
62          return compareAndSetThrowsIO(ref, null, update);
63      }
64  
65      static <T> T compareAndSetThrowsIO(final AtomicReference<T> ref, final T expected, final T update) throws IOException {
66          if (!ref.compareAndSet(expected, update)) {
67              throw new IOException("Unexpected");
68          }
69          return ref.get(); // same as update
70      }
71  
72      static <T> T compareAndSetThrowsRE(final AtomicReference<T> ref, final T expected, final T update) {
73          if (!ref.compareAndSet(expected, update)) {
74              throw new RuntimeException("Unexpected");
75          }
76          return ref.get(); // same as update
77      }
78  
79      @SuppressWarnings("unchecked")
80      static <T, U> IOBiConsumer<T, U> throwingIOBiConsumer() {
81          return (IOBiConsumer<T, U>) TestConstants.THROWING_IO_BI_CONSUMER;
82      }
83  
84      @SuppressWarnings("unchecked")
85      static <T, U, V> IOBiFunction<T, U, V> throwingIOBiFunction() {
86          return (IOBiFunction<T, U, V>) TestConstants.THROWING_IO_BI_FUNCTION;
87      }
88  
89      @SuppressWarnings("unchecked")
90      static <T> IOBinaryOperator<T> throwingIOBinaryOperator() {
91          return (IOBinaryOperator<T>) TestConstants.THROWING_IO_BINARY_OPERATOR;
92      }
93  
94      @SuppressWarnings("unchecked")
95      static <T> IOComparator<T> throwingIOComparator() {
96          return (IOComparator<T>) TestConstants.THROWING_IO_COMPARATOR;
97      }
98  
99      @SuppressWarnings("unchecked")
100     static <T> IOConsumer<T> throwingIOConsumer() {
101         return (IOConsumer<T>) TestConstants.THROWING_IO_CONSUMER;
102     }
103 
104     @SuppressWarnings("unchecked")
105     static <T, U> IOFunction<T, U> throwingIOFunction() {
106         return (IOFunction<T, U>) TestConstants.THROWING_IO_FUNCTION;
107     }
108 
109     static IOIntConsumer throwingIOIntConsumer() {
110         return TestConstants.THROWING_IO_INT_CONSUMER;
111     }
112 
113     @SuppressWarnings("unchecked")
114     static <T> IOPredicate<T> throwingIOPredicate() {
115         return (IOPredicate<T>) TestConstants.THROWING_IO_PREDICATE;
116     }
117 
118     static IORunnable throwingIORunnable() {
119         return TestConstants.THROWING_IO_RUNNABLE;
120     }
121 
122     @SuppressWarnings("unchecked")
123     static <T> IOSupplier<T> throwingIOSupplier() {
124         return (IOSupplier<T>) TestConstants.THROWING_IO_SUPPLIER;
125     }
126 
127     @SuppressWarnings("unchecked")
128     static <T> IOUnaryOperator<T> throwingIOUnaryOperator() {
129         return (IOUnaryOperator<T>) TestConstants.THROWING_IO_UNARY_OPERATOR;
130     }
131 
132 }