1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.engine;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.io.RandomAccessFile;
21 import com.hughes.util.IndexedObject;
22 import com.hughes.util.raf.RAFListSerializer;
24 public abstract class RowBase extends IndexedObject {
26 * the Index owning this RowBase.
28 public final Index index;
31 * Where this RowBase points to.
33 public final int referenceIndex;
36 * the TokenRow above this RowBase, populated on demand.
38 private TokenRow tokenRow = null;
40 RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
43 this.referenceIndex = raf.readInt(); // what this points to.
46 public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
49 this.referenceIndex = referenceIndex;
53 * @return the TokenRow that this row is "filed under".
55 public TokenRow getTokenRow(final boolean search) {
56 if (tokenRow == null && search) {
58 int rUp = index() + 1;
60 final RowBase row = index.rows.get(r);
61 final TokenRow candidate = row.getTokenRow(false);
62 if (candidate != null) {
63 for (++r; r <= index(); ++r) {
64 index.rows.get(r).setTokenRow(candidate);
68 if (rUp < index.rows.size()) {
69 final RowBase rowUp = index.rows.get(rUp);
70 TokenRow candidateUp = rowUp.getTokenRow(false);
71 if (candidateUp != null) {
72 // Did we hit the next set of TokenRows?
73 if (candidateUp.index() > this.index()) {
74 final int tokenIndex = index.sortedIndexEntries.get(candidateUp.referenceIndex - 1).startRow;
75 candidateUp = (TokenRow) index.rows.get(tokenIndex);
77 for (--rUp; rUp >= index(); --rUp) {
78 index.rows.get(rUp).setTokenRow(candidateUp);
86 assert tokenRow != null;
91 public void setTokenRow(TokenRow tokenRow) {
92 assert this.tokenRow == null;
93 assert tokenRow != null;
94 this.tokenRow = tokenRow;
97 public abstract void print(PrintStream out);
99 public abstract String getRawText(final boolean compact);
101 // RowBase must manage "disk-based" polymorphism. All other polymorphism is
102 // dealt with in the normal manner.
103 static class Serializer implements RAFListSerializer<RowBase> {
107 Serializer(final Index index) {
112 public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
113 final byte rowType = raf.readByte();
115 return new PairEntry.Row(raf, listIndex, index);
116 } else if (rowType == 1) {
117 return new TokenRow(raf, listIndex, index);
118 } else if (rowType == 2) {
119 return new TextEntry.Row(raf, listIndex, index);
121 throw new RuntimeException("Invalid rowType:" + rowType);
125 public void write(RandomAccessFile raf, RowBase t) throws IOException {
126 if (t instanceof PairEntry.Row) {
128 } else if (t instanceof TokenRow) {
130 } else if (t instanceof TextEntry.Row) {
133 raf.writeInt(t.referenceIndex);