public final class Sneaky extends Object
Similar to Unchecked, except that Unchecked.RETHROW_ALL is
used as the default way to re-throw checked exceptions.
public static void throwChecked(Throwable t)
public static Runnable runnable(CheckedRunnable runnable)
CheckedRunnable in a Runnable.
Example:
new Thread(Unchecked.runnable(() -> {
throw new Exception("Cannot run this thread");
})).start();
public static <T> Callable<T> callable(CheckedCallable<T> callable)
CheckedCallable in a Callable.
Example:
Executors.newFixedThreadPool(1).submit(Unchecked.callable(() -> {
throw new Exception("Cannot execute this task");
})).get();
public static <T> Comparator<T> comparator(CheckedComparator<T> comparator)
CheckedComparator in a Comparator.public static <T,U> BiConsumer<T,U> biConsumer(CheckedBiConsumer<T,U> consumer)
CheckedBiConsumer in a BiConsumer.
Example:
map.forEach(Unchecked.biConsumer((k, v) -> {
if (k == null || v == null)
throw new Exception("No nulls allowed in map");
}));
public static <T> ObjIntConsumer<T> objIntConsumer(CheckedObjIntConsumer<T> consumer)
CheckedObjIntConsumer in a ObjIntConsumer.public static <T> ObjLongConsumer<T> objLongConsumer(CheckedObjLongConsumer<T> consumer)
CheckedObjLongConsumer in a ObjLongConsumer.public static <T> ObjDoubleConsumer<T> objDoubleConsumer(CheckedObjDoubleConsumer<T> consumer)
CheckedObjDoubleConsumer in a ObjDoubleConsumer.public static <T,U,R> BiFunction<T,U,R> biFunction(CheckedBiFunction<T,U,R> function)
CheckedBiFunction in a BiFunction.
Example:
map.computeIfPresent("key", Unchecked.biFunction((k, v) -> {
if (k == null || v == null)
throw new Exception("No nulls allowed in map");
return 42;
}));
public static <T,U> ToIntBiFunction<T,U> toIntBiFunction(CheckedToIntBiFunction<T,U> function)
CheckedToIntBiFunction in a ToIntBiFunction.public static <T,U> ToLongBiFunction<T,U> toLongBiFunction(CheckedToLongBiFunction<T,U> function)
CheckedToLongBiFunction in a ToLongBiFunction.public static <T,U> ToDoubleBiFunction<T,U> toDoubleBiFunction(CheckedToDoubleBiFunction<T,U> function)
CheckedToDoubleBiFunction in a ToDoubleBiFunction.public static <T,U> BiPredicate<T,U> biPredicate(CheckedBiPredicate<T,U> predicate)
CheckedBiPredicate in a BiPredicate.public static <T> BinaryOperator<T> binaryOperator(CheckedBinaryOperator<T> operator)
CheckedBinaryOperator in a BinaryOperator.
Example:
Stream.of("a", "b", "c").reduce(Unchecked.binaryOperator((s1, s2) -> {
if (s2.length() > 10)
throw new Exception("Only short strings allowed");
return s1 + s2;
}));
public static IntBinaryOperator intBinaryOperator(CheckedIntBinaryOperator operator)
CheckedIntBinaryOperator in a IntBinaryOperator.
Example:
IntStream.of(1, 2, 3).reduce(Unchecked.intBinaryOperator((i1, i2) -> {
if (i2 < 0)
throw new Exception("Only positive numbers allowed");
return i1 + i2;
}));
public static LongBinaryOperator longBinaryOperator(CheckedLongBinaryOperator operator)
CheckedLongBinaryOperator in a LongBinaryOperator.
Example:
LongStream.of(1L, 2L, 3L).reduce(Unchecked.longBinaryOperator((l1, l2) -> {
if (l2 < 0L)
throw new Exception("Only positive numbers allowed");
return l1 + l2;
}));
public static DoubleBinaryOperator doubleBinaryOperator(CheckedDoubleBinaryOperator operator)
CheckedDoubleBinaryOperator in a DoubleBinaryOperator.
Example:
DoubleStream.of(1.0, 2.0, 3.0).reduce(Unchecked.doubleBinaryOperator((d1, d2) -> {
if (d2 < 0.0)
throw new Exception("Only positive numbers allowed");
return d1 + d2;
}));
public static <T> Consumer<T> consumer(CheckedConsumer<T> consumer)
CheckedConsumer in a Consumer.
Example:
Arrays.asList("a", "b").stream().forEach(Unchecked.consumer(s -> {
if (s.length() > 10)
throw new Exception("Only short strings allowed");
}));
public static IntConsumer intConsumer(CheckedIntConsumer consumer)
CheckedIntConsumer in a IntConsumer.
Example:
Arrays.stream(new int[] { 1, 2 }).forEach(Unchecked.intConsumer(i -> {
if (i < 0)
throw new Exception("Only positive numbers allowed");
}));
public static LongConsumer longConsumer(CheckedLongConsumer consumer)
CheckedLongConsumer in a LongConsumer.
Example:
Arrays.stream(new long[] { 1L, 2L }).forEach(Unchecked.longConsumer(l -> {
if (l < 0)
throw new Exception("Only positive numbers allowed");
}));
public static DoubleConsumer doubleConsumer(CheckedDoubleConsumer consumer)
CheckedDoubleConsumer in a DoubleConsumer.
Example:
Arrays.stream(new double[] { 1.0, 2.0 }).forEach(Unchecked.doubleConsumer(d -> {
if (d < 0.0)
throw new Exception("Only positive numbers allowed");
}));
public static <T,R> Function<T,R> function(CheckedFunction<T,R> function)
CheckedFunction in a Function.
Example:
map.computeIfAbsent("key", Unchecked.function(k -> {
if (k.length() > 10)
throw new Exception("Only short strings allowed");
return 42;
}));
public static <T> ToIntFunction<T> toIntFunction(CheckedToIntFunction<T> function)
CheckedToIntFunction in a ToIntFunction.
Example:
map.computeIfAbsent("key", Unchecked.toIntFunction(k -> {
if (k.length() > 10)
throw new Exception("Only short strings allowed");
return 42;
}));
public static <T> ToLongFunction<T> toLongFunction(CheckedToLongFunction<T> function)
CheckedToLongFunction in a ToLongFunction.
Example:
map.computeIfAbsent("key", Unchecked.toLongFunction(k -> {
if (k.length() > 10)
throw new Exception("Only short strings allowed");
return 42L;
}));
public static <T> ToDoubleFunction<T> toDoubleFunction(CheckedToDoubleFunction<T> function)
CheckedToDoubleFunction in a ToDoubleFunction.
Example:
map.computeIfAbsent("key", Unchecked.toDoubleFunction(k -> {
if (k.length() > 10)
throw new Exception("Only short strings allowed");
return 42.0;
}));
public static <R> IntFunction<R> intFunction(CheckedIntFunction<R> function)
CheckedIntFunction in a IntFunction.
Example:
IntStream.of(1, 2, 3).mapToObj(Unchecked.intFunction(i -> {
if (i < 0)
throw new Exception("Only positive numbers allowed");
return "" + i;
});
public static IntToLongFunction intToLongFunction(CheckedIntToLongFunction function)
CheckedIntToLongFunction in a IntToLongFunction.
Example:
IntStream.of(1, 2, 3).mapToLong(Unchecked.intToLongFunction(i -> {
if (i < 0)
throw new Exception("Only positive numbers allowed");
return (long) i;
});
public static IntToDoubleFunction intToDoubleFunction(CheckedIntToDoubleFunction function)
CheckedIntToDoubleFunction in a IntToDoubleFunction.
Example:
IntStream.of(1, 2, 3).mapToDouble(Unchecked.intToDoubleFunction(i -> {
if (i < 0)
throw new Exception("Only positive numbers allowed");
return (double) i;
});
public static <R> LongFunction<R> longFunction(CheckedLongFunction<R> function)
CheckedLongFunction in a LongFunction.
Example:
LongStream.of(1L, 2L, 3L).mapToObj(Unchecked.longFunction(l -> {
if (l < 0L)
throw new Exception("Only positive numbers allowed");
return "" + l;
});
public static LongToIntFunction longToIntFunction(CheckedLongToIntFunction function)
CheckedLongToIntFunction in a LongToIntFunction.
Example:
LongStream.of(1L, 2L, 3L).mapToInt(Unchecked.longToIntFunction(l -> {
if (l < 0L)
throw new Exception("Only positive numbers allowed");
return (int) l;
});
public static LongToDoubleFunction longToDoubleFunction(CheckedLongToDoubleFunction function)
CheckedLongToDoubleFunction in a LongToDoubleFunction.
Example:
LongStream.of(1L, 2L, 3L).mapToInt(Unchecked.longToDoubleFunction(l -> {
if (l < 0L)
throw new Exception("Only positive numbers allowed");
return (double) l;
});
public static <R> DoubleFunction<R> doubleFunction(CheckedDoubleFunction<R> function)
CheckedDoubleFunction in a DoubleFunction.
Example:
DoubleStream.of(1.0, 2.0, 3.0).mapToObj(Unchecked.doubleFunction(d -> {
if (d < 0.0)
throw new Exception("Only positive numbers allowed");
return "" + d;
});
public static DoubleToIntFunction doubleToIntFunction(CheckedDoubleToIntFunction function)
CheckedDoubleToIntFunction in a DoubleToIntFunction.
Example:
DoubleStream.of(1.0, 2.0, 3.0).mapToInt(Unchecked.doubleToIntFunction(d -> {
if (d < 0.0)
throw new Exception("Only positive numbers allowed");
return (int) d;
});
public static DoubleToLongFunction doubleToLongFunction(CheckedDoubleToLongFunction function)
CheckedDoubleToLongFunction in a DoubleToLongFunction.
Example:
DoubleStream.of(1.0, 2.0, 3.0).mapToLong(Unchecked.doubleToLongFunction(d -> {
if (d < 0.0)
throw new Exception("Only positive numbers allowed");
return (long) d;
});
public static <T> Predicate<T> predicate(CheckedPredicate<T> predicate)
CheckedPredicate in a Predicate.
Example:
Stream.of("a", "b", "c").filter(Unchecked.predicate(s -> {
if (s.length() > 10)
throw new Exception("Only short strings allowed");
return true;
}));
public static IntPredicate intPredicate(CheckedIntPredicate predicate)
CheckedPredicate in a IntPredicate.
Example:
IntStream.of(1, 2, 3).filter(Unchecked.intPredicate(i -> {
if (i < 0)
throw new Exception("Only positive numbers allowed");
return true;
}));
public static LongPredicate longPredicate(CheckedLongPredicate predicate)
CheckedLongPredicate in a LongPredicate.
Example:
LongStream.of(1L, 2L, 3L).filter(Unchecked.longPredicate(l -> {
if (l < 0L)
throw new Exception("Only positive numbers allowed");
return true;
}));
public static DoublePredicate doublePredicate(CheckedDoublePredicate predicate)
CheckedDoublePredicate in a DoublePredicate.
Example:
DoubleStream.of(1.0, 2.0, 3.0).filter(Unchecked.doublePredicate(d -> {
if (d < 0.0)
throw new Exception("Only positive numbers allowed");
return true;
}));
public static <T> Supplier<T> supplier(CheckedSupplier<T> supplier)
CheckedSupplier in a Supplier.
Example:
ResultSet rs = statement.executeQuery();
Stream.generate(Unchecked.supplier(() -> rs.getObject(1)));
public static IntSupplier intSupplier(CheckedIntSupplier supplier)
CheckedIntSupplier in a IntSupplier.
Example:
ResultSet rs = statement.executeQuery();
Stream.generate(Unchecked.intSupplier(() -> rs.getInt(1)));
public static LongSupplier longSupplier(CheckedLongSupplier supplier)
CheckedLongSupplier in a LongSupplier.
Example:
ResultSet rs = statement.executeQuery();
Stream.generate(Unchecked.longSupplier(() -> rs.getLong(1)));
public static DoubleSupplier doubleSupplier(CheckedDoubleSupplier supplier)
CheckedDoubleSupplier in a DoubleSupplier.
Example:
ResultSet rs = statement.executeQuery();
Stream.generate(Unchecked.doubleSupplier(() -> rs.getDouble(1)));
public static BooleanSupplier booleanSupplier(CheckedBooleanSupplier supplier)
CheckedBooleanSupplier in a BooleanSupplier.
Example:
ResultSet rs = statement.executeQuery();
Stream.generate(Unchecked.booleanSupplier(() -> rs.getBoolean(1)));
public static <T> UnaryOperator<T> unaryOperator(CheckedUnaryOperator<T> operator)
CheckedUnaryOperator in a UnaryOperator.
Example:
Stream.of("a", "b", "c").map(Unchecked.unaryOperator(s -> {
if (s.length() > 10)
throw new Exception("Only short strings allowed");
return s;
}));
public static IntUnaryOperator intUnaryOperator(CheckedIntUnaryOperator operator)
CheckedIntUnaryOperator in a IntUnaryOperator.
Example:
IntStream.of(1, 2, 3).map(Unchecked.intUnaryOperator(i -> {
if (i < 0)
throw new Exception("Only positive numbers allowed");
return i;
}));
public static LongUnaryOperator longUnaryOperator(CheckedLongUnaryOperator operator)
CheckedLongUnaryOperator in a LongUnaryOperator.
Example:
LongStream.of(1L, 2L, 3L).map(Unchecked.longUnaryOperator(l -> {
if (l < 0L)
throw new Exception("Only positive numbers allowed");
return l;
}));
public static DoubleUnaryOperator doubleUnaryOperator(CheckedDoubleUnaryOperator operator)
CheckedDoubleUnaryOperator in a DoubleUnaryOperator.
Example:
LongStream.of(1.0, 2.0, 3.0).map(Unchecked.doubleUnaryOperator(d -> {
if (d < 0.0)
throw new Exception("Only positive numbers allowed");
return d;
}));
Copyright © 2018. All rights reserved.