public String toString(){ if(isPresent()){ return "Optional(" + value.toString() + ")"; } return "Optional()"; } }
/** * If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException. * * @return the non-null value held by this Optional * @throws NoSuchElementException - if there is no value present */ public T get(){ if (!isPresent()) { throw new NoSuchElementException("Optional is empty"); } return value; }
/** * Return the value if present, otherwise return other. * * @param other the value to be returned if there is no value present, may be null * @return */ public T orElse(T other){ if(isPresent()){ return get(); } return other; }