1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
6 import com.hughes.util.raf.RAFListSerializer;
8 public abstract class RowBase {
10 * the Index owning this RowBase.
15 * Where this row lives within the list of Rows.
20 * Where this RowBase points to.
25 * the TokenRow above this RowBase, populated on demand.
27 TokenRow tokenRow = null;
29 RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
31 this.thisRowIndex = thisRowIndex; // where this was inside the list.
32 this.referenceIndex = raf.readInt(); // what this points to.
35 public void write(RandomAccessFile raf) throws IOException {
36 raf.writeInt(referenceIndex);
40 * @return the TokenRow that this row is "filed under".
42 public TokenRow getTokenRow(final boolean search) {
43 if (tokenRow == null && search) {
44 int r = thisRowIndex - 1;
46 final RowBase row = index.rows.get(r);
47 final TokenRow candidate = row.getTokenRow(false);
48 if (candidate != null) {
49 for (++r; r <= thisRowIndex; ++r) {
50 index.rows.get(r).setTokenRow(candidate);
54 assert tokenRow != null;
59 public void setTokenRow(TokenRow tokenRow) {
60 assert this.tokenRow == null;
61 assert tokenRow != null;
62 this.tokenRow = tokenRow;
66 public abstract Object draw(final String searchText);
69 // RowBase must manage "disk-based" polymorphism. All other polymorphism is
70 // dealt with in the normal manner.
71 static class Serializer implements RAFListSerializer<RowBase> {
75 Serializer(final Index index) {
80 public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
81 final byte rowType = raf.readByte();
83 return new PairEntry.Row(raf, listIndex, index);
84 } else if (rowType == 1) {
85 return new TokenRow(raf, listIndex, index);
87 throw new RuntimeException("Invalid rowType:" + rowType);
91 public void write(RandomAccessFile raf, RowBase t) throws IOException {
92 if (t instanceof PairEntry.Row) {
94 } else if (t instanceof TokenRow) {