
com.io7m.jfunctional.Some Maven / Gradle / Ivy
/*
* Copyright © 2014 http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.io7m.jfunctional;
import com.io7m.jequality.annotations.EqualityStructural;
import com.io7m.jnull.NullCheck;
import com.io7m.jnull.Nullable;
/**
* A value.
*
* @param The type of values.
*
* @see OptionType
*/
@EqualityStructural public final class Some implements OptionType
{
private static final long serialVersionUID = 9158960385113454019L;
private final T x;
private Some(
final T in_x)
{
this.x = NullCheck.notNull(in_x, "Some value");
}
static Some some(
final T x)
{
return new Some(x);
}
@Override public void map_(final ProcedureType p)
{
NullCheck.notNull(p, "Procedure");
p.call(this.x);
}
@Override public void mapPartial_(
final PartialProcedureType p)
throws E
{
NullCheck.notNull(p, "Procedure");
p.call(this.x);
}
@Override public U accept(
final OptionVisitorType v)
{
NullCheck.notNull(v, "Visitor");
return v.some(this);
}
@Override public U acceptPartial(
final OptionPartialVisitorType v)
throws E
{
NullCheck.notNull(v, "Visitor");
return v.some(this);
}
@Override public boolean equals(
final @Nullable Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final Some> other = (Some>) obj;
return this.x.equals(other.x);
}
/**
* @return The value contained within.
*/
public T get()
{
return this.x;
}
@Override public int hashCode()
{
return this.x.hashCode();
}
@Override public OptionType map(
final FunctionType f)
{
NullCheck.notNull(f, "Function");
return Option.of(f.call(this.x));
}
@Override public OptionType mapPartial(
final PartialFunctionType f)
throws E
{
NullCheck.notNull(f, "Function");
return Option.of(f.call(this.x));
}
@Override public String toString()
{
final StringBuilder builder = new StringBuilder();
builder.append("[Some ");
builder.append(this.x);
builder.append("]");
final String s = builder.toString();
assert s != null;
return s;
}
@Override public boolean isNone()
{
return false;
}
@Override public boolean isSome()
{
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy