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

dk.rode.thesis.flyweight
Class CharacterFactory

java.lang.Object
  extended by dk.rode.thesis.flyweight.CharacterFactory

@Participant(value="FlyweightFactory")
public class CharacterFactory
extends Object

A character factory creates and manages Character, Word, and Sentence objects. The first two types are flyweights, while the latter is an unshared flyweight.

This factory supports an invariant that states that any Letter can only contain a single char. Sub-classes may override it to alter this behaviour.

Character factories are thread-safe.

Implementation notes:
The getWords() and getCharacters() method return unmodifiable collections. This corresponds to protection proxies as described in the Proxy pattern.

Author:
Gunni Rode / rode.dk

Field Summary
private  Map<String,Character> characters
          The set of created characters so far.
private  Map<Word,Word> words
          The set of created words so far.
 
Constructor Summary
CharacterFactory()
          No-arg constructor.
 
Method Summary
<C extends Character>
int
count(Class<C> type)
          Returns the number of instances created by this factory of the specific Character type supplied as type.
 int countWords()
          Returns the number of created words by this factory.
protected  Character create(String character)
          Factory method that creates a Character based on the character string supplied as character.
 Character getCharacter(char character)
          Returns the unique Character instance representing the char value supplied as character.
 Character getCharacter(CharSequence character, int index)
          Returns the unique Character instance representing the char value at index index in character.
 Character getCharacter(String character)
          Returns the unique Character instance representing the value supplied as character.
 Collection<Character> getCharacters()
          Returns a read-only collection of the characters created by this factory.
<C extends Character>
List<C>
getCharacters(Class<C> type)
          Returns the created characters of the type supplied as type, if ant.
 Sentence getSentence(CharSequence sentence, int start, int end)
          Returns a new Sentence containing the words parsed from string.
 Sentence getSentence(String sentence)
          Returns a new Sentence containing the words parsed from sentence.
private  Word getWord(List<Character> word)
          Possibly creates and returns the Word corresponding to the characters supplied as word.
 Word getWord(String word)
          Returns a Word instance parsed from the letters in string.
 Collection<Word> getWords()
          Returns a read-only collection of the words created by this factory.
protected  String normalise(String character)
          Normalises the string representing a Character to a format supported by this factory.
 String toString()
          Return the string representation of this factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

characters

private final Map<String,Character> characters
The set of created characters so far.

Never null, but may be empty.


words

private final Map<Word,Word> words
The set of created words so far.

Never null, but may be empty.

Constructor Detail

CharacterFactory

public CharacterFactory()
No-arg constructor.

Method Detail

count

public final <C extends Character> int count(Class<C> type)
Returns the number of instances created by this factory of the specific Character type supplied as type.

Type Parameters:
C - The type of character.
Parameters:
type - The character type; cannot be null.
Returns:
The count.
Throws:
NullPointerException - If type is null.
See Also:
getCharacters(Class)

countWords

public final int countWords()
Returns the number of created words by this factory.

Returns:
The number of words.

create

protected Character create(String character)
Factory method that creates a Character based on the character string supplied as character.

Sub-classes can override this hook for additional functionality.

Parameters:
character - The normalised character string; never null.
Returns:
The corresponding Character instance; never null.
See Also:
normalise(String)

getCharacter

public final Character getCharacter(char character)
Returns the unique Character instance representing the char value supplied as character.

Parameters:
character - The char.
Returns:
The corresponding Character instance; never null.

getCharacter

public final Character getCharacter(CharSequence character,
                                    int index)
Returns the unique Character instance representing the char value at index index in character.

Parameters:
character - The char sequence supplying the desired char at index; cannot be null.
index - The index to use.
Returns:
The corresponding Character instance; never null.
Throws:
NullPointerException - If character is null.
IndexOutOfBoundsException - If index is illegal.

getCharacter

public final Character getCharacter(String character)
Returns the unique Character instance representing the value supplied as character.

Parameters:
character - The char.
Returns:
The corresponding Character instance; never null.
Throws:
NullPointerException - If character is null.
IllegalArgumentException - If character does not contain a single char.

getCharacters

public Collection<Character> getCharacters()
Returns a read-only collection of the characters created by this factory.

Returns:
A collection of created characters; never null, but may be empty.
See Also:
getWords()

getCharacters

public final <C extends Character> List<C> getCharacters(Class<C> type)
Returns the created characters of the type supplied as type, if ant.

Type Parameters:
C - The type of character.
Parameters:
type - The character type; cannot be null.
Returns:
A list containing the created characters by this factory; never null, but can be empty.
Throws:
NullPointerException - If type is null.
See Also:
count(Class)

getSentence

public final Sentence getSentence(CharSequence sentence,
                                  int start,
                                  int end)
Returns a new Sentence containing the words parsed from string.

Parameters:
sentence - The actual sentence as a string; cannot be null.
start - The start index.
end - The end index.
Returns:
A sentence representing string; can be null if string only contains white space characters.
Throws:
NullPointerException - If sentence is null.
IndexOutOfBoundsException - If start or end are illegal.

getSentence

public Sentence getSentence(String sentence)
Returns a new Sentence containing the words parsed from sentence.

Parameters:
sentence - The actual sentence as a string; cannot be null.
Returns:
A sentence representing sentence; never null.
Throws:
NullPointerException - If sentence is null.

getWord

private final Word getWord(List<Character> word)
Possibly creates and returns the Word corresponding to the characters supplied as word.

Parameters:
word - The characters forming the word, in order; never null.
Returns:
The corresponding Word instance; never null.

getWord

public final Word getWord(String word)
Returns a Word instance parsed from the letters in string.

If more than one word is present in word, the first one is returned.

Parameters:
word - The word as a string; cannot be null.
Returns:
A corresponding Word instance; never null.
Throws:
NullPointerException - If word is null.

getWords

public Collection<Word> getWords()
Returns a read-only collection of the words created by this factory.

Returns:
A collections of created words; never null, but may be empty.
See Also:
getCharacters()

normalise

protected String normalise(String character)
Normalises the string representing a Character to a format supported by this factory.

Sub-classes can override this hook for additional functionality.

Parameters:
character - The character string; cannot be null.
Returns:
The normalised string; never null.
Throws:
NullPointerException - If character is null.
IllegalArgumentException - If character is empty, or if after trimming contains more than one character.
See Also:
create(String)

toString

public String toString()
Return the string representation of this factory.

Overrides:
toString in class Object
Returns:
The string representation; 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.