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

dk.rode.thesis.flyweight
Class AbstractCharacter

java.lang.Object
  extended by dk.rode.thesis.flyweight.AbstractCharacter
All Implemented Interfaces:
Character, Textual<Character>, Stringable<Character>, CharSequence, Comparable<Character>
Direct Known Subclasses:
Letter, Symbol, Whitespace

@Participant(value="Flyweight")
public abstract class AbstractCharacter
extends Object
implements Character

An abstract character implements the basic traits of the Character interface.

Implementation notes:
Many of the methods from CharSequencecould be implemented in this class, but it is expected that individual sub-classes do this for better performance. This primary concern is that hashCode and equals(Object) are implemented properly to ensure correct flyweight behaviour (when stored in collections).

The specific Textual methods are all implemented here as they all implicitly work on CharSequence instances. As Textual implements CharSequence, we can simply use a reference to this when actual string functionality is forwarded to the Strings utility class.

Author:
Gunni Rode / rode.dk

Constructor Summary
protected AbstractCharacter()
          No-arg constructor.
 
Method Summary
 boolean equals(Object object)
          Returns true if and only if object is a character having the same type and value as this character, false otherwise.
 StringablePolicy<? super Character> getStringablePolicy(StringablePolicy<? super Character> policy)
          Always return a non-null policy: policy is not null: policy is returned.
 int hashCode()
          Returns the hash code of this character.
 int indexOfIgnoreCase(CharSequence sequence, int index, Locale locale)
          Finds the first index where sequence is present in this textual representation from the index supplied, regardless of case, or -1 if sequence cannot be found.
 String lowerCaseFirst(Locale locale)
          Return the first character of this textual representation lower cased.
 CharSequence toString(StringablePolicy<? super Character> 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.
 String upperCaseFirst(Locale locale)
          Return the first character of this textual representation upper cased.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.CharSequence
charAt, length, subSequence, toString
 
Methods inherited from interface java.lang.Comparable
compareTo
 

Constructor Detail

AbstractCharacter

protected AbstractCharacter()
No-arg constructor.

Method Detail

equals

public final boolean equals(Object object)
Returns true if and only if object is a character having the same type and value as this character, false otherwise.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

getStringablePolicy

public StringablePolicy<? super Character> getStringablePolicy(StringablePolicy<? super Character> 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<Character>
Parameters:
policy - The supplied policy; can be null.
Returns:
The policy to use; never null.
See Also:
Stringable.toString(StringablePolicy)

hashCode

public final int hashCode()
Returns the hash code of this character.

Overrides:
hashCode in class Object
Returns:
The hash code.

indexOfIgnoreCase

public int indexOfIgnoreCase(CharSequence sequence,
                             int index,
                             Locale locale)
Description copied from interface: Textual
Finds the first index where sequence is present in this textual representation from the index supplied, regardless of case, or -1 if sequence cannot be found.

index is allowed to be larger than the length of this textual representation, in which case this method returns -1. The supplied locale is used to perform the case conversion.

Specified by:
indexOfIgnoreCase in interface Textual<Character>
Parameters:
sequence - The char sequence to find; cannot be null.
index - The index to start at; cannot be negative.
locale - The locale to use; can be null, in which case the default locale is used.
Returns:
The first index of sequence from index, regardless of case, or -1 if not found.

lowerCaseFirst

public String lowerCaseFirst(Locale locale)
Description copied from interface: Textual
Return the first character of this textual representation lower cased.

Note, that in some locales, shifting case of a given character may cause it to expand into more than one character! Hence, the return type is a string.

Specified by:
lowerCaseFirst in interface Textual<Character>
Parameters:
locale - The locale to use; can be null, in which case the default locale is used.
Returns:
The lower cased character; never null.

toString

public final CharSequence toString(StringablePolicy<? super Character> 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<Character>
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

upperCaseFirst

public String upperCaseFirst(Locale locale)
Description copied from interface: Textual
Return the first character of this textual representation upper cased.

Note, that in some locales, shifting case of a given character may cause it to expand into more than one character! Hence, the return type is a string.

Specified by:
upperCaseFirst in interface Textual<Character>
Parameters:
locale - The locale to use; can be null, in which case the default locale is used.
Returns:
The upper cased character; never null.

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.