de.cau.cs.kieler.core.util
Class Maybe<T>

java.lang.Object
  extended by de.cau.cs.kieler.core.util.Maybe<T>
Type Parameters:
T - type of contained object

public class Maybe<T>
extends Object

Object that may contain another object, inspired by the Haskell type Maybe.

This class can be used to wrap objects for anonymous classes:

 Myclass foo() {
     final Maybe maybe = new Maybe();
     PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
         public void run() {
             maybe.set(new Myclass("bar"));
         }
     });
     return maybe.get();
 }
 

Another use is as a wrapper for synchronization on objects that may be null:

 Maybe maybe = new Maybe();
 
 void thread1() {
     maybe.set(new Myclass("foo"));
     synchronized (maybe) {
         maybe.notify();
     )
 }
 
 void thread2() {
     synchronized (maybe) {
         if (maybe.get() == null) {
             maybe.wait();
         }
     }
     maybe.get().bar();
 }
 

Rating proposed yellow
(2009-12-11) msp

Constructor Summary
Maybe()
          Creates a maybe without an object.
Maybe(T theobject)
          Creates a maybe with the given object.
 
Method Summary
static
<D> Maybe<D>
create()
          Create a maybe with inferred generic type.
 boolean equals(Object obj)
          
 T get()
          Returns the contained object.
 int hashCode()
          
 void set(T theobject)
          Sets the contained object.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Maybe

public Maybe()
Creates a maybe without an object.


Maybe

public Maybe(T theobject)
Creates a maybe with the given object.

Parameters:
theobject - the object to contain
Method Detail

create

public static <D> Maybe<D> create()
Create a maybe with inferred generic type.

Type Parameters:
D - the generic type
Returns:
a new instance of given type

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

toString

public String toString()

Overrides:
toString in class Object

set

public void set(T theobject)
Sets the contained object.

Parameters:
theobject - the object to set

get

public T get()
Returns the contained object.

Returns:
the contained object