org.jwalk.gen
Class EnumGenerator

java.lang.Object
  extended by org.jwalk.gen.EnumGenerator
All Implemented Interfaces:
CustomGenerator, Generator

public class EnumGenerator
extends java.lang.Object
implements CustomGenerator

EnumGenerator is a custom generator to synthesise enumerated values. For each descendant type of Enum, it is capable of identifying all the enumerated values of that type and producing each of them in order of enumeration.

EnumGenerator is automatically included as a delegate generator.

Version:
1.0
Author:
Anthony Simons

Field Summary
protected  java.util.Map<java.lang.Class<?>,java.lang.Integer> indexMap
          The map relating each seen Enum type to a current index.
 
Constructor Summary
EnumGenerator()
          Creates an EnumGenerator.
 
Method Summary
 boolean canCreate(java.lang.Class<?> type)
          Reports whether this EnumGenerator can create a given type.
 java.lang.Object nextValue(java.lang.Class<?> type)
          Returns the next enumerated value in sequence.
 void setOwner(MasterGenerator generator)
          Sets the owning MasterGenerator for this CustomGenerator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

indexMap

protected java.util.Map<java.lang.Class<?>,java.lang.Integer> indexMap
The map relating each seen Enum type to a current index.

Constructor Detail

EnumGenerator

public EnumGenerator()
Creates an EnumGenerator. Initialises the map of known Enums to an empty map.

Method Detail

canCreate

public boolean canCreate(java.lang.Class<?> type)
Reports whether this EnumGenerator can create a given type.

Specified by:
canCreate in interface Generator
Parameters:
type - the requested type.
Returns:
true if the type is: any enumerated type.

nextValue

public java.lang.Object nextValue(java.lang.Class<?> type)
                           throws GeneratorException
Returns the next enumerated value in sequence. The requested type is an enumerated type. This EnumGenerator maintains a map relating all previously-seen enumerated types to an index number. Seeks the type in the map and uses the corresponding index to select the next enumerated value from the range of the type. If the type was previously unseen, returns the first enumerated value. Updates the map index, so that a different enumerated value is returned on each call. If the requested type is the abstract type Enum, attempts to find a suitable concrete enumerated type and returns an instance of this.

Specified by:
nextValue in interface Generator
Parameters:
type - the requested enumerated type.
Returns:
the next enumerated value in sequence.
Throws:
GeneratorException - if the requested type is not an enumerated type, or it was the abstract Enum and no concrete type could be found.

setOwner

public void setOwner(MasterGenerator generator)
Sets the owning MasterGenerator for this CustomGenerator.

Specified by:
setOwner in interface CustomGenerator
Parameters:
generator - the MasterGenerator of this EnumGenerator.