org.pocketgames.basic
Class Maybe<X>

java.lang.Object
  extended by org.pocketgames.basic.Maybe<X>
Type Parameters:
X - any type that you want to store inside here

public class Maybe<X>
extends java.lang.Object

This class represents something similar to the Haskell type Maybe. Note that part of the operations of that data type cannot be defined in Java at the current time.

Examples:

If you have a computation with a type that is optional, sometimes you might not want to use null to code the case in which that computation fails. In that case, your function can return.

Once you have your value, getting it back is quite easy. If the result of isNothing() is true, then your Maybe object holds nothing inside. If the result of isNothing is false, then fromJust should return that object inside the Maybe.

Author:
Ivan Perez-Dominguez (iperez _at_ babel.ls.fi.upm.es)

Field Summary
static Maybe NOTHING
          A default Nothing object.
 
Constructor Summary
Maybe(X val)
          Default constructor for Just(Object).
 
Method Summary
 X fromJust()
          Obtains the value inside, reporting an exception if no value has been provided.
 boolean isJust()
          Checks if this Maybe is not empty.
 boolean isNothing()
          Checks if this Maybe is empty.
 X unsafeFromJust()
          Obtains the value inside, without throwing any exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOTHING

public static final Maybe NOTHING
A default Nothing object.

Constructor Detail

Maybe

public Maybe(X val)
Default constructor for Just(Object).

Parameters:
val - a value to store inside.
Method Detail

isNothing

public boolean isNothing()
Checks if this Maybe is empty.

Returns:
true if it is, false otherwise.

isJust

public boolean isJust()
Checks if this Maybe is not empty.

Returns:
true if it has something, false otherwise.

unsafeFromJust

public X unsafeFromJust()
Obtains the value inside, without throwing any exception.

Returns:
the value inside if any, undefined in other case

fromJust

public X fromJust()
           throws java.lang.NoSuchFieldException
Obtains the value inside, reporting an exception if no value has been provided.

Returns:
the value inside this Maybe
Throws:
java.lang.NoSuchFieldException - if there's nothing inside