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) {
44 int rUp = index() + 1;
46 final RowBase row = index.rows.get(r);
47 final TokenRow candidate = row.getTokenRow(false);
48 if (candidate != null) {
49 for (++r; r <= index(); ++r) {
50 index.rows.get(r).setTokenRow(candidate);
54 if (rUp < index.rows.size()) {
55 final RowBase rowUp = index.rows.get(rUp);
56 TokenRow candidateUp = rowUp.getTokenRow(false);
57 if (candidateUp != null) {
58 // Did we hit the next set of TokenRows?
59 if (candidateUp.index() > this.index()) {
60 final int tokenIndex = index.sortedIndexEntries.get(candidateUp.referenceIndex - 1).startRow;
61 candidateUp = (TokenRow) index.rows.get(tokenIndex);
63 for (--rUp; rUp >= index(); --rUp) {
64 index.rows.get(rUp).setTokenRow(candidateUp);
72 assert tokenRow != null;
77 public void setTokenRow(TokenRow tokenRow) {
78 assert this.tokenRow == null;
79 assert tokenRow != null;
80 this.tokenRow = tokenRow;
83 public abstract void print(PrintStream out);
85 public abstract String getRawText(final boolean compact);
87 // RowBase must manage "disk-based" polymorphism. All other polymorphism is
88 // dealt with in the normal manner.
89 static class Serializer implements RAFListSerializer<RowBase> {
93 Serializer(final Index index) {
98 public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
99 final byte rowType = raf.readByte();
101 return new PairEntry.Row(raf, listIndex, index);
102 } else if (rowType == 1) {
103 return new TokenRow(raf, listIndex, index);
104 } else if (rowType == 2) {
105 return new TextEntry.Row(raf, listIndex, index);
107 throw new RuntimeException("Invalid rowType:" + rowType);
111 public void write(RandomAccessFile raf, RowBase t) throws IOException {
112 if (t instanceof PairEntry.Row) {
114 } else if (t instanceof TokenRow) {
116 } else if (t instanceof TextEntry.Row) {
119 raf.writeInt(t.referenceIndex);