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;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.u.Nullable;
/**
* Catch checked exception and convert it to RuntimeException
.
*
* @author Haiyang Li
*/
@SuppressWarnings({ "java:S6539" })
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, true);
}
}
/**
*
* @param cmd
* @param actionOnError
*/
public static void run(final Throwables.Runnable extends Throwable> cmd, final java.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, true);
}
}
/**
*
* @param
* @param cmd
* @param actionOnError
* @return
*/
public static R call(final Throwables.Callable cmd,
final java.util.function.Function super Throwable, ? extends 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 java.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 java.util.function.Predicate super Throwable> predicate,
final java.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, true);
}
}
}
/**
*
* @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 java.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, true);
}
}
}
@SuppressWarnings({ "java:S6548" })
public abstract static class ObjIterator implements Immutable {
@SuppressWarnings("rawtypes")
private static final ObjIterator EMPTY = new ObjIterator() {
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
};
/**
*
*
* @param
* @param
* @return
*/
public static ObjIterator empty() {
return EMPTY;
}
/**
*
*
* @param
* @param
* @param val
* @return
*/
public static ObjIterator just(final T val) {
return new ObjIterator<>() {
private boolean done = false;
@Override
public boolean hasNext() {
return !done;
}
@Override
public T next() {
if (done) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
done = true;
return val;
}
};
}
/**
*
*
* @param
* @param
* @param iterable
* @return
*/
public static ObjIterator of(final Iterable extends T> iterable) {
if (iterable == null) {
return empty();
}
final Iterator extends T> iter = iterable.iterator();
return new ObjIterator<>() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public T next() throws E {
return iter.next();
}
};
}
/**
*
*
* @return
* @throws E
*/
public abstract boolean hasNext() throws E;
/**
*
*
* @return
* @throws E
*/
public abstract T next() throws E;
/**
*
*
* @param predicate
* @return
*/
public ObjIterator filter(final Throwables.Predicate super T, E> predicate) {
final ObjIterator iter = this;
return new ObjIterator<>() {
private final T NONE = (T) N.NULL_MASK; //NOSONAR
private T next = NONE;
private T tmp = null;
@Override
public boolean hasNext() throws E {
if (next == NONE) {
while (iter.hasNext()) {
tmp = iter.next();
if (predicate.test(tmp)) {
next = tmp;
break;
}
}
}
return next != NONE;
}
@Override
public T next() throws E {
if (!hasNext()) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
tmp = next;
next = NONE;
return tmp;
}
};
}
/**
*
*
* @param
* @param mapper
* @return
*/
public ObjIterator map(final Throwables.Function super T, U, E> mapper) {
final ObjIterator iter = this;
return new ObjIterator<>() {
@Override
public boolean hasNext() throws E {
return iter.hasNext();
}
@Override
public U next() throws E {
return mapper.apply(iter.next());
}
};
}
/**
*
*
* @return
* @throws E
*/
public Nullable first() throws E {
if (hasNext()) {
return Nullable.of(next());
} else {
return Nullable. empty();
}
}
/**
*
*
* @return
* @throws E
*/
public u.Optional firstNonNull() throws E {
T next = null;
while (hasNext()) {
next = next();
if (next != null) {
return u.Optional.of(next);
}
}
return u.Optional.empty();
}
/**
*
*
* @return
* @throws E
*/
public Nullable last() throws E {
if (hasNext()) {
T next = next();
while (hasNext()) {
next = next();
}
return Nullable.of(next);
} else {
return Nullable. empty();
}
}
/**
*
*
* @return
* @throws E
*/
public Object[] toArray() throws E {
return toArray(N.EMPTY_OBJECT_ARRAY);
}
/**
*
*
* @param
* @param a
* @return
* @throws E
*/
public A[] toArray(A[] a) throws E {
return toList().toArray(a);
}
/**
*
*
* @return
* @throws E
*/
public List toList() throws E {
final List list = new ArrayList<>();
while (hasNext()) {
list.add(next());
}
return list;
}
/**
*
*
* @param action
* @throws E
*/
public void forEachRemaining(java.util.function.Consumer super T> action) throws E { // NOSONAR
N.checkArgNotNull(action);
while (hasNext()) {
action.accept(next());
}
}
/**
*
* @param action
* @throws E the e
*/
public void foreachRemaining(Throwables.Consumer super T, E> action) throws E { // NOSONAR
N.checkArgNotNull(action);
while (hasNext()) {
action.accept(next());
}
}
/**
*
* @param action
* @throws E the e
*/
public void foreachIndexed(Throwables.IntObjConsumer super T, E> action) throws E {
N.checkArgNotNull(action);
int idx = 0;
while (hasNext()) {
action.accept(idx++, next());
}
}
}
/**
* The Interface Runnable.
*
* @param
*/
public interface Runnable {
/**
*
* @throws E the e
*/
void run() throws E;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.Runnable unchecked() {
return () -> {
try {
run();
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* The Interface Callable.
*
* @param
* @param
*/
public interface Callable {
/**
*
* @return
* @throws E the e
*/
R call() throws E;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.Callable unchecked() {
return () -> {
try {
return call();
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* The Interface Supplier.
*
* @param
* @param
*/
public interface Supplier {
/**
*
* @return
* @throws E the e
*/
T get() throws E;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.Supplier unchecked() {
return () -> {
try {
return get();
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* The Interface BooleanSupplier.
*
* @param
*/
public interface BooleanSupplier {
/**
* Gets the as boolean.
*
* @return
* @throws E the e
*/
boolean getAsBoolean() throws E; // NOSONAR
}
/**
* 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;
/**
*
*
* @return
*/
default Predicate negate() {
return t -> !test(t);
}
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.Predicate unchecked() {
return t -> {
try {
return test(t);
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* 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;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.BiPredicate unchecked() {
return (t, u) -> {
try {
return test(t, u);
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* 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;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.Function unchecked() {
return t -> {
try {
return apply(t);
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* 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;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.BiFunction unchecked() {
return (t, u) -> {
try {
return apply(t, u);
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* 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;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.Consumer unchecked() {
return t -> {
try {
accept(t);
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* 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;
/**
*
*
* @return
*/
@Beta
default com.landawn.abacus.util.function.BiConsumer unchecked() {
return (t, u) -> {
try {
accept(t, u);
} catch (Throwable e) {
throw ExceptionUtil.toRuntimeException(e, true);
}
};
}
}
/**
* 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 BooleanConsumer.
*
* @param
*/
public interface BooleanConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(boolean t) 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 CharConsumer.
*
* @param
*/
public interface CharConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(char 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 ByteConsumer.
*
* @param
*/
public interface ByteConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(byte 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 ShortConsumer.
*
* @param
*/
public interface ShortConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(short 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 IntConsumer.
*
* @param
*/
public interface IntConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(int 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 IntToLongFunction.
*
* @param
*/
public interface IntToLongFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
long applyAsLong(int value) throws E;
}
/**
* The Interface IntToDoubleFunction.
*
* @param
*/
public interface IntToDoubleFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
double applyAsDouble(int value) throws E;
}
/**
* The Interface LongConsumer.
*
* @param
*/
public interface LongConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(long 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 LongToIntFunction.
*
* @param
*/
public interface LongToIntFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
int applyAsInt(long value) throws E;
}
/**
* The Interface LongToDoubleFunction.
*
* @param
*/
public interface LongToDoubleFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
double applyAsDouble(long value) throws E;
}
/**
* The Interface FloatConsumer.
*
* @param
*/
public interface FloatConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(float 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 DoubleConsumer.
*
* @param
*/
public interface DoubleConsumer {
/**
*
* @param t
* @throws E the e
*/
void accept(double 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 DoubleToIntFunction.
*
* @param
*/
public interface DoubleToIntFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
int applyAsInt(double value) throws E;
}
/**
* The Interface DoubleToLongFunction.
*
* @param
*/
public interface DoubleToLongFunction {
/**
*
* @param value
* @return
* @throws E the e
*/
long applyAsLong(double value) 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 TriIntFunction.
*
* @param
* @param
* @param
*/
public interface ToIntBiFunction {
/**
*
* @param a
* @param b
* @return
* @throws E the e
*/
int applyAsInt(A a, B b) throws E;
}
/**
* The Interface TriLongFunction.
*
* @param
* @param
* @param
*/
public interface ToLongBiFunction {
/**
*
* @param a
* @param b
* @return
* @throws E the e
*/
long applyAsLong(A a, B b) throws E;
}
/**
* The Interface TriDoubleFunction.
*
* @param
* @param
* @param
*/
public interface ToDoubleBiFunction {
/**
*
* @param a
* @param b
* @return
* @throws E the e
*/
double applyAsDouble(A a, B b) throws E;
}
/**
* The Interface TriIntFunction.
*
* @param
* @param
* @param
* @param
*/
public interface ToIntTriFunction {
/**
*
* @param a
* @param b
* @param c
* @return
* @throws E the e
*/
int applyAsInt(A a, B b, C c) throws E;
}
/**
* The Interface TriLongFunction.
*
* @param
* @param
* @param
* @param
*/
public interface ToLongTriFunction {
/**
*
* @param a
* @param b
* @param c
* @return
* @throws E the e
*/
long applyAsLong(A a, B b, C c) throws E;
}
/**
* The Interface TriDoubleFunction.
*
* @param
* @param
* @param
* @param
*/
public interface ToDoubleTriFunction {
/**
*
* @param a
* @param b
* @param c
* @return
* @throws E the e
*/
double applyAsDouble(A a, B b, C c) throws E;
}
/**
* The Interface UnaryOperator.
*
* @param
* @param
*/
public interface UnaryOperator