|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object Seq<T>
public abstract class Seq<T>
Implements a generic sequence that can be split, spliced, reversed, or tested for equality.
Note
This class requires two subclasses. All documentation is here. Use
new Seq0<whatever>()
new Seq2<whatever>( <<instance of your generic type>>, <<some Seq<whatever>>> )
new Seq2<whatever>( <<instance of your generic type>> )
Warning
This is a rather low level abstraction and is not foolproof. If you create a "circular" sequence or use two iterators to alter one sequence, you must be prepared for the consequences.
Examples of UsageThe following code obtains integers from a scanner and then prints them on one line.
Seq<Integer> seq = new Seq0<Integer>(); while( scan.hasNextInt() ) seq = new Seq2<Integer>( scan.nextInt(), seq ); seq = seq.reverse(); System.out.println(seq);
Seq<Integer>.SeqIterator i = seq.iterator(); while( i.count()<4 ) i.next(); i.setFirst(10);
public static <T> Seq<T> removeFirstOccurence(Seqseq, T removeMe) { Seq .SeqIterator i = seq.iterator(); if( !i.hasNext() ) return seq; if( removeMe==i.next() ) { i.split(); return i.peek(); } while(i.hasNext() && !i.peek().getFirst().equals(removeMe)) { i.next(); } if( i.hasNext() ) { Seq .SeqIterator j = i.split(); i.next(); j.splice(i.peek()); } return seq; }
Implementation Notes:
1) The Seq, Seq0, and Seq2 classes were created to illustrate how object orientation uses subclassing to eliminate branching. The only if statements I permitted myself were to prevent misuse of the methods. The fact that empty sequences must be handled differently than nonempty sequences is taken care of using a subclass Seq0 for empty classes and a subclass Seq2 for nonempty classes.
2) A secondary goal was to illustrate uses of recursion. The implementation of toString() is a particular example of this: the recursion gathers information as it "reads" the sequence and fills an array of chars as it comes back.
3) Normally one would want the empty sequence to be unique. Implementing that way requires the use of a static which conflicts with Java's generics. Accordingly, this implementation permits many empty sequences and overrides equals to make them all equal. An added benefit is that you can compare two sequences for equality. The comparison will make use of the equals method of the generic type.
4) Seq2 uses the standard car,cdr way of implementing a sequence as a list. If you want something more esoteric, you can probably get it by creating an alternative to Seq2 and leaving both Seq and Seq0 unchanged.
Nested Class Summary | |
---|---|
class |
Seq.SeqIterator
An iterator class capable of mutating the sequence. |
Constructor Summary | |
---|---|
Seq()
|
Method Summary | |
---|---|
abstract Seq<T> |
copy()
Accessor that copies the sequence. |
abstract T |
getFirst()
Accessor returning the first element in sequence. |
protected abstract Seq<T> |
getSubseq()
|
abstract boolean |
isEmpty()
Accessor testing emptyness. |
Seq.SeqIterator |
iterator()
Create an iterator over the elements of the sequence. |
abstract int |
length()
Accessor determining length. |
Seq<T> |
reverse()
Accessor that returns a copy in of the sequence in reverse order. |
protected abstract Seq<T> |
reverseRecurs(Seq<T> buildMe)
|
abstract void |
setFirst(T t)
Mutator that resets the first value of the sequence. |
protected abstract void |
setSubseq(Seq<T> new_subseq)
|
java.lang.String |
toString()
String representation obtained applying each elements toString. |
protected abstract char[] |
toStringRecurs(int where)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Seq()
Method Detail |
---|
public abstract boolean isEmpty()
public abstract int length()
public abstract void setFirst(T t)
t
- a generic value to replace the first in the sequencepublic abstract T getFirst()
public Seq.SeqIterator iterator()
public abstract Seq<T> copy()
public Seq<T> reverse()
public java.lang.String toString()
toString
in class java.lang.Object
protected abstract char[] toStringRecurs(int where)
protected abstract Seq<T> reverseRecurs(Seq<T> buildMe)
protected abstract Seq<T> getSubseq()
protected abstract void setSubseq(Seq<T> new_subseq)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |