Package no.digipost

Class DiggBase

java.lang.Object
no.digipost.DiggBase

public final class DiggBase extends Object
Party people, turn up tha...

–– DiggBase ––

This class contains basic utilities. Basically.

  • Method Details

    • nonNull

      public static <T> T nonNull(String description, T t)
      Not allow null-references.
      Parameters:
      description - A small description of the reference. Will be used in the exception message if the reference is null
      t - the reference
      Returns:
      the same instance given as argument.
      Throws:
      NullPointerException - if t is null.
    • nonNull

      public static <T, X extends Throwable> T nonNull(String description, T t, Function<? super String,X> throwIfNull) throws X
      Not allow null-references.
      Parameters:
      description - A small description of the reference. Will be used in the exception message if the reference is null
      t - the reference
      throwIfNull - Construct the exception to be thrown if the reference is null.
      Returns:
      the same instance given as argument.
      Throws:
      X - if t is null.
    • nonNull

      public static <T, X extends Throwable> T nonNull(T t, Supplier<X> throwIfNull) throws X
      Not allow null-references.
      Parameters:
      t - the reference
      throwIfNull - the exception to throw if t is null
      Returns:
      the same instance given as argument.
      Throws:
      X - if t is null.
    • nonNull

      public static <T> T nonNull(String descriptiveRefKey, Function<? super String,T> refResolver)
      Not allow 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.
      Parameters:
      descriptiveRefKey - the key used to resolve the reference
      refResolver - the function the will resolve the non-null result based on the description.
      Returns:
      the reference resolved by refResolver, never null
    • nonNull

      public static <T, X extends Throwable> T nonNull(String descriptiveRefKey, Function<? super String,T> refResolver, Function<? super String,X> throwIfNull) throws X
      Not allow null-references.
      Parameters:
      descriptiveRefKey - the key used to resolve the reference
      refResolver - 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.
      Returns:
      the reference resolved by refResolver, never null
      Throws:
      X - if refResolver yields null
    • friendlyName

      public 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. 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".
      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 be null. Use extract(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 several 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))

      Parameters:
      closeables - The AutoCloseable instances to close.
      Returns:
      the Stream with exceptions, if any, from closing the closeables
      See Also:
    • 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 an 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.
      Parameters:
      action - the action to execute for each provided instance
      instances - the instances to act on with the provided action.
      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 an 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.
      Parameters:
      action - the action to execute for each provided instance
      instances - the instances to act on with the provided action.
      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 an 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.
      Type Parameters:
      T - The type of the wrapped/managed object.
      X - The exception which may be throwed when closing by AutoCloseable.close().
      Parameters:
      object - the object to be managed with try-with-resources.
      closeOperation - the operation to invoke on object to close it.
      Returns:
      The wrapper which is AutoCloseable. Assign this with try-with-resources to have it properly closed.
      See Also:
    • autoClose

      public 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(). 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.
      Type Parameters:
      T - The type of the wrapped/managed object.
      Parameters:
      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.
      Returns:
      The wrapper which is AutoCloseable. Assign this with try-with-resources to have it properly closed.
      See Also: