com.yelstream.topp.standard.util.Optionals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of topp-standard-core Show documentation
Show all versions of topp-standard-core Show documentation
Topp Standard Core addressing basics of Java SE.
The newest version!
package com.yelstream.topp.standard.util;
import lombok.experimental.UtilityClass;
import java.util.Optional;
/**
* Utility addressing instances of {@link Optional}.
*
* @author Morten Sabroe Mortensen
* @version 1.0
* @since 2024-06-23
*/
@UtilityClass
public class Optionals {
/**
* Indicates, if an optional is empty.
*
* Note that while this kind of testing is NOT the intention with a supposed-to-be fluent optional,
* not all APIs are created with this in mind, are used appropriately,
* leading to most defensive coding being necessary!
*
*
* This represents extreme carefulness.
*
* @param optional Optional.
* This may be {@code null}.
* @return Indicats, if empty.
* @param Type of optional value.
*/
@SuppressWarnings("all")
public static boolean isEmpty(Optional optional) {
return optional==null || optional.isEmpty();
}
/**
* Gets the value contained in an optional
*
* Note that while this kind of testing is NOT the intention with a supposed-to-be fluent optional,
* not all APIs are created with this in mind, are used appropriately,
* leading to most defensive coding being necessary!
*
*
* This represents extreme carefulness.
*
* @param optional Optional.
* This may be {@code null}.
* @return Value contained in optional.
* This may be {@code null}.
* @param Type of optional value.
*/
@SuppressWarnings("all")
public static T get(Optional optional) {
return isEmpty(optional)?null:optional.get();
}
}