ezvcard.util
Class CaseClasses<T,V>

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

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).
 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.
 
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 volatile 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

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

all

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

Returns:
all static constants


Copyright © 2012-2014 Michael Angstadt. All Rights Reserved.