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

dk.rode.thesis.flyweight
Class Sentence

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

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

A sentence represents an ordered sequence of words making up that sentence.

A sentence is bounded, consistent, but not unique if the same word is contained more than once, including white space!

Author:
Gunni Rode / rode.dk

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 of this sentence.
private  boolean unique
          True if this sentence 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
Sentence(Sentence sentence)
          Copy constructor.
Sentence(Word[] words)
          Constructor, which creates this sentence to be comprised of the words in words.
 
Method Summary
 char charAt(int index)
           
 int compareTo(Sentence sentence)
           
 Sentence 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.
 Iterator<Word> 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 sentence as a string.
 boolean unique()
          Returns true if any given word in this sentence is only contained once, including white space, 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 of this sentence.


unique

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

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

Constructor Detail

Sentence

Sentence(Sentence sentence)
Copy constructor.

Parameters:
sentence - The sentence to copy; cannot be null.
Throws:
NullPointerException - If sequence is null.

Sentence

Sentence(Word[] words)
Constructor, which creates this sentence to be comprised of the words in words.

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

charAt

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

compareTo

public int compareTo(Sentence sentence)
Specified by:
compareTo in interface Comparable<Sentence>

copy

public Sentence 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<Word>
Specified by:
copy in interface Copyable<Sequence<Word>>
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<Word>
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<Word>
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<Sentence>
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.

iterator

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

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<Sentence>
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 sentence as a string.

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

unique

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

Specified by:
unique in interface Sequence<Word>
Overrides:
unique in class ArraySequence<Word>
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<Sentence>
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.