Package io.blt.util

Class Obj

java.lang.Object
io.blt.util.Obj

public final class Obj extends Object
Static utility methods for operating on Object.
  • Method Details

    • poke

      public static <T, E extends Throwable> T poke(T instance, ThrowingConsumer<T,E> consumer) throws E
      Passes the instance to the consumer, then returns the instance. e.g.
      
       var user = Obj.poke(new User(), u -> {
           u.setName("Greg");
           u.setAge(15);
       });
       

      Optionally, the consumer may throw which will bubble up.

      Type Parameters:
      T - type of instance
      E - type of consumer throwable
      Parameters:
      instance - instance to consume and return
      consumer - operation to perform on instance
      Returns:
      instance after accepting side effects via consumer.
      Throws:
      E extends Throwable
    • tap

      public static <T, E extends Throwable> T tap(Supplier<T> supplier, ThrowingConsumer<T,E> consumer) throws E
      Calls the supplier to retrieve an instance which is mutated by the consumer then returned. e.g.
      
       var user = Obj.tap(User::new, u -> {
           u.setName("Greg");
           u.setAge(15);
       });
       

      Optionally, the consumer may throw which will bubble up.

      Type Parameters:
      T - type of instance
      E - type of consumer throwable
      Parameters:
      supplier - supplies an instance to consume and return
      consumer - operation to perform on supplied instance
      Returns:
      Supplied instance after applying side effects via consumer.
      Throws:
      E extends Throwable
    • orElseGet

      public static <T, E extends Throwable> T orElseGet(T value, ThrowingSupplier<T,E> supplier) throws E
      Returns value if non-null, else invokes and returns the result of supplier.

      Optionally, the supplier may throw which will bubble up.

      e.g.
      
       private URL homepageOrDefault(URL homepage) throws MalformedURLException {
           return Obj.orElseGet(homepage, () -> new URL("https://google.com"));
       }
       
      Type Parameters:
      T - type of the returned value
      E - type of supplier throwable
      Parameters:
      value - returned if non-null
      supplier - called and returned if value is null
      Returns:
      value if non-null, else the result of supplier
      Throws:
      E - Throwable that may be thrown if the supplier is invoked
    • orElseOnException

      public static <T, E extends Throwable> T orElseOnException(ThrowingSupplier<T,E> supplier, T defaultValue)
      Invokes and returns the result of supplier if no exception is thrown, else returns defaultValue. e.g.
      
       private InputStream openFileOrResource(String name) {
           return Obj.orElseOnException(
                   () -> new FileInputStream(name),
                   getClass().getResourceAsStream(name));
       }
       
      Type Parameters:
      T - type of the returned value
      E - type of supplier throwable
      Parameters:
      supplier - called and returned if no exception is thrown
      defaultValue - returned if an exception is thrown when calling supplier
      Returns:
      result of supplier if no exception is thrown, else defaultValue
    • throwIf

      public static <T, E extends Throwable> T throwIf(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) throws E
      Throws the specified throwable if the given value satisfies the provided predicate. For convenience, value is returned. e.g.
      
       public Map<String, String> loadProperties() {
           return throwIf(Properties.loadFromJson(FILENAME), Map::isEmpty,
                   () -> new IllegalStateException("Properties must not be empty"));
       }
       
      Type Parameters:
      T - the type of the value
      E - the type of the throwable
      Parameters:
      value - the value to be checked
      predicate - the predicate to be evaluated
      throwable - the supplier for the throwable to be thrown
      Returns:
      value
      Throws:
      E - if the given value satisfies the provided predicate
      See Also:
    • throwUnless

      public static <T, E extends Throwable> T throwUnless(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) throws E
      Throws the specified throwable if the given value does not satisfy the provided predicate. For convenience, value is returned. e.g.
      
       throwUnless(properties, p -> p.containsKey("host"),
               () -> new IllegalStateException("Properties must contain a host"));
       
      Type Parameters:
      T - the type of the value
      E - the type of the throwable
      Parameters:
      value - the value to be checked
      predicate - the predicate to be evaluated
      throwable - the supplier for the throwable to be thrown
      Returns:
      value
      Throws:
      E - if the given value does not satisfy the provided predicate
      See Also:
    • newInstanceOf

      public static <T> Optional<T> newInstanceOf(T obj)
      Returns a new instance of the same type as the input object if possible; otherwise, returns empty. Supports only instances of concrete types that have a public zero argument constructor. e.g.
      
       public <K, V> Map<K, V> mapOfSameTypeOrHashMap(Map<K, V> map) {
           return Obj.newInstanceOf(map).orElse(new HashMap<>());
       }
       
      Type Parameters:
      T - type of obj
      Parameters:
      obj - object to try and create a new instance of
      Returns:
      a new instance of the same type as obj or empty