biweekly.util
Class CaseClasses<T,V>

java.lang.Object
  extended by biweekly.util.CaseClasses<T,V>
Type Parameters:
T - the class
V - the value that the class holds (e.g. String)
Direct Known Subclasses:
ICalParameterCaseClasses

public abstract class CaseClasses<T,V>
extends Object

Manages objects that are like enums in that they are constant, but unlike enums in that new instances can be created during runtime. This class ensures that all instances of a class are unique, so they can be safely compared using "==" (provided their constructors are private). It mimics the "case class" feature in Scala.

Author:
Michael Angstadt

Field Summary
protected  Class<T> clazz
           
protected  Collection<T> preDefined
           
protected  Collection<T> runtimeDefined
           
 
Constructor Summary
CaseClasses(Class<T> clazz)
          Creates a new case class collection.
 
Method Summary
 Collection<T> all()
          Gets all the static constants of the case class.
protected abstract  T create(V value)
          Creates a new instance of the case class.
 T find(V value)
          Searches for a case object by value, only looking at the case class' static constants (does not include runtime-defined objects).
protected  T find(V value, boolean createIfNotFound, boolean searchRuntimeDefined)
          Searches for a case object by value.
 T get(V value)
          Searches for a case object by value, creating a new object if one cannot be found.
protected abstract  boolean matches(T object, V value)
          Determines if a value is associated with a case object.
protected abstract  V valueOf(T object)
          Gets the value of a case object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clazz

protected final Class<T> clazz

preDefined

protected Collection<T> preDefined

runtimeDefined

protected Collection<T> runtimeDefined
Constructor Detail

CaseClasses

public CaseClasses(Class<T> clazz)
Creates a new case class collection.

Parameters:
clazz - the case class
Method Detail

create

protected abstract T create(V value)
Creates a new instance of the case class.

Parameters:
value - the value to give the instance
Returns:
the new instance

valueOf

protected abstract V valueOf(T object)
Gets the value of a case object.

Parameters:
object - the object
Returns:
the object's value

matches

protected abstract boolean matches(T object,
                                   V value)
Determines if a value is associated with a case object.

Parameters:
object - the object
value - the value
Returns:
true if it matches, false if not

find

public T find(V value)
Searches for a case object by value, only looking at the case class' static constants (does not include runtime-defined objects).

Parameters:
value - the value
Returns:
the object or null if one wasn't found

get

public T get(V value)
Searches for a case object by value, creating a new object if one cannot be found.

Parameters:
value - the value
Returns:
the object

find

protected T find(V value,
                 boolean createIfNotFound,
                 boolean searchRuntimeDefined)
Searches for a case object by value.

Parameters:
value - the value
createIfNotFound - true to create a new instance of the object if it can't be found, false to return "null" if not found
searchRuntimeDefined - true to include the runtime-defined objects in the search, false not to
Returns:
the object

all

public Collection<T> all()
Gets all the static constants of the case class.

Returns:
all static constants


Copyright © 2013 Michael Angstadt. All Rights Reserved.