org.gjt.lindfors.util
Class TypeSupport

java.lang.Object
  extended byorg.gjt.lindfors.util.TypeSupport
All Implemented Interfaces:
java.io.Serializable, Visualizable

public abstract class TypeSupport
extends java.lang.Object
implements java.io.Serializable, Visualizable

Support class for creating enumerated types. You can use this abstract class to easily create immutable type classes. Type classes are useful for creating enumerated types which provide type safety for objects and their methods.

To create a unique type for a class with help of the TypeSupport class, you need to define a top-level inner class within the class that requires enumerated types. This inner class can then extend the TypeSupport class and provide its own private constructor that calls the protected constructor in TypeSupport. The constructors of your inner class must be private in order to guarantee the safety of this type. The inner class must be immutable as well.

Here's an example:

 public class TypeSafeClass {
    public static final ClassType FOO = new ClassType("Foo");
    public static final ClassType BAR = new ClassType("Bar");

    public static class ClassType extends TypeSupport {
        private ClassType(String name) {
            super(name);
        }
    }

    public void TypeSafeMethod(ClassType type) {
        // ... do something ...
    }
 }

 

In the example above, the TypeSafeMethod method of TypeSafeClass is guaranteed to get as an argument either FOO or BAR. The ClassType inner class has a private constructor, so the only class that can instantiate it, is TypeSafeClass. And since TypeSafeClass only creates two instances of ClassType ever, FOO and BAR, that are public to other classes, no other arguments can ever be passed to TypeSafeMethod method.

For more detailed documentation, refer to the Util Library Tutorial .

Since:
JDK 1.1
Version:
$Revision: 1.7 $
Author:
Juha Lindfors
See Also:
Static class declarations, Serialized Form

Constructor Summary
protected TypeSupport(java.lang.String name)
          Constructs a new type with given name.
 
Method Summary
 java.lang.String getName()
          Returns the name of this type.
 java.awt.Component toComponent()
          [PENDING] Returns a visual representation of this object.
 java.lang.String toString()
          Returns a string representation of this type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TypeSupport

protected TypeSupport(java.lang.String name)
Constructs a new type with given name. This protected constructor is provided as a convenience to the private inner class defining the actual type for the class that requires it.

Throws:
java.lang.IllegalArgumentException - if a null pointer is passed as a name
Method Detail

toString

public java.lang.String toString()
Returns a string representation of this type. The string contains this type's name.

The output is similar to the following:

 TypeSupport[name="type name"]
 

Returns:
string representing this type

getName

public java.lang.String getName()
Returns the name of this type. Name is set in constructor.

Returns:
name of this type

toComponent

public java.awt.Component toComponent()
[PENDING] Returns a visual representation of this object.

Specified by:
toComponent in interface Visualizable