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    *      http://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.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(); // same as update
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(); // same as update
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(); // same as update
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(); // same as update
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 }