
io.helidon.build.util.Unchecked Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.build.util;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility to deal with checked exceptions in lambdas.
*/
public interface Unchecked {
/**
* Checked consumer.
*
* @param item type
* @param checked exception type
*/
interface CheckedConsumer {
/**
* Accept an item.
*
* @param item item
* @throws E if an error occurs
*/
void accept(T item) throws E;
}
/**
* Checked consumer.
*
* @param 1st item type
* @param 2nd item type
* @param checked exception type
*/
interface CheckedBiConsumer {
/**
* Accept an item.
*
* @param item1 1st item
* @param item2 2nd item
* @throws E if an error occurs
*/
void accept(T item1, U item2) throws E;
}
/**
* Checked consumer.
*
* @param input type
* @param output type
* @param checked exception type
*/
interface CheckedFunction {
/**
* Accept an item.
*
* @param item input item
* @return U
* @throws E if an error occurs
*/
U apply(T item) throws E;
}
/**
* Checked runnable.
*
* @param checked exception type
*/
interface CheckedRunnable {
/**
* Run.
*
* @throws E if an error occurs
*/
void run() throws E;
}
/**
* Checked supplier.
*
* @param supplier type
* @param checked exception type
*/
interface CheckedSupplier {
/**
* Get the value.
*
* @return T value
* @throws E if an error occurs
*/
T get() throws E;
}
/**
* Wrap a {@link CheckedSupplier} into a {@link Supplier}.
*
* @param supplier checked supplier
* @param supplier type
* @param checked exception type
* @return Supplier
*/
static Supplier unchecked(CheckedSupplier supplier) {
return () -> {
try {
return supplier.get();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* Wrap a {@link CheckedRunnable} into a {@link Runnable}.
*
* @param runnable checked runnable
* @param checked exception type
* @return Consumer
*/
static Runnable unchecked(CheckedRunnable runnable) {
return () -> {
try {
runnable.run();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* Wrap a {@link CheckedConsumer} into a {@link Consumer}.
*
* @param consumer checked consumer
* @param item type
* @param checked exception type
* @return Consumer
*/
static Consumer unchecked(CheckedConsumer consumer) {
return t -> {
try {
consumer.accept(t);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* Wrap a {@link CheckedBiConsumer} into a {@link BiConsumer}.
*
* @param consumer checked consumer
* @param 1st item type
* @param 2nd item type
* @param checked exception type
* @return BiConsumer
*/
static BiConsumer unchecked(CheckedBiConsumer consumer) {
return (t, u) -> {
try {
consumer.accept(t, u);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
/**
* Wrap a {@link CheckedBiConsumer} into a {@link BiConsumer}.
*
* @param function checked function
* @param 1st item type
* @param 2nd item type
* @param checked exception type
* @return Function
*/
static Function unchecked(CheckedFunction function) {
return t -> {
try {
return function.apply(t);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy