// Assignment 4 - Pipe-and-Filter KWIC Indexing.
// Concurrent Programming, CS 498-598
// Summer 2002
//
// This code is unmaintained and may be obsolete (as well as being incorrect).
// See the answer to assignment 4 for possible updates.
import java.util.Vector;
class
SortFilter
extends FilterRunnableTemplate {
private Vector descriptions;
private void
add(KWICIndexEntryGroup entries) {
// Put the new entries into the database using insertion sort.
for (int i = entries.count() - 1; i >= 0; i--) {
descriptions.add(null);
final KWICIndexEntry e1 = entries.get(i);
int j = descriptions.size() - 1;
while (j > 0) {
final KWICIndexEntry e2 = (KWICIndexEntry) descriptions.get(j - 1);
if (e2.less(e1))
break;
descriptions.setElementAt(e2, j);
j--;
}
descriptions.setElementAt(e1, j);
}
}
protected void
end_loop() {
// On termination the output filter should send the sorted KWIC index entry
// database downstream.
try {
outb.put(new KWICIndexEntryGroup(descriptions));
outb.put(null);
}
catch (Exception ie) {
throw new RuntimeException();
}
}
protected boolean
isMisbehavingObject(Object o) {
KWICIndexEntryGroup kie = (KWICIndexEntryGroup) o;
return
(kie.count() == 1) && (kie.get(0).toString().compareTo("sort") == 0);
}
protected boolean
isValidObject(Object o) {
// The sort filter expects to receive KWIC index entry groups from the
// upstream (rotate) filter.
return o instanceof KWICIndexEntryGroup;
}
protected void
misbehave() {
throw new RuntimeException();
}
protected void
process_input(Object o) {
// Add each new group of KWIC index entries to the group.
add((KWICIndexEntryGroup) o);
}
SortFilter(String fn, buffer ib, buffer ob) {
super(fn, ib, ob);
descriptions = new Vector();
}
}
syntax highlighted by Code2HTML, v. 0.9