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.Comparator;
21 import java.util.List;
22 import java.util.regex.Pattern;
24 import com.hughes.util.IndexedObject;
25 import com.hughes.util.raf.RAFListSerializer;
26 import com.ibm.icu.text.Transliterator;
28 public abstract class RowBase extends IndexedObject {
30 * the Index owning this RowBase.
32 public final Index index;
35 * Where this RowBase points to.
37 public final int referenceIndex;
40 * the TokenRow above this RowBase, populated on demand.
42 private TokenRow tokenRow = null;
44 RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
47 this.referenceIndex = raf.readInt(); // what this points to.
50 public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
53 this.referenceIndex = referenceIndex;
56 static final class RowKey {
57 final Class<? extends RowBase> rowClass;
58 final int referenceIndex;
60 private RowKey(Class<? extends RowBase> rowClass, int referenceIndex) {
61 this.rowClass = rowClass;
62 this.referenceIndex = referenceIndex;
66 public boolean equals(Object o) {
67 if (!(o instanceof RowKey)) {
70 final RowKey that = (RowKey) o;
71 return this.referenceIndex == that.referenceIndex && this.rowClass.equals(that.rowClass);
75 public int hashCode() {
76 return rowClass.hashCode() + referenceIndex;
80 public RowKey getRowKey() {
81 return new RowKey(this.getClass(), referenceIndex);
85 * @return the TokenRow that this row is "filed under".
87 public TokenRow getTokenRow(final boolean search) {
88 if (tokenRow == null && search) {
90 int rUp = index() + 1;
92 final RowBase row = index.rows.get(r);
93 final TokenRow candidate = row.getTokenRow(false);
94 if (candidate != null) {
95 for (++r; r <= index(); ++r) {
96 index.rows.get(r).setTokenRow(candidate);
100 if (rUp < index.rows.size()) {
101 final RowBase rowUp = index.rows.get(rUp);
102 TokenRow candidateUp = rowUp.getTokenRow(false);
103 if (candidateUp != null) {
104 // Did we hit the next set of TokenRows?
105 if (candidateUp.index() > this.index()) {
106 final int tokenIndex = index.sortedIndexEntries.get(candidateUp.referenceIndex - 1).startRow;
107 candidateUp = (TokenRow) index.rows.get(tokenIndex);
109 for (--rUp; rUp >= index(); --rUp) {
110 index.rows.get(rUp).setTokenRow(candidateUp);
118 assert tokenRow != null;
123 public void setTokenRow(TokenRow tokenRow) {
124 assert this.tokenRow == null;
125 assert tokenRow != null;
126 this.tokenRow = tokenRow;
129 public abstract void print(PrintStream out);
131 public abstract String getRawText(final boolean compact);
133 public abstract RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatch, final Transliterator normalizer, boolean swapPairEntries);
135 // RowBase must manage "disk-based" polymorphism. All other polymorphism is
136 // dealt with in the normal manner.
137 static class Serializer implements RAFListSerializer<RowBase> {
141 Serializer(final Index index) {
146 public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
147 final byte rowType = raf.readByte();
149 return new PairEntry.Row(raf, listIndex, index);
150 } else if (rowType == 1 || rowType == 3) {
151 return new TokenRow(raf, listIndex, index, /* hasMainEntry */ rowType == 1);
152 } else if (rowType == 2) {
153 return new TextEntry.Row(raf, listIndex, index);
154 } else if (rowType == 4) {
155 return new HtmlEntry.Row(raf, listIndex, index);
157 throw new RuntimeException("Invalid rowType:" + rowType);
161 public void write(RandomAccessFile raf, RowBase t) throws IOException {
162 if (t instanceof PairEntry.Row) {
164 } else if (t instanceof TokenRow) {
165 final TokenRow tokenRow = (TokenRow) t;
166 raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
167 } else if (t instanceof TextEntry.Row) {
169 } else if (t instanceof HtmlEntry.Row) {
172 raf.writeInt(t.referenceIndex);
176 public static final class LengthComparator implements Comparator<RowBase> {
178 final boolean swapPairEntries;
180 public LengthComparator(boolean swapPairEntries) {
181 this.swapPairEntries = swapPairEntries;
185 public int compare(RowBase row1, RowBase row2) {
186 final int l1 = row1.getSideLength(swapPairEntries);
187 final int l2 = row2.getSideLength(swapPairEntries);
188 return l1 < l2 ? -1 : l1 == l2 ? 0 : 1;
192 public int getSideLength(boolean swapPairEntries) {
193 return getRawText(false).length();