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

dk.rode.thesis.bridge
Class SequenceValueSet<E extends Serializable,C extends Set<E>>

java.lang.Object
  extended by dk.rode.thesis.bridge.SequenceValueCollection<E,C>
      extended by dk.rode.thesis.bridge.SequenceValueSet<E,C>
Type Parameters:
E - The type of serializable values stored in this value set.
C - The type of set storing the values in this value set.
All Implemented Interfaces:
SequenceValueGenerator<E>, Copyable<SequenceValueGenerator<E>>, Stringable<SequenceValueGenerator<E>>
Direct Known Subclasses:
SequenceValueHashSet, SequenceValueLinkedHashSet, SequenceValueTreeSet

@Participant(value="Implementor")
public abstract class SequenceValueSet<E extends Serializable,C extends Set<E>>
extends SequenceValueCollection<E,C>

A sequence value set stores sequence values in a specific java.util.Set implementation as specified by the type parameter C.

Implementation notes:
The get(int) and numberOf(Serializable, int) methods are not fast. They have a worst-case running time proportional to the number of elements, n, in the value set, i.e. O(n). However, numberOf can quickly deduce if a given value is contained in the value set or not based on a hash lookup. For these simple classes, this implementation is deemed adequate.

Author:
Gunni Rode / rode.dk

Field Summary
private  Iterator<E> iterator
          The current iterator used to deliver the next value in first(), get(), or get(int).
 
Fields inherited from class dk.rode.thesis.bridge.SequenceValueCollection
number, type, values
 
Constructor Summary
protected SequenceValueSet(Class<E> type, C values, int number)
          Constructor, which creates this sequence value set to use the java.util.Set instance supplied as values.
 
Method Summary
 boolean duplicates()
          Returns false.
 E first()
          Resets this generator to re-generate its first value, where after values can (again) be generated by SequenceValueGenerator.get().
 E get()
          Returns a value generated by this generator.
 E get(int number)
          Returns the number'th value generated by this generator, which may or may not have been generated yet.
private  E next(Iterator<E> iterator, int count)
          Helper method that will call next() count times on iterator.
 int numberOf(E value, int fromNumber)
          Returns the number of the generated value supplied as value using an offset of fromNumber, or Const.EOF if no such value will ever been generated by this generator.
 
Methods inherited from class dk.rode.thesis.bridge.SequenceValueCollection
checkNumber, copy, equals, getStringablePolicy, getValueType, hashCode, number, size, toCollection, toString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.bridge.SequenceValueGenerator
ordered, sorted
 

Field Detail

iterator

private Iterator<E extends Serializable> iterator
The current iterator used to deliver the next value in first(), get(), or get(int).

Never null.

Constructor Detail

SequenceValueSet

protected SequenceValueSet(Class<E> type,
                           C values,
                           int number)
Constructor, which creates this sequence value set to use the java.util.Set instance supplied as values. The actual type of values is determined by the type parameter C.

The next value to return by get() is the value that is the number'th value in this value set, and this constructor ensures that this value set is initialised accordingly!

Modifying values externally will affect this value set and should not be performed. The result of doing so is undefined - but never good.

Parameters:
type - The type of values stored in this set (and values); cannot be null.
values - The actual Set instance storing the sequence values; cannot be null, empty, or contain null values.
number - The number pointing the the number'th value to be returned by the next call to get().
Throws:
NullPointerException - If either argument is null.
IllegalArgumentException - If values is empty, contain a null value, or if number is illegal.
Method Detail

duplicates

public final boolean duplicates()
Returns false.

Returns:
False.

first

public final E first()
Description copied from interface: SequenceValueGenerator
Resets this generator to re-generate its first value, where after values can (again) be generated by SequenceValueGenerator.get().

The number of generated values is reset when this method is invoked.

If this generator does not generate ordered, nor sorted values, it is undefined which value is generated as the first value.

Returns:
A first value generated by this generator; never null.

get

public final E get()
Description copied from interface: SequenceValueGenerator
Returns a value generated by this generator.

The number of the generated value is one higher than the last value returned by either SequenceValueGenerator.first(), SequenceValueGenerator.get(), or SequenceValueGenerator.get(int).

The number of generated values is reset when first() is invoked.

If this generator generates ordered or sorted values, the generation of values is consistent even after SequenceValueGenerator.first() have been invoked. Otherwise the order is undefined.

Returns:
A value generated by this generator; never null.
See Also:
SequenceValueGenerator.get(int)

get

public final E get(int number)
Description copied from interface: SequenceValueGenerator
Returns the number'th value generated by this generator, which may or may not have been generated yet. The next call to SequenceValueGenerator.get() from generate and return the number + 1'th value.

The number of generated values is reset when first() is invoked.

If this generator generates ordered or sorted values, the generation of values is consistent even after SequenceValueGenerator.first() have been invoked. Otherwise the order is undefined, and this method may generate different values for the same number each time invoked.

Parameters:
number - The number in question; cannot be zero or negative. Is one-, not zero-based!
Returns:
The number'th value generated by this generator; never null.
See Also:
SequenceValueGenerator.get(), SequenceValueGenerator.numberOf(Object, int)

next

private final E next(Iterator<E> iterator,
                     int count)
Helper method that will call next() count times on iterator.

Parameters:
iterator - The iterator; never null.
count - The number of times to call next().
Returns:
The value from the last call to next(); never null.

numberOf

public int numberOf(E value,
                    int fromNumber)
Description copied from interface: SequenceValueGenerator
Returns the number of the generated value supplied as value using an offset of fromNumber, or Const.EOF if no such value will ever been generated by this generator.

Unlike SequenceValueGenerator.first(), SequenceValueGenerator.get(), and SequenceValueGenerator.get(int), this method will not alter the number of values generated by this generator.

Parameters:
value - The value to test; cannot be null.
fromNumber - The number to start from; only relevant if the values generated contain duplicates. Is one-, not zero-based! If larger than SequenceValueGenerator.size(), this method will always return EOF.
Returns:
The number of the generated value supplied as value, or EOF if no such value can be generated by this value generator.
See Also:
SequenceValueGenerator.get(int)

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.