sharkysoft.com

com.sharkysoft.util
Class EnumeratedType

java.lang.Object
  extended bycom.sharkysoft.util.EnumeratedType
All Implemented Interfaces:
java.lang.Comparable

public abstract class EnumeratedType
extends java.lang.Object
implements java.lang.Comparable

Type-safe enumeration.

Details: An EnumeratedType defines a finite set of values for a property.

In C, enumerate types are typically represented using the enum construct. This offers many of the benefits of type-safety without sacrificing the efficiency of using ints to represent the values. Unfortunately, Java has no such construct, and it has become common and acceptable to directly represent enumerated types using final ints. Unfortunately, this adaptation lacks type safety.

EnumeratedType facilitates the declaration of type-safe enumerated types. Each element in the type's value set is represented using both ints and typed objects. Methods that accept or return enumerated types can do so using type-safe object values under most circumstances, and convert them to ints when necessary, such as in a switch statement. Using enumerated types derived from this base class not only encourages healthy code, but it also makes debugging easier, since each type-safe value can also be converted into a meaningful string value using toString.

Author:
Sharky

Constructor Summary
protected EnumeratedType(int inValue, java.lang.String isString)
          Initializes type-safe object value.
 
Method Summary
 int compareTo(java.lang.Object ipValue)
          Orders enumerated values.
static EnumeratedType toEnumeratedType(int inValue)
          Converts int value to object value.
 int toInt()
          Converts object value to integer value.
 java.lang.String toString()
          Converts object value to string form.
static java.lang.String toString(int inValue)
          Converts integer value to string form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EnumeratedType

protected EnumeratedType(int inValue,
                         java.lang.String isString)
Initializes type-safe object value.

Details: This constructor initializes type-safe object values. inValue is the magic int value associated with the enumerated value (usually declared as public static final int in the subclass). isString is the string representation of the same value, which is usually just the int identifier in string form.

Parameters:
inValue - int representation of value
isString - string representation of value
Method Detail

toInt

public int toInt()
Converts object value to integer value.

Details: toInt returns the unique "magic integer" associated with this type-safe value. This is useful in switch statements and other situations where int represention of the value might be more efficient.

Returns:
integer value

toString

public java.lang.String toString()
Converts object value to string form.

Details: toString converts this type-safe object value to a string. This may be useful for debugging.

Returns:
string form

toEnumeratedType

public static EnumeratedType toEnumeratedType(int inValue)
Converts int value to object value.

Details: toEnumeratedType returns the type-safe object value corresponding to the given int value.

Because this is a generic implementation that returns an EnumeratedType, rather than the specific object type related defined by the subclass, it is a good idea to define a static helper method in the subclass that returns a more specifically typed value.

Parameters:
inValue - integer value
Returns:
object value

toString

public static java.lang.String toString(int inValue)
Converts integer value to string form.

Details: toString converts the given integer value to a string. This may be useful for debugging.

Parameters:
inValue - integer value
Returns:
string form

compareTo

public int compareTo(java.lang.Object ipValue)
Orders enumerated values.

Details: compareTo satisfies the contract of compareTo in interface java.lang.Comparable. Enumerated values are ordered according to their magic integer.

This implementation throws a ClassCastException if one object's class does not descend from the other object's class.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
ipValue - value to commpare
Returns:
order of values

sharkysoft.com

Copyright © 1997-2004 Sharkysoft (sharkysoft.com). All rights reserved.