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

dk.rode.thesis.strategy
Enum ObjectPolicy

java.lang.Object
  extended by java.lang.Enum<ObjectPolicy>
      extended by dk.rode.thesis.strategy.ObjectPolicy
All Implemented Interfaces:
StringablePolicy<Object>, Serializable, Comparable<ObjectPolicy>

public enum ObjectPolicy
extends Enum<ObjectPolicy>
implements StringablePolicy<Object>

Standard policies for formatting any type of Object into textual representations in form of char sequences (appendable) or as strings (immutable).

Implementation notes:
Though defined as an enumeration, different applicable policies may still be applied to format objects, i.e. any policy having the type Stringable&ltObject>.

NetBeans 5.5.1 has problems with annotated enumeration constants in its editor. The constants are not listed when using code-completion, merely a single @ regardless of the number of constants. Even more so, the standard Java compiler version 1.6.0_01 (in NetBeans 5.5.1) does not allow each constant to implement interface functionality without the interface methods being declared (abstract) in the enumeration class it self, which Eclipse 3.3 does. Hence, the toString(Object) method is declared abstract here and implemented by each constant for compatibility with the standard compiler.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.strategy.StringablePolicy
StringablePolicy.NameIdiom, StringablePolicy.Type
 
Enum Constant Summary
HASHCODE
          Generates a char sequence (StringBuilder) representation of an object having the following format: hash-code: ..[; identity-hash-code: ..]
ID
          Generates a char sequence representation of an object having the following format.
ID_OPEN
          Generates a char sequence (StingBuilder) representation of an object having the following format.
IDENTITY
          Generates a char sequence representation of an object having the following format: Class[hash-code: ..[; identity-hash-code: ..]]
IDENTITY_OPEN
          Generates a char sequence (StringBuilder) representation of an object having the following format: Class[hash-code: ..[; identity-hash-code: ..]
NAME
          Generates a char sequence (StringBuilder) representation of an object having the following format: Class Where Class is the simple name of the class of the object.
NAME_OPEN
          Generates a char sequence (StringBuilder) representation of an object having the following format: Class[ Where Class is the simple name of the class of the object.
 
Method Summary
 String asString(Object object)
          Handy shortcut that returns a textual representation of the object supplied as object as a string.
abstract  CharSequence toString(Object object)
          Returns a textual representation of the object supplied as object in form of a char sequence.
 StringablePolicy.Type type()
          Returns the type of this policy.
static ObjectPolicy valueOf(String name)
          Returns the enum constant of this type with the specified name.
static ObjectPolicy[] 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, toString, valueOf
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Enum Constant Detail

HASHCODE

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy HASHCODE
Generates a char sequence (StringBuilder) representation of an object having the following format:
   hash-code: ..[; identity-hash-code: ..]
 
Where hash-code is the hash-code of the object.

If the identity hash-code differs from the object hash code, identity-hash-code is the identity hash code.

Note, that identity hash codes need still not be unique per unique object!

This policy is considered StringablePolicy.Type.OPEN.


ID

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy ID
Generates a char sequence representation of an object having the following format.
   Class[..]
 
Where Class is the simple class name of the class of the object, and .. is the identity hash-code of the object.

See Also:
ID_OPEN

ID_OPEN

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy ID_OPEN
Generates a char sequence (StingBuilder) representation of an object having the following format.
   Class[..
 
Where Class is the simple class name of the class of the object, and .. is the identity hash-code of the object.

The difference between this policy and ID is that the representation formatted by this policy does not end with a closing bracket (]), i.e. this policy is StringablePolicy.Type.OPEN.


IDENTITY

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy IDENTITY
Generates a char sequence representation of an object having the following format:
   Class[hash-code: ..[; identity-hash-code: ..]]
 
Where Class is the simple class name of the class of the object, and hash-code is the hash-code of the object.

If the identity hash-code differs from the object hash code, identity-hash-code is the identity hash code.

See Also:
IDENTITY_OPEN

IDENTITY_OPEN

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy IDENTITY_OPEN
Generates a char sequence (StringBuilder) representation of an object having the following format:
   Class[hash-code: ..[; identity-hash-code: ..]
 
Where Class is the simple class name of the class of the object, and hash-code is the hash-code of the object.

If the identity hash-code differs from the object hash code, identity-hash-code is the identity hash code.

The difference between this policy and IDENTITY is that the representation formatted by this policy does not end with a closing bracket (]), i.e. this policy is StringablePolicy.Type.OPEN.


NAME

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy NAME
Generates a char sequence (StringBuilder) representation of an object having the following format:
   Class
 
Where Class is the simple name of the class of the object.

See Also:
NAME_OPEN

NAME_OPEN

@Participant(value="ConcreteStrategy")
public static final ObjectPolicy NAME_OPEN
Generates a char sequence (StringBuilder) representation of an object having the following format:
   Class[
 
Where Class is the simple name of the class of the object.

The difference between this policy and NAME is that the representation formatted by this policy includes an opening bracket ([), i.e. this policy is considered StringablePolicy.Type.OPEN.

Method Detail

asString

public String asString(Object object)
Handy shortcut that returns a textual representation of the object supplied as object as a string.

This is typically useful for StringablePolicy.Type.CLOSED stringable policies and/or when such a policy is applied in the standard Object.toString() method.

Parameters:
object - The object; cannot be null.
Returns:
The string representation; never null.
Throws:
NullPointerException - If object is null.
See Also:
toString(Object)

toString

public abstract CharSequence toString(Object object)
Returns a textual representation of the object supplied as object in form of a char sequence.

Specified by:
toString in interface StringablePolicy<Object>
Parameters:
object - The object; cannot be null.
Returns:
The char sequence representation; never null.
See Also:
Stringable.toString(StringablePolicy)

type

public StringablePolicy.Type type()
Description copied from interface: StringablePolicy
Returns the type of this policy.

The stringable context can use the type to decide how to use the textual representation of this policy in conjunction with additional text.

Specified by:
type in interface StringablePolicy<Object>
Returns:
StringablePolicy.Type.APPENDABLE is returned unless explicitly documented otherwise for a given constant.

valueOf

public static ObjectPolicy 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 ObjectPolicy[] 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 (ObjectPolicy c : ObjectPolicy.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.