EnumeratedType
Keywords: com.sharkysoft.util.EnumeratedType class

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| sharkysoft.com
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.sharkysoft.util
Class EnumeratedType

    java.lang.Object
      com.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 `int`s to represent the values. Unfortunately, Java has no such construct, and it has become common and acceptable to directly represent enumerated types using `final int`s. 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 `int`s 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 `int`s 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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| sharkysoft.com
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

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