1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.PrintStream;
5 import java.io.RandomAccessFile;
7 import com.hughes.util.IndexedObject;
8 import com.hughes.util.raf.RAFListSerializer;
10 public abstract class RowBase extends IndexedObject {
12 * the Index owning this RowBase.
14 public final Index index;
17 * Where this RowBase points to.
19 public final int referenceIndex;
22 * the TokenRow above this RowBase, populated on demand.
24 private TokenRow tokenRow = null;
26 RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
29 this.referenceIndex = raf.readInt(); // what this points to.
32 public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
35 this.referenceIndex = referenceIndex;
39 * @return the TokenRow that this row is "filed under".
41 public TokenRow getTokenRow(final boolean search) {
42 if (tokenRow == null && search) {
45 final RowBase row = index.rows.get(r);
46 final TokenRow candidate = row.getTokenRow(false);
47 if (candidate != null) {
48 for (++r; r <= index(); ++r) {
49 index.rows.get(r).setTokenRow(candidate);
55 assert tokenRow != null;
60 public void setTokenRow(TokenRow tokenRow) {
61 assert this.tokenRow == null;
62 assert tokenRow != null;
63 this.tokenRow = tokenRow;
66 public abstract void print(PrintStream out);
68 public abstract String getRawText(final boolean compact);
70 // RowBase must manage "disk-based" polymorphism. All other polymorphism is
71 // dealt with in the normal manner.
72 static class Serializer implements RAFListSerializer<RowBase> {
76 Serializer(final Index index) {
81 public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
82 final byte rowType = raf.readByte();
84 return new PairEntry.Row(raf, listIndex, index);
85 } else if (rowType == 1) {
86 return new TokenRow(raf, listIndex, index);
88 throw new RuntimeException("Invalid rowType:" + rowType);
92 public void write(RandomAccessFile raf, RowBase t) throws IOException {
93 if (t instanceof PairEntry.Row) {
95 } else if (t instanceof TokenRow) {
98 raf.writeInt(t.referenceIndex);