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

dk.rode.thesis.flyweight
Class Word

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.meta.model.ArraySequence<Character>
          extended by dk.rode.thesis.flyweight.Word
All Implemented Interfaces:
Textual<Word>, Sequence<Character>, Copyable<Sequence<Character>>, StrictCopyable<Sequence<Character>>, Stringable<Sequence<Character>>, CharSequence, Comparable<Word>, Iterable<Character>

@Participant(value="ConcreteFlyweight")
public class Word
extends ArraySequence<Character>
implements Sequence<Character>, Iterable<Character>, Textual<Word>

A word represents an ordered sequence of characters making up that word.

A word is bounded, consistent, but not unique if the same character is contained more than once in the same word.

Author:
Gunni Rode / rode.dk
See Also:
Sentence

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  int hashCode
          The hash code for this word.
private  boolean unique
          True if this word is unique, false if not.
 
Fields inherited from class dk.rode.thesis.meta.model.ArraySequence
elements, index
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
Word(Character[] characters)
          Constructor, which creates this word to be comprised of the characters in characters.
Word(Word word)
          Copy constructor.
 
Method Summary
 char charAt(int index)
           
 int compareTo(Word word)
           
 Word copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
 boolean equals(Object object)
          Returns true if object is an array sequence having the same type as this array sequence, using an equivalent array, and is at the same sequence index, false if not.
 int hashCode()
          Returns the hash code of this sequence.
 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.
 boolean isWhiteSpace()
          Returns true if this word represents a single white space character.
 Iterator<Character> iterator()
           
 int length()
           
 String lowerCaseFirst(Locale locale)
          Return the first character of this textual representation lower cased.
 CharSequence subSequence(int start, int end)
           
 String toString()
          Returns this word as a string.
 boolean unique()
          Returns true if any given character in this word is only contained once, false otherwise.
 String upperCaseFirst(Locale locale)
          Return the first character of this textual representation upper cased.
 
Methods inherited from class dk.rode.thesis.meta.model.ArraySequence
bounded, consistent, current, next, reset, size
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
bounded, consistent, current, next, reset, state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

hashCode

private final int hashCode
The hash code for this word.


unique

private final boolean unique
True if this word is unique, false if not.

If the same character is contained more than once, this word is not unique.

Constructor Detail

Word

Word(Character[] characters)
Constructor, which creates this word to be comprised of the characters in characters.

Parameters:
characters - The characters; cannot be null or empty.
Throws:
NullPointerException - If characters is null.
IllegalArgumentException - If characters is empty.

Word

Word(Word word)
Copy constructor.

Parameters:
word - The word to copy; cannot be null.
Throws:
NullPointerException - If word is null.
Method Detail

charAt

public char charAt(int index)
Specified by:
charAt in interface CharSequence

compareTo

public int compareTo(Word word)
Specified by:
compareTo in interface Comparable<Word>

copy

public Word copy()
Description copied from interface: Sequence
Returns a copy of this sequence that will start at the same sequence index as this sequence.

Specified by:
copy in interface Sequence<Character>
Specified by:
copy in interface Copyable<Sequence<Character>>
Returns:
A copy of this sequence; never null.

equals

public final boolean equals(Object object)
Description copied from class: ArraySequence
Returns true if object is an array sequence having the same type as this array sequence, using an equivalent array, and is at the same sequence index, false if not.

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

hashCode

public final int hashCode()
Description copied from class: ArraySequence
Returns the hash code of this sequence.

Overrides:
hashCode in class ArraySequence<Character>
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<Word>
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.

isWhiteSpace

public boolean isWhiteSpace()
Returns true if this word represents a single white space character.

Returns:
True if white space, false if not.

iterator

public Iterator<Character> iterator()
Specified by:
iterator in interface Iterable<Character>

length

public int length()
Specified by:
length in interface CharSequence

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<Word>
Parameters:
locale - The locale to use; can be null, in which case the default locale is used.
Returns:
The lower cased character; never null.

subSequence

public CharSequence subSequence(int start,
                                int end)
Specified by:
subSequence in interface CharSequence

toString

public String toString()
Returns this word as a string.

Specified by:
toString in interface CharSequence
Overrides:
toString in class AbstractSequence<Character>
Returns:
This word as a string; never null.

unique

public boolean unique()
Returns true if any given character in this word is only contained once, false otherwise.

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

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<Word>
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.