Package no.digipost
Class DiggBase
- java.lang.Object
-
- no.digipost.DiggBase
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> AutoClosed<T>autoClose(T object, Consumer<? super T> closeOperation)Wrap an arbitrary object to anAutoCloseablecontainer, and assign an operation to be performed on the wrapped object when callingAutoCloseable.close().static Stream<Exception>close(AutoCloseable... closeables)Create a stream which will yield the exceptions from closing severalcloseables.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 anactionon severalinstances.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 anactionon severalinstances.static StringfriendlyName(Class<?> clazz)The "friendly name" of a class is defined as itssimple name, with all enclosing classes prepended and joined with a'.'delimiter.static <T> TnonNull(String descriptiveRefKey, Function<? super String,T> refResolver)Not allownull-references.static <T,X extends Throwable>
TnonNull(String descriptiveRefKey, Function<? super String,T> refResolver, Function<? super String,X> throwIfNull)Not allownull-references.static <T> TnonNull(String description, T t)Not allownull-references.static <T,X extends Throwable>
TnonNull(String description, T t, Function<? super String,X> throwIfNull)Not allownull-references.static <T,X extends Throwable>
TnonNull(T t, Supplier<X> throwIfNull)Not allownull-references.static <T,X extends Exception>
ThrowingAutoClosed<T,X>throwingAutoClose(T object, ThrowingConsumer<? super T,X> closeOperation)Wrap an arbitrary object to anAutoCloseablecontainer, and assign an operation to be performed on the wrapped object when callingAutoCloseable.close().
-
-
-
Method Detail
-
nonNull
public static <T> T nonNull(String description, T t)
Not allownull-references.- Parameters:
description- A small description of the reference. Will be used in the exception message if the reference isnullt- the reference- Returns:
- the same instance given as argument.
- Throws:
NullPointerException- iftisnull.
-
nonNull
public static <T,X extends Throwable> T nonNull(String description, T t, Function<? super String,X> throwIfNull) throws X extends Throwable
Not allownull-references.- Parameters:
description- A small description of the reference. Will be used in the exception message if the reference isnullt- the referencethrowIfNull- Construct the exception to be thrown if the reference isnull.- Returns:
- the same instance given as argument.
- Throws:
X- iftisnull.X extends Throwable
-
nonNull
public static <T,X extends Throwable> T nonNull(T t, Supplier<X> throwIfNull) throws X extends Throwable
Not allownull-references.- Parameters:
t- the referencethrowIfNull- the exception to throw iftisnull- Returns:
- the same instance given as argument.
- Throws:
X- iftisnull.X extends Throwable
-
nonNull
public static <T> T nonNull(String descriptiveRefKey, Function<? super String,T> refResolver)
Not allownull-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 isnull.- Parameters:
descriptiveRefKey- the key used to resolve the referencerefResolver- the function the will resolve the non-null result based on the description.- Returns:
- the reference resolved by
refResolver, nevernull
-
nonNull
public static <T,X extends Throwable> T nonNull(String descriptiveRefKey, Function<? super String,T> refResolver, Function<? super String,X> throwIfNull) throws X extends Throwable
Not allownull-references.- Parameters:
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 therefResolveryieldsnull.- Returns:
- the reference resolved by
refResolver, nevernull - Throws:
X- ifrefResolveryieldsnullX extends Throwable
-
friendlyName
public static String friendlyName(Class<?> clazz)
The "friendly name" of a class is defined as itssimple 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 theNestedclass is "Base.Nested".- Parameters:
clazz- the clazz to get the friendly name of- Returns:
- the friendly name
-
extract
@SafeVarargs public static final <T,R> Stream<R> extract(T object, Function<? super T,? extends R>... extractors)
Extract (derive) multiple values from one given object.- Type Parameters:
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.- Parameters:
object- the object to extract values from.extractors- each function that will extract a value from the given object. The resulting value must not benull. Useextract(Object, Function...)if the extractors may yield non-existing values.- Returns:
- a stream of the extracted values. The resulting stream will have the same size as the amount of given extractors.
-
extractIfPresent
@SafeVarargs public static final <T,R> Stream<R> extractIfPresent(T object, Function<? super T,? extends Optional<R>>... extractors)
Extract (derive) multiple values from one given object. This will only include the "present" values yielded from the given extractors, and is a shorthand for:{@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)}- Type Parameters:
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.- Parameters:
object- the object to extract values from.extractors- each function that will extract a value from the given object.- Returns:
- a stream of values to be extracted. The resulting stream will have either the same size as or less than the amount of given extractors.
-
close
public static Stream<Exception> close(AutoCloseable... closeables)
Create a stream which will yield the exceptions from closing severalcloseables. Consuming the stream will ensure that all closeables are attemptedclosed, 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(..))inDiggCollectors.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))- Parameters:
closeables- TheAutoCloseableinstances to close.- Returns:
- the Stream with exceptions, if any, from closing the closeables
- See Also:
DiggCollectors.toSingleExceptionWithSuppressed(),DiggCollectors.asSuppressedExceptionsOf(Throwable)
-
forceOnAll
@SafeVarargs public 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 anactionon severalinstances. 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.- Parameters:
action- the action to execute for each provided instanceinstances- the instances to act on with the providedaction.- Returns:
- the Stream with exceptions, if any
-
forceOnAll
public 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 anactionon severalinstances. 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.- Parameters:
action- the action to execute for each provided instanceinstances- the instances to act on with the providedaction.- Returns:
- the Stream with exceptions, if any
-
throwingAutoClose
public static <T,X extends Exception> ThrowingAutoClosed<T,X> throwingAutoClose(T object, ThrowingConsumer<? super T,X> closeOperation)
Wrap an arbitrary object to anAutoCloseablecontainer, and assign an operation to be performed on the wrapped object when callingAutoCloseable.close(). This can be used for legacy classes which does not implementAutoCloseableto be used with thetry-with-resourcesconstruct. It should not be used (although it can) for objects already implementingAutoCloseable.- Type Parameters:
T- The type of the wrapped/managed object.X- The exception which may be throwed when closing byAutoCloseable.close().- Parameters:
object- the object to be managed withtry-with-resources.closeOperation- the operation to invoke onobjectto close it.- Returns:
- The wrapper which is
AutoCloseable. Assign this withtry-with-resourcesto have it properly closed. - See Also:
autoClose(Object, Consumer)
-
autoClose
public static <T> AutoClosed<T> autoClose(T object, Consumer<? super T> closeOperation)
Wrap an arbitrary object to anAutoCloseablecontainer, and assign an operation to be performed on the wrapped object when callingAutoCloseable.close(). This can be used for legacy classes which does not implementAutoCloseableto be used with thetry-with-resourcesconstruct. It should not be used (although it can) for objects already implementingAutoCloseable.- Type Parameters:
T- The type of the wrapped/managed object.- Parameters:
object- the object to be managed withtry-with-resources.closeOperation- the operation to invoke onobjectto close it. If the operation can throw a checked exception, usethrowingAutoClose(Object, ThrowingConsumer)instead.- Returns:
- The wrapper which is
AutoCloseable. Assign this withtry-with-resourcesto have it properly closed. - See Also:
throwingAutoClose(Object, ThrowingConsumer)
-
-