com.landawn.abacus.util.Throwables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (C) 2019 HaiYang Li
*
* 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 com.landawn.abacus.util;
/**
* Catch checked exception and convert it to RuntimeException
.
*
* @author Haiyang Li
* @param
* @since 0.8
*/
public final class Throwables {
private Throwables() {
// Singleton for utility class.
}
/**
*
* @param cmd
* @throws RuntimeException if some error happens
*/
public static void run(final Throwables.Runnable extends Throwable> cmd) {
try {
cmd.run();
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e);
}
}
/**
*
* @param cmd
* @param actionOnError
*/
public static void run(final Throwables.Runnable extends Throwable> cmd,
final com.landawn.abacus.util.function.Consumer super Throwable> actionOnError) {
N.checkArgNotNull(actionOnError);
try {
cmd.run();
} catch (Throwable e) {
actionOnError.accept(e);
}
}
/**
*
* @param
* @param cmd
* @return
* @throws RuntimeException if some error happens
*/
public static R call(final Throwables.Callable cmd) {
try {
return cmd.call();
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e);
}
}
/**
*
* @param
* @param cmd
* @param actionOnError
* @return
*/
public static R call(final Throwables.Callable cmd,
final com.landawn.abacus.util.function.Function super Throwable, R> actionOnError) {
N.checkArgNotNull(actionOnError);
try {
return cmd.call();
} catch (Throwable e) {
return actionOnError.apply(e);
}
}
/**
*
* @param
* @param cmd
* @param supplier
* @return
*/
public static R call(final Throwables.Callable cmd, final com.landawn.abacus.util.function.Supplier supplier) {
N.checkArgNotNull(supplier);
try {
return cmd.call();
} catch (Throwable e) {
return supplier.get();
}
}
/**
*
* @param
* @param cmd
* @param defaultValue
* @return
*/
public static R call(final Throwables.Callable cmd, final R defaultValue) {
try {
return cmd.call();
} catch (Throwable e) {
return defaultValue;
}
}
/**
*
* @param
* @param cmd
* @param predicate
* @param supplier
* @return
* @throws RuntimeException if some error happens and predicate
return false.
*/
public static R call(final Throwables.Callable cmd,
final com.landawn.abacus.util.function.Predicate super Throwable> predicate, final com.landawn.abacus.util.function.Supplier supplier) {
N.checkArgNotNull(predicate);
N.checkArgNotNull(supplier);
try {
return cmd.call();
} catch (Throwable e) {
if (predicate.test(e)) {
return supplier.get();
} else {
throw ExceptionUtil.toRuntimeException(e);
}
}
}
/**
*
* @param
* @param cmd
* @param predicate
* @param defaultValue
* @return
* @throws RuntimeException if some error happens and predicate
return false.
*/
public static R call(final Throwables.Callable cmd,
final com.landawn.abacus.util.function.Predicate super Throwable> predicate, final R defaultValue) {
N.checkArgNotNull(predicate);
try {
return cmd.call();
} catch (Throwable e) {
if (predicate.test(e)) {
return defaultValue;
} else {
throw ExceptionUtil.toRuntimeException(e);
}
}
}
/**
* The Interface Runnable.
*
* @param
*/
public interface Runnable {
/**
*
* @throws E the e
*/
void run() throws E;
}
/**
* The Interface Callable.
*
* @param
* @param
*/
public interface Callable {
/**
*
* @return
* @throws E the e
*/
R call() throws E;
}
/**
* The Interface Supplier.
*
* @param
* @param
*/
public interface Supplier {
/**
*
* @return
* @throws E the e
*/
T get() throws E;
}
/**
* The Interface BooleanSupplier.
*
* @param
*/
public interface BooleanSupplier {
/**
* Gets the as boolean.
*
* @return
* @throws E the e
*/
boolean getAsBoolean() throws E;
}
/**
* The Interface CharSupplier.
*
* @param
*/
public interface CharSupplier {
/**
* Gets the as char.
*
* @return
* @throws E the e
*/
char getAsChar() throws E;
}
/**
* The Interface ByteSupplier.
*
* @param
*/
public interface ByteSupplier {
/**
* Gets the as byte.
*
* @return
* @throws E the e
*/
byte getAsByte() throws E;
}
/**
* The Interface ShortSupplier.
*
* @param
*/
public interface ShortSupplier {
/**
* Gets the as short.
*
* @return
* @throws E the e
*/
short getAsShort() throws E;
}
/**
* The Interface IntSupplier.
*
* @param
*/
public interface IntSupplier {
/**
* Gets the as int.
*
* @return
* @throws E the e
*/
int getAsInt() throws E;
}
/**
* The Interface LongSupplier.
*
* @param
*/
public interface LongSupplier {
/**
* Gets the as long.
*
* @return
* @throws E the e
*/
long getAsLong() throws E;
}
/**
* The Interface FloatSupplier.
*
* @param
*/
public interface FloatSupplier {
/**
* Gets the as float.
*
* @return
* @throws E the e
*/
float getAsFloat() throws E;
}
/**
* The Interface DoubleSupplier.
*
* @param
*/
public interface DoubleSupplier {
/**
* Gets the as double.
*
* @return
* @throws E the e
*/
double getAsDouble() throws E;
}
/**
* The Interface Predicate.
*
* @param
* @param
*/
public interface Predicate {
/**
*
* @param t
* @return
* @throws E the e
*/
boolean test(T t) throws E;
}
/**
* The Interface BiPredicate.
*
* @param
* @param
* @param
*/
public interface BiPredicate {
/**
*
* @param t
* @param u
* @return
* @throws E the e
*/
boolean test(T t, U u) throws E;
}
/**
* The Interface TriPredicate.
*
* @param
* @param
* @param
* @param
*/
public interface TriPredicate {
/**
*
* @param a
* @param b
* @param c
* @return
* @throws E the e
*/
boolean test(A a, B b, C c) throws E;
}
/**
* The Interface QuadPredicate.
*
* @param
* @param
* @param
* @param
* @param
*/
public interface QuadPredicate {
/**
*
* @param a
* @param b
* @param c
* @param d
* @return
* @throws E the e
*/
boolean test(A a, B b, C c, D d) throws E;
}
/**
* The Interface Function.
*
* @param
* @param
* @param
*/
public interface Function {
/**
*
* @param t
* @return
* @throws E the e
*/
R apply(T t) throws E;
}
/**
* The Interface BiFunction.
*
* @param
* @param
* @param
* @param
*/
public interface BiFunction {
/**
*
* @param t
* @param u
* @return
* @throws E the e
*/
R apply(T t, U u) throws E;
}
/**
* The Interface TriFunction.
*
* @param
* @param
* @param
* @param
* @param
*/
public interface TriFunction {
/**
*
* @param a
* @param b
* @param c
* @return
* @throws E the e
*/
R apply(A a, B b, C c) throws E;
}
/**
* The Interface QuadFunction.
*
* @param
* @param
* @param
* @param
* @param
* @param
*/
public interface QuadFunction {
/**
*
* @param a
* @param b
* @param c
* @param d
* @return
* @throws E the e
*/
R apply(A a, B b, C c, D d) throws E;
}
/**
* The Interface Consumer.
*
* @param
* @param
*/
public interface Consumer {
/**
*
* @param t
* @throws E the e
*/
void accept(T t) throws E;
}
/**
* The Interface BiConsumer.
*
* @param
* @param
* @param
*/
public interface BiConsumer {
/**
*
* @param t
* @param u
* @throws E the e
*/
void accept(T t, U u) throws E;
}
/**
* The Interface TriConsumer.
*
* @param
* @param
* @param
* @param
*/
public interface TriConsumer {
/**
*
* @param a
* @param b
* @param c
* @throws E the e
*/
void accept(A a, B b, C c) throws E;
}
/**
* The Interface QuadConsumer.
*
* @param
* @param
* @param
* @param
* @param
*/
public interface QuadConsumer {
/**
*
* @param a
* @param b
* @param c
* @param d
* @throws E the e
*/
void accept(A a, B b, C c, D d) throws E;
}
/**
* The Interface IndexedConsumer.
*
* @param
* @param
*/
public interface IndexedConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, T e) throws E;
}
/**
* The Interface IndexedBiConsumer.
*
* @param
* @param
* @param
*/
public interface IndexedBiConsumer {
/**
*
* @param u
* @param idx
* @param e
* @throws E the e
*/
void accept(U u, int idx, T e) throws E;
}
/**
* The Interface IndexedFunction.
*
* @param
* @param
* @param
*/
public interface IndexedFunction {
/**
*
* @param idx
* @param e
* @return
* @throws E the e
*/
R apply(int idx, T e) throws E;
}
/**
* The Interface IndexedBiFunction.
*
* @param
* @param
* @param
* @param
*/
public interface IndexedBiFunction {
/**
*
* @param u
* @param idx
* @param e
* @return
* @throws E the e
*/
R apply(U u, int idx, T e) throws E;
}
/**
* The Interface IndexedPredicate.
*
* @param
* @param
*/
public interface IndexedPredicate {
/**
*
* @param idx
* @param e
* @return
* @throws E the e
*/
boolean test(int idx, T e) throws E;
}
/**
* The Interface IndexedBiPredicate.
*
* @param
* @param
* @param
*/
public interface IndexedBiPredicate {
/**
*
* @param u
* @param idx
* @param e
* @return
* @throws E the e
*/
boolean test(U u, int idx, T e) throws E;
}
/**
* The Interface IndexedBooleanConsumer.
*
* @param
*/
public interface IndexedBooleanConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, boolean e) throws E;
}
/**
* The Interface IndexedCharConsumer.
*
* @param
*/
public interface IndexedCharConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, char e) throws E;
}
/**
* The Interface IndexedByteConsumer.
*
* @param
*/
public interface IndexedByteConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, byte e) throws E;
}
/**
* The Interface IndexedShortConsumer.
*
* @param
*/
public interface IndexedShortConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, short e) throws E;
}
/**
* The Interface IndexedIntConsumer.
*
* @param
*/
public interface IndexedIntConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, int e) throws E;
}
/**
* The Interface IndexedLongConsumer.
*
* @param
*/
public interface IndexedLongConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, long e) throws E;
}
/**
* The Interface IndexedFloatConsumer.
*
* @param
*/
public interface IndexedFloatConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, float e) throws E;
}
/**
* The Interface IndexedDoubleConsumer.
*
* @param
*/
public interface IndexedDoubleConsumer {
/**
*
* @param idx
* @param e
* @throws E the e
*/
void accept(int idx, double e) throws E;
}
/**
* The Interface BooleanPredicate.
*
* @param
*/
public interface BooleanPredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(boolean value) throws E;
}
/**
* The Interface BooleanFunction.
*
* @param
* @param
*/
public interface BooleanFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(boolean value) throws E;
}
/**
* The Interface BooleanConsumer.
*
* @param
*/
public interface BooleanConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(boolean t) throws E;
}
/**
* The Interface CharPredicate.
*
* @param
*/
public interface CharPredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(char value) throws E;
}
/**
* The Interface CharFunction.
*
* @param
* @param
*/
public interface CharFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(char value) throws E;
}
/**
* The Interface CharConsumer.
*
* @param
*/
public interface CharConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(char t) throws E;
}
/**
* The Interface BytePredicate.
*
* @param
*/
public interface BytePredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(byte value) throws E;
}
/**
* The Interface ByteFunction.
*
* @param
* @param
*/
public interface ByteFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(byte value) throws E;
}
/**
* The Interface ByteConsumer.
*
* @param
*/
public interface ByteConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(byte t) throws E;
}
/**
* The Interface ShortPredicate.
*
* @param
*/
public interface ShortPredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(short value) throws E;
}
/**
* The Interface ShortFunction.
*
* @param
* @param
*/
public interface ShortFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(short value) throws E;
}
/**
* The Interface ShortConsumer.
*
* @param
*/
public interface ShortConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(short t) throws E;
}
/**
* The Interface IntPredicate.
*
* @param
*/
public interface IntPredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(int value) throws E;
}
/**
* The Interface IntFunction.
*
* @param
* @param
*/
public interface IntFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(int value) throws E;
}
/**
* The Interface IntConsumer.
*
* @param
*/
public interface IntConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(int t) throws E;
}
/**
* The Interface LongPredicate.
*
* @param
*/
public interface LongPredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(long value) throws E;
}
/**
* The Interface LongFunction.
*
* @param
* @param
*/
public interface LongFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(long value) throws E;
}
/**
* The Interface LongConsumer.
*
* @param
*/
public interface LongConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(long t) throws E;
}
/**
* The Interface FloatPredicate.
*
* @param
*/
public interface FloatPredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(float value) throws E;
}
/**
* The Interface FloatFunction.
*
* @param
* @param
*/
public interface FloatFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(float value) throws E;
}
/**
* The Interface FloatConsumer.
*
* @param
*/
public interface FloatConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(float t) throws E;
}
/**
* The Interface DoublePredicate.
*
* @param
*/
public interface DoublePredicate {
/**
*
* @param value
* @return
* @throws E the e
*/
boolean test(double value) throws E;
}
/**
* The Interface DoubleFunction.
*
* @param
* @param
*/
public interface DoubleFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
R apply(double value) throws E;
}
/**
* The Interface DoubleConsumer.
*
* @param
*/
public interface DoubleConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(double t) throws E;
}
/**
* The Interface ToBooleanFunction.
*
* @param
* @param
*/
public interface ToBooleanFunction {
/**
* Apply as boolean.
*
* @param t
* @return
* @throws E the e
*/
boolean applyAsBoolean(T t) throws E;
}
/**
* The Interface ToCharFunction.
*
* @param
* @param
*/
public interface ToCharFunction {
/**
* Apply as char.
*
* @param t
* @return
* @throws E the e
*/
char applyAsChar(T t) throws E;
}
/**
* The Interface ToByteFunction.
*
* @param
* @param
*/
public interface ToByteFunction {
/**
* Apply as byte.
*
* @param t
* @return
* @throws E the e
*/
byte applyAsByte(T t) throws E;
}
/**
* The Interface ToShortFunction.
*
* @param
* @param
*/
public interface ToShortFunction {
/**
* Apply as short.
*
* @param t
* @return
* @throws E the e
*/
short applyAsShort(T t) throws E;
}
/**
* The Interface ToIntFunction.
*
* @param
* @param
*/
public interface ToIntFunction {
/**
* Apply as int.
*
* @param t
* @return
* @throws E the e
*/
int applyAsInt(T t) throws E;
}
/**
* The Interface ToLongFunction.
*
* @param
* @param
*/
public interface ToLongFunction {
/**
* Apply as long.
*
* @param t
* @return
* @throws E the e
*/
long applyAsLong(T t) throws E;
}
/**
* The Interface ToFloatFunction.
*
* @param
* @param
*/
public interface ToFloatFunction {
/**
* Apply as float.
*
* @param t
* @return
* @throws E the e
*/
float applyAsFloat(T t) throws E;
}
/**
* The Interface ToDoubleFunction.
*
* @param
* @param
*/
public interface ToDoubleFunction {
/**
* Apply as double.
*
* @param t
* @return
* @throws E the e
*/
double applyAsDouble(T t) throws E;
}
/**
* The Interface UnaryOperator.
*
* @param
* @param
*/
public interface UnaryOperator extends Function {
}
/**
* The Interface BinaryOperator.
*
* @param
* @param
*/
public interface BinaryOperator extends BiFunction {
}
/**
* The Interface TernaryOperator.
*
* @param
* @param
*/
public interface TernaryOperator extends BiFunction {
}
/**
* The Interface BooleanUnaryOperator.
*
* @param
*/
public interface BooleanUnaryOperator {
/**
* Apply as boolean.
*
* @param operand
* @return
* @throws E the e
*/
boolean applyAsBoolean(boolean operand) throws E;
}
/**
* The Interface CharUnaryOperator.
*
* @param
*/
public interface CharUnaryOperator {
/**
* Apply as char.
*
* @param operand
* @return
* @throws E the e
*/
char applyAsChar(char operand) throws E;
}
/**
* The Interface ByteUnaryOperator.
*
* @param
*/
public interface ByteUnaryOperator {
/**
* Apply as byte.
*
* @param operand
* @return
* @throws E the e
*/
byte applyAsByte(byte operand) throws E;
}
/**
* The Interface ShortUnaryOperator.
*
* @param
*/
public interface ShortUnaryOperator {
/**
* Apply as short.
*
* @param operand
* @return
* @throws E the e
*/
short applyAsShort(short operand) throws E;
}
/**
* The Interface IntUnaryOperator.
*
* @param
*/
public interface IntUnaryOperator {
/**
* Apply as int.
*
* @param operand
* @return
* @throws E the e
*/
int applyAsInt(int operand) throws E;
}
/**
* The Interface LongUnaryOperator.
*
* @param
*/
public interface LongUnaryOperator {
/**
* Apply as long.
*
* @param operand
* @return
* @throws E the e
*/
long applyAsLong(long operand) throws E;
}
/**
* The Interface FloatUnaryOperator.
*
* @param
*/
public interface FloatUnaryOperator {
/**
* Apply as float.
*
* @param operand
* @return
* @throws E the e
*/
float applyAsFloat(float operand) throws E;
}
/**
* The Interface DoubleUnaryOperator.
*
* @param
*/
public interface DoubleUnaryOperator {
/**
* Apply as double.
*
* @param operand
* @return
* @throws E the e
*/
double applyAsDouble(double operand) throws E;
}
/**
* The Interface BooleanBinaryOperator.
*
* @param
*/
public interface BooleanBinaryOperator {
/**
* Apply as boolean.
*
* @param left
* @param right
* @return
* @throws E the e
*/
boolean applyAsBoolean(boolean left, boolean right) throws E;
}
/**
* The Interface CharBinaryOperator.
*
* @param
*/
public interface CharBinaryOperator {
/**
* Apply as char.
*
* @param left
* @param right
* @return
* @throws E the e
*/
char applyAsChar(char left, char right) throws E;
}
/**
* The Interface ByteBinaryOperator.
*
* @param