Package io.blt.util
Class Obj
java.lang.Object
io.blt.util.Obj
Static utility methods for operating on
Object.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T>newInstanceOf(T obj) Returns a new instance of the same type as the input object if possible; otherwise, returns empty.static <T,E extends Throwable>
TorElseGet(T value, ThrowingSupplier<T, E> supplier) Returnsvalueif non-null, else invokes and returns the result ofsupplier.static <T,E extends Throwable>
TorElseOnException(ThrowingSupplier<T, E> supplier, T defaultValue) Invokes and returns the result ofsupplierif no exception is thrown, else returnsdefaultValue.static <T,E extends Throwable>
Tpoke(T instance, ThrowingConsumer<T, E> consumer) Passes theinstanceto theconsumer, then returns theinstance.static <T,E extends Throwable>
Ttap(Supplier<T> supplier, ThrowingConsumer<T, E> consumer) Calls thesupplierto retrieve an instance which is mutated by theconsumerthen returned.static <T,E extends Throwable>
TThrows the specifiedthrowableif the givenvaluesatisfies the providedpredicate.static <T,E extends Throwable>
TthrowUnless(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) Throws the specifiedthrowableif the givenvaluedoes not satisfy the providedpredicate.
-
Method Details
-
poke
Passes theinstanceto theconsumer, then returns theinstance. e.g.var user = Obj.poke(new User(), u -> { u.setName("Greg"); u.setAge(15); });Optionally, the
consumermay throw which will bubble up.- Type Parameters:
T- type ofinstanceE- type ofconsumerthrowable- Parameters:
instance- instance to consume and returnconsumer- operation to perform oninstance- Returns:
instanceafter accepting side effects viaconsumer.- Throws:
E extends Throwable
-
tap
public static <T,E extends Throwable> T tap(Supplier<T> supplier, ThrowingConsumer<T, E> consumer) throws ECalls thesupplierto retrieve an instance which is mutated by theconsumerthen returned. e.g.var user = Obj.tap(User::new, u -> { u.setName("Greg"); u.setAge(15); });Optionally, the
consumermay throw which will bubble up.- Type Parameters:
T- type of instanceE- type ofconsumerthrowable- Parameters:
supplier- supplies an instance to consume and returnconsumer- 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 EReturnsvalueif non-null, else invokes and returns the result ofsupplier.Optionally, the
e.g.suppliermay throw which will bubble up.private URL homepageOrDefault(URL homepage) throws MalformedURLException { return Obj.orElseGet(homepage, () -> new URL("https://google.com")); }- Type Parameters:
T- type of the returned valueE- type ofsupplierthrowable- Parameters:
value- returned if non-nullsupplier- called and returned ifvalueis null- Returns:
valueif non-null, else the result ofsupplier- Throws:
E-Throwablethat may be thrown if thesupplieris invoked
-
orElseOnException
public static <T,E extends Throwable> T orElseOnException(ThrowingSupplier<T, E> supplier, T defaultValue) Invokes and returns the result ofsupplierif no exception is thrown, else returnsdefaultValue. e.g.private InputStream openFileOrResource(String name) { return Obj.orElseOnException( () -> new FileInputStream(name), getClass().getResourceAsStream(name)); }- Type Parameters:
T- type of the returned valueE- type ofsupplierthrowable- Parameters:
supplier- called and returned if no exception is throwndefaultValue- returned if an exception is thrown when callingsupplier- Returns:
- result of
supplierif no exception is thrown, elsedefaultValue
-
throwIf
public static <T,E extends Throwable> T throwIf(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) throws E Throws the specifiedthrowableif the givenvaluesatisfies the providedpredicate. For convenience,valueis 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 valueE- the type of the throwable- Parameters:
value- the value to be checkedpredicate- the predicate to be evaluatedthrowable- the supplier for the throwable to be thrown- Returns:
value- Throws:
E- if the givenvaluesatisfies the providedpredicate- 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 specifiedthrowableif the givenvaluedoes not satisfy the providedpredicate. For convenience,valueis returned. e.g.throwUnless(properties, p -> p.containsKey("host"), () -> new IllegalStateException("Properties must contain a host"));- Type Parameters:
T- the type of the valueE- the type of the throwable- Parameters:
value- the value to be checkedpredicate- the predicate to be evaluatedthrowable- the supplier for the throwable to be thrown- Returns:
value- Throws:
E- if the givenvaluedoes not satisfy the providedpredicate- See Also:
-
newInstanceOf
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 ofobj- Parameters:
obj- object to try and create a new instance of- Returns:
- a new instance of the same type as
objor empty
-