| Modifier and Type | Method and Description |
|---|---|
static <T> AutoClosed<T> |
autoClose(T object,
Consumer<? super T> closeOperation)
Wrap an arbitrary object to an
AutoCloseable container, and assign an operation to be
performed on the wrapped object when calling AutoCloseable.close(). |
static Stream<Exception> |
close(AutoCloseable... closeables)
Create a stream which will yield the exceptions from closing several
closeables. |
static <T,R> Stream<R> |
extract(T object,
Function<? super T,? extends R>... extractors)
Extract (derive) multiple values from one given object.
|
static <T,R> Stream<R> |
extractIfPresent(T object,
Function<? super T,? extends Optional<R>>... extractors)
Extract (derive) multiple values from one given object.
|
static <T> Stream<Exception> |
forceOnAll(ThrowingConsumer<? super T,? extends Exception> action,
Stream<T> instances)
Create a stream which will yield the exceptions (if any) from invoking an
action on
several instances. |
static <T> Stream<Exception> |
forceOnAll(ThrowingConsumer<? super T,? extends Exception> action,
T... instances)
Create a stream which will yield the exceptions (if any) from invoking an
action on
several instances. |
static String |
friendlyName(Class<?> clazz)
The "friendly name" of a class is defined as its
simple name, with
all enclosing classes prepended and joined with a '.' delimiter. |
static <T> T |
nonNull(String descriptiveRefKey,
Function<? super String,T> refResolver)
Not allow
null-references. |
static <T,X extends Throwable> |
nonNull(String descriptiveRefKey,
Function<? super String,T> refResolver,
Function<? super String,X> throwIfNull)
Not allow
null-references. |
static <T> T |
nonNull(String description,
T t)
Not allow
null-references. |
static <T,X extends Throwable> |
nonNull(String description,
T t,
Function<? super String,X> throwIfNull)
Not allow
null-references. |
static <T,X extends Throwable> |
nonNull(T t,
Supplier<X> throwIfNull)
Not allow
null-references. |
static <T,X extends Exception> |
throwingAutoClose(T object,
ThrowingConsumer<? super T,X> closeOperation)
Wrap an arbitrary object to an
AutoCloseable container, and assign an operation to be
performed on the wrapped object when calling AutoCloseable.close(). |
public static <T> T nonNull(String description, T t)
null-references.description - A small description of the reference. Will be used in the exception message if
the reference is nullt - the referenceNullPointerException - if t is null.public static <T,X extends Throwable> T nonNull(String description, T t, Function<? super String,X> throwIfNull) throws X extends Throwable
null-references.description - A small description of the reference. Will be used in the exception message if
the reference is nullt - the referencethrowIfNull - Construct the exception to be thrown if the reference is null.X - if t is null.X extends Throwablepublic static <T,X extends Throwable> T nonNull(T t, Supplier<X> throwIfNull) throws X extends Throwable
null-references.t - the referencethrowIfNull - the exception to throw if t is nullX - if t is null.X extends Throwablepublic static <T> T nonNull(String descriptiveRefKey, Function<? super String,T> refResolver)
null-references. This is a convenience method for when a descriptive refKey
can be used to resolve the reference, for instance to resolve resources
on classpath with .class::getResourceAsStream.
The refKey will appear in the exception message if the resolved reference is null.descriptiveRefKey - the key used to resolve the referencerefResolver - the function the will resolve the non-null result based on the description.refResolver, never nullpublic static <T,X extends Throwable> T nonNull(String descriptiveRefKey, Function<? super String,T> refResolver, Function<? super String,X> throwIfNull) throws X extends Throwable
null-references.descriptiveRefKey - the key used to resolve the referencerefResolver - the function the will resolve the non-null result based on the description.throwIfNull - the function to construct the exception if the refResolver yields null.refResolver, never nullX - if refResolver yields nullX extends Throwablepublic static String friendlyName(Class<?> clazz)
simple name, with
all enclosing classes prepended and joined with a '.' delimiter. This name is typically
useful for logging, naming based on classes, where the fully qualified name would be too verbose
and the simple name is not specific enough.
Given the following class model:
class Base {
class Nested {}
}
The friendly name for the Nested class is "Base.Nested".clazz - the clazz to get the friendly name of@SafeVarargs public static final <T,R> Stream<R> extract(T object, Function<? super T,? extends R>... extractors)
T - The type of the object to extract from.R - The resulting most common type of the extracted values. Typically, the extractors
should yield the same type.object - the object to extract values from.extractors - each function that will extract a value from the given object. The resulting
value must not be null. Use extract(Object, Function...)
if the extractors may yield non-existing values.@SafeVarargs public static final <T,R> Stream<R> extractIfPresent(T object, Function<? super T,? extends Optional<R>>... extractors)
{@link #extract(Object, Function...) extract(object, extractors...)}{@link Stream#filter(java.util.function.Predicate) .filter(}{@link Optional#isPresent() Optional::isPresent)}{@link Stream#map(Function) .map(}{@link Optional#get() Optional::get)}
T - The type of the object to extract from.R - The resulting most common type of the extracted values. Typically, the extractors
should yield the same type.object - the object to extract values from.extractors - each function that will extract a value from the given object.public static Stream<Exception> close(AutoCloseable... closeables)
closeables.
Consuming the stream will ensure that all closeables are attempted
closed, and any exceptions happening will be available
through the returned stream.
To further collapse the possibly multiple exceptions into one throwable exception,
use either
.collect(toSingleExceptionWithSuppressed())
or .collect(asSuppressedExceptionsOf(..))
in DiggCollectors.
If you have non-AutoCloseable related actions that need to be performed as well, this can be achieved
by using Stream.concat(close(..),
forceOnAll(T::action, T ... instances))
closeables - The AutoCloseable instances to close.DiggCollectors.toSingleExceptionWithSuppressed(),
DiggCollectors.asSuppressedExceptionsOf(Throwable)@SafeVarargs public static <T> Stream<Exception> forceOnAll(ThrowingConsumer<? super T,? extends Exception> action, T... instances)
action on
several instances. Consuming the stream will ensure that all instances will have
the action invoked on them, and any exceptions happening will be available through the returned stream.action - the action to execute for each provided instanceinstances - the instances to act on with the provided action.public static <T> Stream<Exception> forceOnAll(ThrowingConsumer<? super T,? extends Exception> action, Stream<T> instances)
action on
several instances. Consuming the stream will ensure that all instances will have
the action invoked on them, and any exceptions happening will be available through the returned stream.action - the action to execute for each provided instanceinstances - the instances to act on with the provided action.public static <T,X extends Exception> ThrowingAutoClosed<T,X> throwingAutoClose(T object, ThrowingConsumer<? super T,X> closeOperation)
AutoCloseable container, and assign an operation to be
performed on the wrapped object when calling AutoCloseable.close(). This can be
used for legacy classes which does not implement AutoCloseable to be used with the
try-with-resources construct. It should not be used (although it can) for
objects already implementing AutoCloseable.T - The type of the wrapped/managed object.X - The exception which may be throwed when closing by AutoCloseable.close().object - the object to be managed with try-with-resources.closeOperation - the operation to invoke on object to close it.AutoCloseable. Assign this with try-with-resources to
have it properly closed.autoClose(Object, Consumer)public static <T> AutoClosed<T> autoClose(T object, Consumer<? super T> closeOperation)
AutoCloseable container, and assign an operation to be
performed on the wrapped object when calling AutoCloseable.close(). This can be
used for legacy classes which does not implement AutoCloseable to be used with the
try-with-resources construct. It should not be used (although it can) for
objects already implementing AutoCloseable.T - The type of the wrapped/managed object.object - the object to be managed with try-with-resources.closeOperation - the operation to invoke on object to close it. If the operation
can throw a checked exception, use throwingAutoClose(Object, ThrowingConsumer)
instead.AutoCloseable. Assign this with try-with-resources to
have it properly closed.throwingAutoClose(Object, ThrowingConsumer)Copyright © 2019 Digipost. All rights reserved.