--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.List;
+
+import com.hughes.util.raf.RAFSerializable;
+
+public class Dictionary {
+
+ // persisted
+ List<PairEntry> pairEntries;
+
+ // persisted
+ List<EntrySource> sources;
+
+ // --------------------------------------------------------------------------
+
+ final class Index {
+ // One big list!
+ // Various sub-types.
+ // persisted
+ List<Row> rows;
+
+ // persisted
+ List<IndexEntry> sortedIndexEntries;
+
+ Dictionary getDict() {
+ return Dictionary.this;
+ }
+ }
+
+ static final class IndexEntry implements RAFSerializable<IndexEntry> {
+ String token;
+ int startRow;
+
+ public void write(RandomAccessFile raf) throws IOException {
+ raf.writeUTF(token);
+ raf.write(startRow);
+ }
+ }
+
+
+}
\ No newline at end of file
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.util.List;
+
+public abstract class Entry {
+
+ EntrySource entrySource;
+
+ abstract List<String> getMainTokens();
+ abstract List<String> getOtherTokens();
+
+}
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+public class EntrySource {
+
+}
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.List;
+
+public class PairEntry extends Entry {
+
+ @Override
+ List<String> getMainTokens() {
+ return null;
+ }
+
+ @Override
+ List<String> getOtherTokens() {
+ return null;
+ }
+
+
+ public static class Row extends RowWithIndex {
+ Row(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException {
+ super(raf, thisRowIndex, index);
+ }
+ public PairEntry getEntry() {
+ return index.getDict().pairEntries.get(referenceIndex);
+ }
+ }
+
+}
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+import com.hughes.util.raf.RAFListSerializer;
+
+public interface Row {
+
+ public void write(RandomAccessFile raf) throws IOException;
+
+ /**
+ * @return the TokenRow that this row is "filed under".
+ */
+ public TokenRow getTokenRow(final boolean search);
+
+ public void setTokenRow(final TokenRow tokenRow);
+
+
+ // Row must manage "disk-based" polymorphism. All other polymorphism is
+ // dealt with in the normal manner.
+ static class Serializer implements RAFListSerializer<Row> {
+
+ final Dictionary.Index index;
+
+ Serializer(final Dictionary.Index index) {
+ this.index = index;
+ }
+
+ @Override
+ public Row read(RandomAccessFile raf, final int listIndex) throws IOException {
+ final byte rowType = raf.readByte();
+ if (rowType == 0) {
+ return new PairEntry.Row(raf, listIndex, index);
+ } else if (rowType == 1) {
+ return new TokenRow(raf, listIndex, index);
+ }
+ throw new RuntimeException("Invalid rowType:" + rowType);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, Row t) throws IOException {
+ if (t instanceof PairEntry.Row) {
+ raf.writeByte(0);
+ } else if (t instanceof TokenRow) {
+ raf.writeByte(1);
+ }
+ }
+ };
+}
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+public abstract class RowWithIndex implements Row {
+ final Dictionary.Index index;
+ int thisRowIndex;
+ int referenceIndex;
+
+ TokenRow tokenRow = null;
+
+ RowWithIndex(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException {
+ this.index = index;
+ this.thisRowIndex = thisRowIndex;
+ this.referenceIndex = raf.readInt();
+ }
+
+ @Override
+ public void write(RandomAccessFile raf) throws IOException {
+ raf.writeInt(referenceIndex);
+ }
+
+ @Override
+ public TokenRow getTokenRow(final boolean search) {
+ if (tokenRow == null && search) {
+ int r = thisRowIndex - 1;
+ while (r >= 0) {
+ final Row row = index.rows.get(r);
+ final TokenRow candidate = row.getTokenRow(false);
+ if (candidate != null) {
+ for (++r; r <= thisRowIndex; ++r) {
+ index.rows.get(r).setTokenRow(candidate);
+ }
+ }
+ }
+ assert tokenRow != null;
+ }
+ return tokenRow;
+ }
+
+ @Override
+ public void setTokenRow(TokenRow tokenRow) {
+ assert this.tokenRow == null;
+ assert tokenRow != null;
+ this.tokenRow = tokenRow;
+ }
+
+
+}
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+public class TokenRow extends RowWithIndex {
+
+ TokenRow(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException {
+ super(raf, thisRowIndex, index);
+ }
+
+ @Override
+ public TokenRow getTokenRow(final boolean search) {
+ return this;
+ }
+
+ @Override
+ public void setTokenRow(TokenRow tokenRow) {
+ throw new RuntimeException("Shouldn't be setting TokenRow!");
+ }
+
+ public String getToken() {
+ return index.sortedIndexEntries.get(referenceIndex).token;
+ }
+
+
+}