
framework.Try Maven / Gradle / Ivy
package framework;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.sql.SQLException;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.LongConsumer;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
/**
* support lambda exception
*/
public class Try {
/**
* Exception catcher
*/
public static final Consumer catcher = e -> {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
if (e instanceof IOException) {
throw new UncheckedIOException((IOException) e);
}
if (e instanceof SQLException) {
throw new UncheckedSQLException((SQLException) e);
}
throw new RuntimeException(e);
};
/**
* throwable runnable
*/
@FunctionalInterface
public interface TryRunnable {
/**
* @throws Exception exception
*/
void run() throws Exception;
}
/**
* @param runnable throwable runnable
* @param error error action
* @return runnable
*/
public static Runnable r(TryRunnable runnable, Consumer error) {
return () -> {
try {
runnable.run();
} catch (Exception e) {
error.accept(e);
}
};
}
/**
* @param runnable throwable runnable
* @return runnable
*/
public static Runnable r(TryRunnable runnable) {
return r(runnable, catcher);
}
/**
* throwable consumer
*
* @param object type
*/
@FunctionalInterface
public interface TryConsumer {
/**
* @param t object
* @throws Exception exception
*/
void accept(T t) throws Exception;
}
/**
* @param value type
* @param consumer throwable consumer
* @param error error action
* @return consumer
*/
public static Consumer c(TryConsumer consumer, BiConsumer error) {
return value -> {
try {
consumer.accept(value);
} catch (Exception e) {
error.accept(e, value);
}
};
}
/**
* @param value type
* @param consumer throwable consumer
* @return consumer
*/
public static Consumer c(TryConsumer consumer) {
return c(consumer, (e, a) -> catcher.accept(e));
}
/**
* throwable consumer
*/
@FunctionalInterface
public interface TryIntConsumer {
/**
* @param t object
* @throws Exception exception
*/
void accept(int t) throws Exception;
}
/**
* @param consumer throwable consumer
* @param error error action
* @return consumer
*/
public static IntConsumer intC(TryIntConsumer consumer, ObjIntConsumer error) {
return t -> {
try {
consumer.accept(t);
} catch (Exception e) {
error.accept(e, t);
}
};
}
/**
* @param consumer throwable consumer
* @return consumer
*/
public static IntConsumer intC(TryIntConsumer consumer) {
return intC(consumer, (e, a) -> catcher.accept(e));
}
/**
* throwable consumer
*/
@FunctionalInterface
public interface TryLongConsumer {
/**
* @param t object
* @throws Exception exception
*/
void accept(long t) throws Exception;
}
/**
* @param consumer throwable consumer
* @param error error action
* @return consumer
*/
public static LongConsumer longC(TryLongConsumer consumer, ObjLongConsumer error) {
return t -> {
try {
consumer.accept(t);
} catch (Exception e) {
error.accept(e, t);
}
};
}
/**
* @param consumer throwable consumer
* @return consumer
*/
public static LongConsumer longC(TryLongConsumer consumer) {
return longC(consumer, (e, a) -> catcher.accept(e));
}
/**
* throwable consumer
*/
@FunctionalInterface
public interface TryDoubleConsumer {
/**
* @param t object
* @throws Exception exception
*/
void accept(double t) throws Exception;
}
/**
* @param consumer throwable consumer
* @param error error action
* @return consumer
*/
public static DoubleConsumer doubleC(TryDoubleConsumer consumer, ObjDoubleConsumer error) {
return t -> {
try {
consumer.accept(t);
} catch (Exception e) {
error.accept(e, t);
}
};
}
/**
* @param consumer throwable consumer
* @return consumer
*/
public static DoubleConsumer doubleC(TryDoubleConsumer consumer) {
return doubleC(consumer, (e, a) -> catcher.accept(e));
}
/**
* throwable consumer
*
* @param first object type
* @param second object type
*/
@FunctionalInterface
public interface TryBiConsumer {
/**
* @param t first object
* @param u second object
* @throws Exception exception
*/
void accept(T t, U u) throws Exception;
}
/**
* @param first object type
* @param second object type
* @param consumer throwable consumer
* @param error error action
* @return consumer
*/
public static BiConsumer biC(TryBiConsumer consumer, TriConsumer error) {
return (t, u) -> {
try {
consumer.accept(t, u);
} catch (Exception e) {
error.accept(e, t, u);
}
};
}
/**
* @param first object type
* @param second object type
* @param consumer throwable consumer
* @return consumer
*/
public static BiConsumer biC(TryBiConsumer consumer) {
return biC(consumer, (e, t, u) -> catcher.accept(e));
}
/**
* throwable consumer
*
* @param object type
* @param other object type
* @param other object type
*/
@FunctionalInterface
public interface TriConsumer {
/**
* @param t object
* @param u other object
* @param v other object
*/
void accept(T t, U u, V v);
}
/**
* throwable consumer
*
* @param object type
* @param other object type
* @param other object type
*/
@FunctionalInterface
public interface TryTriConsumer {
/**
* @param t object
* @param u other object
* @param v other object
* @throws Exception exception
*/
void accept(T t, U u, V v) throws Exception;
}
/**
* 4 consumer
*
* @param object type
* @param other object type
* @param other object type
* @param other object type
*/
@FunctionalInterface
public interface QuadConsumer {
/**
* @param t object
* @param u other object
* @param v other object
* @param w other object
*/
void accept(T t, U u, V v, W w);
}
/**
* @param consumer consumer
* @param error error action
* @return consumer
* @param object type
* @param other object type
* @param other object type
*/
public static TriConsumer triC(TryTriConsumer consumer, QuadConsumer error) {
return (t, u, v) -> {
try {
consumer.accept(t, u, v);
} catch (Exception e) {
error.accept(e, t, u, v);
}
};
}
/**
* @param consumer consumer
* @return consumer
* @param object type
* @param other object type
* @param other object type
*/
public static TriConsumer triC(TryTriConsumer consumer) {
return triC(consumer, (e, t, u, v) -> catcher.accept(e));
}
/**
* throwable consumer
*
* @param object type
*/
@FunctionalInterface
public interface TryObjIntConsumer {
/**
* @param t object
* @param u int value
* @throws Exception exception
*/
void accept(T t, int u) throws Exception;
}
/**
* throwable consumer
*
* @param object type
* @param other object type
*/
@FunctionalInterface
public interface ObjObjIntConsumer {
/**
* @param t object
* @param u other value
* @param i integer value
*/
void accept(T t, U u, int i);
}
/**
* @param object type
* @param consumer throwable consumer
* @param error error action
* @return consumer
*/
public static ObjIntConsumer intC(TryObjIntConsumer consumer, ObjObjIntConsumer error) {
return (t, i) -> {
try {
consumer.accept(t, i);
} catch (Exception e) {
error.accept(e, t, i);
}
};
}
/**
* @param object type
* @param consumer throwable consumer
* @return consumer
*/
public static ObjIntConsumer intC(TryObjIntConsumer consumer) {
return intC(consumer, (e, t, i) -> catcher.accept(e));
}
/**
* throwable function
*
* @param argument type
* @param return object type
*/
@FunctionalInterface
public interface TryFunction {
/**
* @param t object
* @return value
* @throws Exception exception
*/
R apply(A t) throws Exception;
}
/**
* @param argument type
* @param return type
* @param function throwable function
* @param error error action
* @return function
*/
public static Function f(TryFunction function, BiFunction error) {
return argument -> {
try {
return function.apply(argument);
} catch (Exception e) {
return error.apply(e, argument);
}
};
}
/**
* @param argument type
* @param return type
* @param function throwable function
* @return function
*/
public static Function f(TryFunction function) {
return f(function, (e, a) -> {
catcher.accept(e);
return null;
});
}
/**
* throwable function
*
* @param return object type
*/
@FunctionalInterface
public interface TryIntFunction {
/**
* @param t object
* @return value
* @throws Exception exception
*/
R apply(int t) throws Exception;
}
/**
* @param return type
* @param function throwable function
* @param error error action
* @return function
*/
public static IntFunction intF(TryIntFunction function, ObjIntConsumer error) {
return i -> {
try {
return function.apply(i);
} catch (Exception e) {
error.accept(e, i);
return null;
}
};
}
/**
* @param return type
* @param function throwable function
* @return function
*/
public static IntFunction intF(TryIntFunction function) {
return intF(function, (e, i) -> catcher.accept(e));
}
/**
* throwable function
*
* @param parameter type
*/
@FunctionalInterface
public interface TryToIntFunction {
/**
* @param t object
* @return value
* @throws Exception exception
*/
int apply(T t) throws Exception;
}
/**
* @param parameter type
* @param function throwable function
* @param error error action
* @return function
*/
public static ToIntFunction toIntF(TryToIntFunction function, BiConsumer error) {
return i -> {
try {
return function.apply(i);
} catch (Exception e) {
error.accept(e, i);
return 0;
}
};
}
/**
* @param parameter type
* @param function throwable function
* @return function
*/
public static ToIntFunction toIntF(TryToIntFunction function) {
return toIntF(function, (e, i) -> catcher.accept(e));
}
/**
* throwable function
*
* @param first argument type
* @param second argument type
* @param return type
*/
@FunctionalInterface
public interface TryBiFunction {
/**
* @param t first argument
* @param u second argument
* @return value
* @throws Exception exception
*/
R apply(T t, U u) throws Exception;
}
/**
* @param first argument type
* @param second argument type
* @param return type
* @param function throwable function
* @param error error action
* @return BiFunction
*/
public static BiFunction biF(TryBiFunction function, TriFunction error) {
return (t, u) -> {
try {
return function.apply(t, u);
} catch (Exception e) {
return error.apply(e, t, u);
}
};
}
/**
* @param first argument type
* @param second argument type
* @param return type
* @param function throwable function
* @return BiFunction
*/
public static BiFunction biF(TryBiFunction function) {
return biF(function, (e, t, u) -> {
catcher.accept(e);
return null;
});
}
/**
* throwable function
*
* @param return object type
*/
@FunctionalInterface
public interface TrySupplier {
/**
* @return value
* @throws Exception exception
*/
R get() throws Exception;
}
/**
* @param return type
* @param function throwable function
* @param error error action
* @return function
*/
public static Supplier s(TrySupplier function, Function error) {
return () -> {
try {
return function.get();
} catch (Exception e) {
return error.apply(e);
}
};
}
/**
* @param return type
* @param function throwable function
* @return function
*/
public static Supplier s(TrySupplier function) {
return s(function, e -> {
catcher.accept(e);
return null;
});
}
/**
* throwable consumer
*
* @param object type
*/
@FunctionalInterface
public interface TryPredicate {
/**
* @param t object type
* @return result
* @throws Exception exception
*/
boolean test(T t) throws Exception;
}
/**
* @param object type
* @param predicate predicate
* @param error error action
* @return consumer
*/
public static Predicate p(TryPredicate predicate, BiPredicate error) {
return t -> {
try {
return predicate.test(t);
} catch (Exception e) {
return error.test(e, t);
}
};
}
/**
* @param object type
* @param predicate predicate
* @return consumer
*/
public static Predicate p(TryPredicate predicate) {
return p(predicate, (e, t) -> {
catcher.accept(e);
return false;
});
}
/**
* Tri function
*
* @param object type
* @param other object type
* @param other object type
* @param other object type
*/
@FunctionalInterface
public interface TriFunction {
/**
* @param t object
* @param u other object
* @param v other object
* @return return object
*/
R apply(T t, U u, V v);
}
/**
* Quad function
*
* @param object type
* @param other object type
* @param other object type
* @param other object type
* @param return object type
*/
@FunctionalInterface
public interface QuadFunction {
/**
* @param t object
* @param u other object
* @param v other object
* @param w other object
* @return return object
*/
R apply(T t, U u, V v, W w);
}
/**
* Penta function
*
* @param object type
* @param other object type
* @param other object type
* @param other object type
* @param other object type
* @param return object type
*/
@FunctionalInterface
public interface PentaFunction {
/**
* @param t object
* @param u other object
* @param v other object
* @param w other object
* @param x other object
* @return return object
*/
R apply(T t, U u, V v, W w, X x);
}
/**
* R(T, int) function
*
* @param first argument type
* @param return type
*/
@FunctionalInterface
public interface ObjIntFunction {
/**
* @param t first argument
* @param i second argument
* @return value
*/
R apply(T t, int i);
}
/**
* R(int, int) function
*
* @param return type
*/
@FunctionalInterface
public interface BiIntFunction {
/**
* @param i first argument
* @param j second argument
* @return value
*/
R apply(int i, int j);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy