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

me.shaftesbury.utils.functional.Option Maven / Gradle / Ivy

There is a newer version: 1.17
Show newest version
package me.shaftesbury.utils.functional;

/**
 * Created with IntelliJ IDEA.
 * User: Bob
 * Date: 22/10/13
 * Time: 13:19
 * To change this template use File | Settings | File Templates.
 */
public final class Option
{
    private final T _t;
    public Option()
    {
        _t=null;
    }
    public Option(final T t)
    {
        _t = t;
    }
    public final T Some() throws OptionNoValueAccessException
    {
        if(_t!=null) return _t;
        else throw new OptionNoValueAccessException();
    }
    public static final Option None()
    {
        return new Option();
    }
    public final boolean isSome()
    {
        return _t!=null;
    }
    public final boolean isNone()
    {
        return _t==null;
    }
    public final boolean equals(final Option other)
    {
        try
        {
            return isSome() == other.isSome() && Some()==other.Some();
        }
        catch(OptionNoValueAccessException o)
        {
            return true; // every None is considered to be the same
        }
    }
    public final int hashCode()
    {
        return isNone() ? 0 : 31 * _t.hashCode();
    }

    public final static Option toOption(T t)
    {
        return new Option(t);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy