Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.singleton
Enum DanishAlphabetSequence

java.lang.Object
  extended by java.lang.Enum<DanishAlphabetSequence>
      extended by dk.rode.thesis.singleton.DanishAlphabetSequence
All Implemented Interfaces:
Sequence<String>, Copyable<Sequence<String>>, StrictCopyable<Sequence<String>>, Stringable<Sequence<String>>, Serializable, Comparable<DanishAlphabetSequence>

@Participant(value="Singleton")
@Singleton(value="valueOf",
           arguments="Instance")
public enum DanishAlphabetSequence
extends Enum<DanishAlphabetSequence>
implements Sequence<String>

A singleton alphabet sequence for letters in the Danish alphabet: a though z as well as æ, ø, and å, all lower-cased.

This class cannot be sub-classed, but is serializable.

Implementation notes:
By using an enumeration with a single constant to represent the singleton instance of this sequence, many singleton traits are enforced by Java: the constructor has to be private and cannot be instantiated from elsewhere, not even via reflection. The sequence type is guaranteed not cloneable, but is serializable! Lazy loading is also possible. There is no need to implement hashCode() and equals(Object), as no other sequence will be identical. Identity (==) comparison can be used. The singleton instance can still be referenced through a static singleton method, but also through the constant. We consider a Singleton implementation using enumerations in this manner for an application of the Singleton-as-Single-Constant idiom.

Passing arguments to a singleton type is difficult, though. The type cannot inherit functionality as the java.lang.Enum class is already inherited. Hence, composition has to be used in combination with interface implementation.

The inherited static method valueOf(String) from java.lang.Enum can also be used to initialise and/or fetch the singleton instance. This is utilised here to be able to fetch this sequence from a StatelessSingletonRegistry instance as it is annotated with the Singleton enumeration.

Author:
Gunni Rode / rode.dk
See Also:
NorwegianAlphabetSequence, SimpsonsFamilySequence, SimpsonsAndBouvierFamilySequence, SmileySequence

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Enum Constant Summary
Instance
          The DanishAlphabetSequence singleton instance to use.
 
Field Summary
private  Sequence<String> sequence
          Delegate sequence storing the actual letters in the Danish alphabet.
static int SIZE
          The size of the Danish alphabet.
 
Method Summary
 boolean bounded()
          Returns true if this sequence is bounded, i.e.
 boolean consistent()
          Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset.
 DanishAlphabetSequence copy()
          Returns this instance.
 String current()
          Returns the current element from this sequence.
 StringablePolicy<? super Sequence<String>> getStringablePolicy(StringablePolicy<? super Sequence<String>> policy)
          Always return a non-null policy: policy is not null: policy is returned.
 String next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 Sequence.State state()
          Returns the internal state of this sequence.
 String toString()
           
 CharSequence toString(StringablePolicy<? super Sequence<String>> policy)
          Returns a char sequence representation of this stringable object using the format determined by policy or the default policy in case policy is null.
 boolean unique()
          Returns true if this sequence deliver a given sequence value at most one time until reset or restarted.
static DanishAlphabetSequence valueOf(String name)
          Returns the enum constant of this type with the specified name.
static DanishAlphabetSequence[] values()
          Returns an array containing the constants of this enum type, in the order they are declared.
 
Methods inherited from class java.lang.Enum
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, valueOf
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Enum Constant Detail

Instance

public static final DanishAlphabetSequence Instance
The DanishAlphabetSequence singleton instance to use.

Is not initialised until first time accessed.

Field Detail

sequence

private final Sequence<String> sequence
Delegate sequence storing the actual letters in the Danish alphabet.

Implementation notes:
Delegation has to be used as enumerations cannot extend other classes as they already extend the java.lang.Enum class.

This cannot be declared static because then it might be loaded after Instance, as Instance is in fact a separate class. As only a single constant is ever created, this works just fine.


SIZE

public static final int SIZE
The size of the Danish alphabet.

Implementation notes:
Accessing this static field will before Instance will not cause the singleton instance to be created!

See Also:
NorwegianAlphabetSequence.SIZE, SimpsonsFamilySequence.SIZE, SimpsonsAndBouvierFamilySequence.SIZE, SmileySequence.SIZE, Constant Field Values
Method Detail

bounded

public boolean bounded()
Description copied from interface: Sequence
Returns true if this sequence is bounded, i.e. deliver values between the initial sequence value and some upper bound.

The same type of sequence may represent both bounded and unbounded sequences and the behaviour is determined and fixed at construction time. Bounded sequences will restart if they deliver consistent values.

Specified by:
bounded in interface Sequence<String>
Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

consistent

public boolean consistent()
Description copied from interface: Sequence
Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset.

Only bounded consistent sequences will restart. Consistent sequences need not deliver unique sequence values.

Instances of the same type of sequences are always consistent or inconsistent; it is determined at design time, not construction time.

Specified by:
consistent in interface Sequence<String>
Returns:
True if consistent, false if not.

copy

public DanishAlphabetSequence copy()
Returns this instance.

Specified by:
copy in interface Sequence<String>
Specified by:
copy in interface Copyable<Sequence<String>>
Returns:
This instance; never null.

current

public String current()
Description copied from interface: Sequence
Returns the current element from this sequence.

This method can be invoked even if Sequence.next() has not been invoked yet, thus delivering the initial value of this sequence.

Specified by:
current in interface Sequence<String>
Returns:
The current element; never null.

getStringablePolicy

public StringablePolicy<? super Sequence<String>> getStringablePolicy(StringablePolicy<? super Sequence<String>> policy)
Description copied from interface: Stringable
Always return a non-null policy:

  1. policy is not null: policy is returned.

  2. policy is null: a default, non-null policy is returned.

Specified by:
getStringablePolicy in interface Stringable<Sequence<String>>
Parameters:
policy - The supplied policy; can be null.
Returns:
The policy to use; never null.
See Also:
Stringable.toString(StringablePolicy)

next

public String next()
Description copied from interface: Sequence
Returns the next element from this sequence.

Specified by:
next in interface Sequence<String>
Returns:
The next element; never null.
See Also:
Sequence.current(), Sequence.state()

reset

public void reset()
Description copied from interface: Sequence
Resets this sequence to start over if it is consistent.

If this sequence is consistent, the sequence will restart.

Specified by:
reset in interface Sequence<String>

state

public Sequence.State state()
Description copied from interface: Sequence
Returns the internal state of this sequence.

Specified by:
state in interface Sequence<String>
Returns:
The internal state; never null.

toString

public String toString()
Overrides:
toString in class Enum<DanishAlphabetSequence>

toString

public CharSequence toString(StringablePolicy<? super Sequence<String>> policy)
Description copied from interface: Stringable
Returns a char sequence representation of this stringable object using the format determined by policy or the default policy in case policy is null.

In Foo, a typical implementation of this method could be:

    public CharSequence toString(StringablePolicy<? super Foo> policy) {
      return this.getStringablePolicy(policy).toString(this);
    }
 
There are two approaches to formatting this stringable object into a char sequence representation:

  1. Let policy decide the entire format, as in the Foo example above; or

  2. Use policy to format part of the overall representation, for example letting this method append certain text regardless of the policy used.

Bullet 1) is not always applicable because a given policy implementation may not have access to all required information in its StringablePolicy.toString(Object) method, for example in case multiple stringable objects should be formatted into an overall representation.

In case an implementation uses the approach from bullet 2), care must be take to respect the policy hints so the overall format remains meaningful.

Specified by:
toString in interface Stringable<Sequence<String>>
Parameters:
policy - The policy to dictate the formatting; can be null, in which case the result of toString method is returned.
Returns:
The char sequence representation; never null.
See Also:
StringablePolicy.Type

unique

public boolean unique()
Description copied from interface: Sequence
Returns true if this sequence deliver a given sequence value at most one time until reset or restarted.

Unbounded sequences that are unique will never deliver the same sequence value more than once.

The same type of sequence may represent both unique and not unique sequences and the behaviour is determined and fixed at construction time.

Specified by:
unique in interface Sequence<String>
Returns:
True if unique, false if not.
See Also:
Sequence.consistent()

valueOf

public static DanishAlphabetSequence valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:
name - the name of the enum constant to be returned.
Returns:
the enum constant with the specified name
Throws:
IllegalArgumentException - if this enum type has no constant with the specified name
NullPointerException - if the argument is null

values

public static DanishAlphabetSequence[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for (DanishAlphabetSequence c : DanishAlphabetSequence.values())
    System.out.println(c);

Returns:
an array containing the constants of this enum type, in the order they are declared

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.