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;
20 import java.util.List;
22 import com.hughes.util.IndexedObject;
23 import com.hughes.util.raf.RAFListSerializer;
24 import com.ibm.icu.text.Transliterator;
26 public abstract class RowBase extends IndexedObject {
28 * the Index owning this RowBase.
30 public final Index index;
33 * Where this RowBase points to.
35 public final int referenceIndex;
38 * the TokenRow above this RowBase, populated on demand.
40 private TokenRow tokenRow = null;
42 RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
45 this.referenceIndex = raf.readInt(); // what this points to.
48 public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
51 this.referenceIndex = referenceIndex;
55 * @return the TokenRow that this row is "filed under".
57 public TokenRow getTokenRow(final boolean search) {
58 if (tokenRow == null && search) {
60 int rUp = index() + 1;
62 final RowBase row = index.rows.get(r);
63 final TokenRow candidate = row.getTokenRow(false);
64 if (candidate != null) {
65 for (++r; r <= index(); ++r) {
66 index.rows.get(r).setTokenRow(candidate);
70 if (rUp < index.rows.size()) {
71 final RowBase rowUp = index.rows.get(rUp);
72 TokenRow candidateUp = rowUp.getTokenRow(false);
73 if (candidateUp != null) {
74 // Did we hit the next set of TokenRows?
75 if (candidateUp.index() > this.index()) {
76 final int tokenIndex = index.sortedIndexEntries.get(candidateUp.referenceIndex - 1).startRow;
77 candidateUp = (TokenRow) index.rows.get(tokenIndex);
79 for (--rUp; rUp >= index(); --rUp) {
80 index.rows.get(rUp).setTokenRow(candidateUp);
88 assert tokenRow != null;
93 public void setTokenRow(TokenRow tokenRow) {
94 assert this.tokenRow == null;
95 assert tokenRow != null;
96 this.tokenRow = tokenRow;
99 public abstract void print(PrintStream out);
101 public abstract String getRawText(final boolean compact);
103 public abstract RowMatchType matches(final List<String> searchTokens, final Transliterator normalizer, boolean swapPairEntries);
105 // RowBase must manage "disk-based" polymorphism. All other polymorphism is
106 // dealt with in the normal manner.
107 static class Serializer implements RAFListSerializer<RowBase> {
111 Serializer(final Index index) {
116 public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
117 final byte rowType = raf.readByte();
119 return new PairEntry.Row(raf, listIndex, index);
120 } else if (rowType == 1 || rowType == 3) {
121 return new TokenRow(raf, listIndex, index, rowType == 1);
122 } else if (rowType == 2) {
123 return new TextEntry.Row(raf, listIndex, index);
125 throw new RuntimeException("Invalid rowType:" + rowType);
129 public void write(RandomAccessFile raf, RowBase t) throws IOException {
130 if (t instanceof PairEntry.Row) {
132 } else if (t instanceof TokenRow) {
133 final TokenRow tokenRow = (TokenRow) t;
134 raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
135 } else if (t instanceof TextEntry.Row) {
138 raf.writeInt(t.referenceIndex);