|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.meta.model.AbstractSequence<E> dk.rode.thesis.templatemethod.SequenceTemplate<File,byte[]> dk.rode.thesis.templatemethod.FileSequence
@Participant(value="ConcreteClass") public class FileSequence
A file sequence delivers byte arrays as sequence values, where each array corresponds to the contents of the current file or directory in a given directory.
Implementation notes:
Returning a byte array instead of an input stream is done
to illustrate the use of nextValueClose(File, byte[])
.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence |
---|
Sequence.State |
Field Summary | |
---|---|
private File |
directory
The directory where the files are read from. |
private File[] |
files
The matching files. |
private FileFilter |
filter
A filter to filter the files in directory . |
private FileInputStream |
stream
The current file input stream. |
Fields inherited from class dk.rode.thesis.templatemethod.SequenceTemplate |
---|
POLICY |
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence |
---|
state |
Constructor Summary | |
---|---|
FileSequence(File directory,
FileFilter filter)
Constructor. |
|
FileSequence(FileSequence sequence)
Copy constructor. |
Method Summary | |
---|---|
private void |
closeStream()
|
FileSequence |
copy()
Returns a copy of this sequence that will start at the same sequence index as this sequence. |
protected byte[] |
nextValue(File file,
byte[] current)
Creates a new input stream based on file and reads
and returns the data. |
protected void |
nextValueClose(File key,
byte[] value)
Closes the current stream. |
protected File |
nextValueOpen(byte[] current)
Prepares this sequence for the next sequence value to
be delivered based on the current value supplied
as current . |
private FileInputStream |
openStream(File file)
Opens a new file input stream based on the file supplied as file . |
protected void |
sequenceClose()
Performs sub-class specific procedures for closing
this sequence. |
protected boolean |
sequenceOpen(Object... arguments)
Performs sub-class specific initialisation of this sequence before sequence values will be delivered, and returns true upon success. |
Methods inherited from class dk.rode.thesis.templatemethod.SequenceTemplate |
---|
bounded, close, consistent, current, ensureOpen, finalize, index, isClosed, next, reset, sequenceReset, size, unique |
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence |
---|
getStringablePolicy, state, toString, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private File directory
Never null.
private File[] files
Never null, and never empty as this sequence must always have a current value.
private FileFilter filter
directory
. Can be null, in which case all files are fetched.
private FileInputStream stream
Constructor Detail |
---|
public FileSequence(File directory, FileFilter filter) throws SequenceValueException
directory
- The directory to deliver the files and/or
directories; cannot be null.filter
- A filter to filter the files in directory
;
can be null, in which case all files and
directories are returned.
NullPointerException
- If directory
is null.
SequenceValueException
- If directory
does not exist, or if it
represents a file, or if the initialisation fails otherwise.public FileSequence(FileSequence sequence) throws SequenceValueException
If sequence
is closed
, this
sequence will be closed as well.
sequence
- The sequence to copy; cannot be null.
NullPointerException
- If sequence
is null.
SequenceValueException
- If the initialisation fails, or if the
current value of this sequence cannot be fetched.Method Detail |
---|
private final void closeStream() throws IOException
IOException
- If the closing fails.public FileSequence copy()
Sequence
protected byte[] nextValue(File file, byte[] current) throws Exception
file
and reads
and returns the data.
nextValue
in class SequenceTemplate<File,byte[]>
file
- The correlation key just created by
nextValueOpen(E)
; can be null.current
- The current (soon to be previous) sequence value; never null
unless initialising this sequence (once).
SequenceTemplate.next()
returns; null means sequence restart,
which is only allowed if this sequence is bounded
.
Exception
- In case value construction fails.protected void nextValueClose(File key, byte[] value) throws Exception
nextValueClose
in class SequenceTemplate<File,byte[]>
key
- The correlation key just created by
nextValueOpen(E)
;
can be null.value
- The sequence value just created by
nextValue(K, E)
; never null.
Exception
- In case the closing fails; not thrown by the
default implementationprotected File nextValueOpen(byte[] current) throws Exception
SequenceTemplate
next
sequence value to
be delivered based on the current
value supplied
as current
. This is step 1/3 in value creation, where
the next steps are nextValue(K, E)
and
nextValueClose(K, E)
.
The returned correlation key will be used to identify the
same creation process in subsequent calls to nextValue
and nextValueClose
.
nextValueOpen
in class SequenceTemplate<File,byte[]>
current
- The current (soon to be previous) sequence value; never null
unless initialising this sequence (once).
File
in the file stream; can be null in
case the stream has no more files (restart).
Exception
- In case the opening fails.private final FileInputStream openStream(File file) throws IOException
file
.
file
- The file to open the stream for; never null.
IOException
- If the stream cannot be opened.protected void sequenceClose() throws Exception
SequenceTemplate
closing
this sequence. Once this sequence is closed, sequence values can
no longer be delivered. Default implementation does nothing.
sequenceClose
in class SequenceTemplate<File,byte[]>
Exception
- If the super sequence close method throws an
exception.SequenceTemplate.sequenceReset()
,
SequenceTemplate.sequenceOpen(Object...)
protected boolean sequenceOpen(Object... arguments) throws Exception
SequenceTemplate
closed
.
Initialisation is performed upon construction
of new sequences and when
copying
existing
sequences, if needed. The arguments supplied to this (abstract) operation
is the varargs arguments supplied at construction time (e.g. via sub-class
implementations).
sequenceOpen
in class SequenceTemplate<File,byte[]>
arguments
- Arguments required by the initialisation, if any;
never null, but can be empty.
NullPointerException
- If no directory is supplied at
construction time.
IllegalArgumentException
- If the buffer size supplied at
construction time is illegal, or the file supplied does
not exist, or if not a directory, or if the directly
is empty.
Exception
- If the super sequence open method throws an
exception.SequenceTemplate.sequenceReset()
,
SequenceTemplate.sequenceClose()
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |