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

in.ashwanthkumar.utils.lang.option.Option Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package in.ashwanthkumar.utils.lang.option;

/**
 * Safely wrap around null values
 * @param 
 */
abstract public class Option {
    protected T data;

    public Option(T value) {
        this.data = value;
    }

    public T getOrElse(T defaultValue) {
        if (isEmpty()) return defaultValue;
        else return data;
    }

    public boolean isDefined() {
        return !isEmpty();
    }

    public boolean isEmpty() {
        return data == null;
    }

    public abstract T get() throws IllegalStateException;

    public static  Option option(T value) {
        if (value == null) return new None();
        else return new Some(value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Option)) return false;

        Option option = (Option) o;

        if (data != null ? !data.equals(option.data) : option.data != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return data != null ? data.hashCode() : 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy