001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.function;
018
019import java.io.IOException;
020
021/**
022 * Erases {@link IOException} for the compiler but still throws that exception at runtime.
023 *
024 * @since 2.16.0
025 */
026public final class Erase {
027
028    /**
029     * Delegates to the given {@link IOBiConsumer} but erases its {@link IOException} for the compiler, while still throwing
030     * the exception at runtime.
031     *
032     * @param <T> See delegate.
033     * @param <U> See delegate.
034     * @param consumer See delegate.
035     * @param t See delegate.
036     * @param u See delegate.
037     * @see IOBiConsumer
038     */
039    static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
040        try {
041            consumer.accept(t, u);
042        } catch (final IOException ex) {
043            rethrow(ex); // throws IOException
044        }
045    }
046
047    /**
048     * Delegates to the given {@link IOConsumer} but erases its {@link IOException} for the compiler, while still throwing
049     * the exception at runtime.
050     *
051     * @param <T> See delegate.
052     * @param consumer See delegate.
053     * @param t See delegate.
054     * @see IOConsumer
055     */
056    static <T> void accept(final IOConsumer<T> consumer, final T t) {
057        try {
058            consumer.accept(t);
059        } catch (final IOException ex) {
060            rethrow(ex); // throws IOException
061        }
062    }
063
064    /**
065     * Delegates to the given {@link IOBiFunction} but erases its {@link IOException} for the compiler, while still throwing
066     * the exception at runtime.
067     *
068     * @param <T> See delegate.
069     * @param <U> See delegate.
070     * @param <R> See delegate.
071     * @param mapper See delegate.
072     * @param t See delegate.
073     * @param u See delegate.
074     * @return See delegate.
075     * @see IOBiFunction
076     */
077    static <T, U, R> R apply(final IOBiFunction<? super T, ? super U, ? extends R> mapper, final T t, final U u) {
078        try {
079            return mapper.apply(t, u);
080        } catch (final IOException e) {
081            throw rethrow(e); // throws IOException
082        }
083    }
084
085    /**
086     * Delegates to the given {@link IOFunction} but erases its {@link IOException} for the compiler, while still throwing
087     * the exception at runtime.
088     *
089     * @param <T> See delegate.
090     * @param <R> See delegate.
091     * @param mapper See delegate.
092     * @param t See delegate.
093     * @return See delegate.
094     * @see IOFunction
095     */
096    static <T, R> R apply(final IOFunction<? super T, ? extends R> mapper, final T t) {
097        try {
098            return mapper.apply(t);
099        } catch (final IOException e) {
100            throw rethrow(e); // throws IOException
101        }
102    }
103
104    /**
105     * Delegates to the given {@link IOComparator} but erases its {@link IOException} for the compiler, while still throwing
106     * the exception at runtime.
107     *
108     * @param <T> See delegate.
109     * @param comparator See delegate.
110     * @param t See delegate.
111     * @param u See delegate.
112     * @return See delegate.
113     * @see IOComparator
114     */
115    static <T> int compare(final IOComparator<? super T> comparator, final T t, final T u) {
116        try {
117            return comparator.compare(t, u);
118        } catch (final IOException e) {
119            throw rethrow(e); // throws IOException
120        }
121    }
122
123    /**
124     * Delegates to the given {@link IOSupplier} but erases its {@link IOException} for the compiler, while still throwing
125     * the exception at runtime.
126     *
127     * @param <T> See delegate.
128     * @param supplier See delegate.
129     * @return See delegate.
130     * @see IOSupplier
131     */
132    static <T> T get(final IOSupplier<T> supplier) {
133        try {
134            return supplier.get();
135        } catch (final IOException e) {
136            throw rethrow(e); // throws IOException
137        }
138    }
139
140    /**
141     * Throws the given throwable.
142     *
143     * @param <T> The throwable cast type.
144     * @param throwable The throwable to rethrow.
145     * @return nothing because we throw.
146     * @throws T Always thrown.
147     */
148    @SuppressWarnings("unchecked")
149    public static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
150        throw (T) throwable;
151    }
152
153    /**
154     * Delegates to the given {@link IORunnable} but erases its {@link IOException} for the compiler, while still throwing
155     * the exception at runtime.
156     *
157     * @param runnable See delegate.
158     * @see IORunnable
159     */
160    static void run(final IORunnable runnable) {
161        try {
162            runnable.run();
163        } catch (final IOException e) {
164            throw rethrow(e); // throws IOException
165        }
166    }
167
168    /**
169     * Delegates to the given {@link IOPredicate} but erases its {@link IOException} for the compiler, while still throwing
170     * the exception at runtime.
171     *
172     * @param <T> See delegate.
173     * @param predicate See delegate.
174     * @param t See delegate.
175     * @return See delegate.
176     * @see IOPredicate
177     */
178    static <T> boolean test(final IOPredicate<? super T> predicate, final T t) {
179        try {
180            return predicate.test(t);
181        } catch (final IOException e) {
182            throw rethrow(e); // throws IOException
183        }
184    }
185
186    /** No instances. */
187    private Erase() {
188        // No instances.
189    }
190
191}