All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.organicdesign.fp.oneOf.None Maven / Gradle / Ivy

package org.organicdesign.fp.oneOf;

import org.organicdesign.fp.function.Fn0;
import org.organicdesign.fp.function.Fn1;

/** Represents the absence of a value */
public final class None implements Option {
    // For serializable.  Make sure to change whenever internal data format changes.
    private static final long serialVersionUID = 20170810211300L;

    // ========================================== Static ==========================================
    /** None is a singleton and this is its only instance. */
    static final Option NONE = new None();

    /** Generic version of the singleton instance. */
    @SuppressWarnings("unchecked")
    public static  None none() { return (None) NONE; }

    /** Private constructor for singleton. */
    private None() {}

    /** {@inheritDoc} */
    @Override public T get() { throw new IllegalStateException("Called get on None"); }

    /** {@inheritDoc} */
    @Override public T getOrElse(T t) { return t; }

    /** {@inheritDoc} */
    @Override public boolean isSome() { return false; }
//
//        @Override public UnmodSortedIterator iterator() {
//            return UnmodSortedIterator.empty();
//        }

    /** {@inheritDoc} */
    @Override public  U match(Fn1 has, Fn0 hasNot) {
        return hasNot.get();
    }

    /** {@inheritDoc} */
    @Override public  Option then(Fn1> f) { return Option.none(); }

    /** Valid, but deprecated because it's usually an error to call this in client code. */
    @Deprecated // Has no effect.  Darn!
    @Override public int hashCode() { return 0; }

    /** Valid, but deprecated because it's usually an error to call this in client code. */
    @Deprecated // Has no effect.  Darn!
    @Override public boolean equals(Object other) {
        return (this == other) || (other instanceof org.organicdesign.fp.oneOf.None);
    }

    /** Defend our singleton property in the face of deserialization. */
    private Object readResolve() { return NONE; }

    @Override public String toString() { return "None"; }
}