From e023e674020f1a435f7b8c8b9276704f576ea6e5 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Mon, 29 Mar 2021 14:08:45 +0200 Subject: structure change 1 --- .../org/uic/barcode/asn1/datatypes/Optional.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/main/java/org/uic/barcode/asn1/datatypes/Optional.java (limited to 'src/main/java/org/uic/barcode/asn1/datatypes/Optional.java') diff --git a/src/main/java/org/uic/barcode/asn1/datatypes/Optional.java b/src/main/java/org/uic/barcode/asn1/datatypes/Optional.java new file mode 100644 index 0000000..757ba29 --- /dev/null +++ b/src/main/java/org/uic/barcode/asn1/datatypes/Optional.java @@ -0,0 +1,96 @@ +package org.uic.barcode.asn1.datatypes; + +import java.util.NoSuchElementException; +import java.util.Objects; + +/** Represents optional values. + * + * Should be replaced by java.util.Optional from Java 8, when project moves to Java 8. + * + * @param type of contained elements */ +public class Optional { + + private final T element; + private final boolean isPresent; + + private Optional(T element, boolean isPresent) { + this.element = element; + this.isPresent = isPresent; + } + + /** @return true if the Option contains a value */ + public boolean isPresent() { + return isPresent; + } + + /** @return the element if the option is not empty + * @throws java.util.NoSuchElementException if the option is empty */ + public T get() { + if (isPresent) { + return element; + } else { + throw new NoSuchElementException("None.get"); + } + } + + /** @return the value, if present, otherwise return {@code other} + * @param other the value to be returned if there is no value present */ + public T orElse(T other) { + return isPresent() ? get() : other; + } + + /** + * Indicates whether some other object is "equal to" this Optional. The + * other object is considered equal if: + * + * + * @param obj an object to be tested for equality + * @return {code true} if the other object is "equal to" this object + * otherwise {@code false} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (!(obj instanceof Optional)) { + return false; + } + + Optional other = (Optional) obj; + return Objects.equals(element, other.element); + } + + /** + * Returns the hash code value of the present value, if any, or 0 (zero) if + * no value is present. + * + * @return hash code value of the present value or 0 if no value is present + */ + @Override + public int hashCode() { + return Objects.hashCode(element); + } + + /** Returns an Option containing the value. + * + * @param the type of the value + * @param element contained value + * @return a new Option that contains the value */ + public static Optional of(final A element) { + return new Optional(element, true); + } + + /** Returns an empty option. + * + * @param + * @return an empty Option */ + public static Optional empty() { + return new Optional(null, false); + } +} -- cgit v1.2.3