import java.util.concurrent.atomic.AtomicBoolean;\r
\r
import com.hughes.util.CachingList;\r
-import com.hughes.util.raf.FileList;\r
+import com.hughes.util.raf.RAFList;\r
import com.hughes.util.raf.RAFFactory;\r
import com.hughes.util.raf.RAFSerializable;\r
import com.hughes.util.raf.RAFSerializableSerializer;\r
import com.hughes.util.raf.RAFSerializer;\r
-import com.hughes.util.raf.UniformFileList;\r
+import com.hughes.util.raf.UniformRAFList;\r
\r
public final class Dictionary implements RAFSerializable<Dictionary> {\r
\r
\r
public Dictionary(final RandomAccessFile raf) throws IOException {\r
dictionaryInfo = raf.readUTF();\r
- sources = new ArrayList<String>(FileList.create(raf, RAFSerializer.STRING, raf.getFilePointer()));\r
- entries = CachingList.create(FileList.create(raf, ENTRY_SERIALIZER, raf\r
+ sources = new ArrayList<String>(RAFList.create(raf, RAFSerializer.STRING, raf.getFilePointer()));\r
+ entries = CachingList.create(RAFList.create(raf, ENTRY_SERIALIZER, raf\r
.getFilePointer()), 10000);\r
languageDatas[0] = new LanguageData(this, raf, SimpleEntry.LANG1);\r
languageDatas[1] = new LanguageData(this, raf, SimpleEntry.LANG2);\r
\r
public void write(RandomAccessFile raf) throws IOException {\r
raf.writeUTF(dictionaryInfo);\r
- FileList.write(raf, sources, RAFSerializer.STRING);\r
- FileList.write(raf, entries, ENTRY_SERIALIZER);\r
+ RAFList.write(raf, sources, RAFSerializer.STRING);\r
+ RAFList.write(raf, entries, ENTRY_SERIALIZER);\r
languageDatas[0].write(raf);\r
languageDatas[1].write(raf);\r
raf.writeUTF(VERSION_CODE);\r
throw new RuntimeException("Unknown language.");\r
}\r
this.lang = lang;\r
- rows = CachingList.create(UniformFileList.create(raf, ROW_SERIALIZER, raf\r
+ rows = CachingList.create(UniformRAFList.create(raf, ROW_SERIALIZER, raf\r
.getFilePointer()), 10000);\r
- sortedIndex = CachingList.create(FileList.create(raf,\r
+ sortedIndex = CachingList.create(RAFList.create(raf,\r
INDEX_ENTRY_SERIALIZER, raf.getFilePointer()), 10000);\r
}\r
\r
public void write(final RandomAccessFile raf) throws IOException {\r
raf.writeUTF(language.symbol);\r
- UniformFileList.write(raf, rows, ROW_SERIALIZER, 4);\r
- FileList.write(raf, sortedIndex, INDEX_ENTRY_SERIALIZER);\r
+ UniformRAFList.write(raf, rows, ROW_SERIALIZER, 4);\r
+ RAFList.write(raf, sortedIndex, INDEX_ENTRY_SERIALIZER);\r
}\r
\r
String rowToString(final Row row, final boolean onlyFirstSubentry) {\r
package com.hughes.android.dictionary.engine;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.ArrayList;
import java.util.List;
+import com.hughes.util.raf.RAFList;
+import com.hughes.util.raf.RAFSerializer;
+
public class Dictionary {
// persisted
final List<EntrySource> sources;
+ // persisted
final List<Index> indices;
+
+ public Dictionary() {
+ pairEntries = new ArrayList<PairEntry>();
+ sources = new ArrayList<EntrySource>();
+ indices = new ArrayList<Index>();
+ }
+
+ public Dictionary(final RandomAccessFile raf) throws IOException {
+ pairEntries = RAFList.create(raf, PairEntry.SERIALIZER, raf.getFilePointer());
+ sources = new ArrayList<EntrySource>();
+
+ final RAFSerializer<Index> indexSerializer = new RAFSerializer<Index>() {
+
+ @Override
+ public Index read(RandomAccessFile raf) throws IOException {
+ return new Index(Dictionary.this, raf);
+ }
+ @Override
+ public void write(RandomAccessFile raf, Index t) throws IOException {
+ t.write(raf);
+
+ }};
+ indices = RAFList.create(raf, indexSerializer, raf.getFilePointer());
+ }
}
\ No newline at end of file
package com.hughes.android.dictionary.engine;
-public class EntrySource {
+import java.io.Serializable;
+import com.hughes.util.IndexedObject;
+
+public class EntrySource extends IndexedObject implements Serializable {
+
+ private static final long serialVersionUID = -1323165134846120269L;
+
+ final String name;
+
+ public EntrySource(final String name) {
+ this.name = name;
+ }
+
}
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.util.Collection;
import java.util.List;
-import com.hughes.android.dictionary.engine.Dictionary.Index.IndexEntry;
+import com.hughes.util.raf.RAFList;
import com.hughes.util.raf.RAFSerializable;
+import com.hughes.util.raf.RAFSerializer;
+import com.hughes.util.raf.UniformRAFList;
-final class Index {
- final Dictionary dict;
- final String name;
+final class Index implements RAFSerializable<Index> {
+ final Dictionary dict;
+ final String shortName;
+ final String longName;
+
+ // persisted
+ final List<Index.IndexEntry> sortedIndexEntries;
+
// One big list!
// Various sub-types.
// persisted
- final List<Row> rows;
+ final List<RowBase> rows;
- // persisted
- final List<Index.IndexEntry> sortedIndexEntries;
+ // --------------------------------------------------------------------------
+
+ public Index(final Dictionary dict, final RandomAccessFile raf) throws IOException {
+ this.dict = dict;
+ shortName = raf.readUTF();
+ longName = raf.readUTF();
+ // TODO: caching
+ sortedIndexEntries = RAFList.create(raf, IndexEntry.SERIALIZER, raf.getFilePointer());
+ rows = UniformRAFList.create(raf, new RowBase.Serializer(this), raf.getFilePointer());
+ }
+ @Override
+ public void write(final RandomAccessFile raf) throws IOException {
+ raf.writeUTF(shortName);
+ raf.writeUTF(longName);
+ RAFList.write(raf, sortedIndexEntries, IndexEntry.SERIALIZER);
+ UniformRAFList.write(raf, (Collection<RowBase>) rows, new RowBase.Serializer(this), 5);
+ }
+
static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
String token;
int startRow;
-
+ static final RAFSerializer<IndexEntry> SERIALIZER = new RAFSerializer<IndexEntry> () {
+ @Override
+ public IndexEntry read(RandomAccessFile raf) throws IOException {
+ return new IndexEntry(raf);
+ }
+ @Override
+ public void write(RandomAccessFile raf, IndexEntry t) throws IOException {
+ t.write(raf);
+ }};
+ public IndexEntry(final RandomAccessFile raf) throws IOException {
+ token = raf.readUTF();
+ startRow = raf.readInt();
+ }
public void write(RandomAccessFile raf) throws IOException {
raf.writeUTF(token);
raf.write(startRow);
}
}
+
}
\ No newline at end of file
import java.io.RandomAccessFile;
import java.util.List;
-public class PairEntry extends Entry {
+import com.hughes.util.raf.RAFSerializable;
+import com.hughes.util.raf.RAFSerializer;
+
+public class PairEntry extends Entry implements RAFSerializable<PairEntry> {
+
+ public PairEntry(final RandomAccessFile raf) {
+ }
+ @Override
+ public void write(RandomAccessFile raf) throws IOException {
+ }
+
+ static final RAFSerializer<PairEntry> SERIALIZER = new RAFSerializer<PairEntry>() {
+ @Override
+ public PairEntry read(RandomAccessFile raf) throws IOException {
+ return new PairEntry(raf);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, PairEntry t) throws IOException {
+ t.write(raf);
+ }
+ };
+
@Override
List<String> getMainTokens() {
List<String> getOtherTokens() {
return null;
}
+
+
- public static class Row extends RowWithIndex {
+ public static class Row extends RowBase {
+
Row(final RandomAccessFile raf, final int thisRowIndex,
final Index index) throws IOException {
super(raf, thisRowIndex, index);
}
+
public PairEntry getEntry() {
return index.dict.pairEntries.get(referenceIndex);
}
+
+ @Override
+ public Object draw(String searchText) {
+ // TODO Auto-generated method stub
+ return null;
+ }
}
+
+
}
+++ /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);
-
- public Object draw(final String searchText);
-
-
- // Row must manage "disk-based" polymorphism. All other polymorphism is
- // dealt with in the normal manner.
- static class Serializer implements RAFListSerializer<Row> {
-
- final Index index;
-
- Serializer(final 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);
- }
- t.write(raf);
- }
- };
-}
--- /dev/null
+package com.hughes.android.dictionary.engine;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+import com.hughes.util.raf.RAFListSerializer;
+
+public abstract class RowBase {
+ /**
+ * the Index owning this RowBase.
+ */
+ final Index index;
+
+ /**
+ * Where this row lives within the list of Rows.
+ */
+ int thisRowIndex;
+
+ /**
+ * Where this RowBase points to.
+ */
+ int referenceIndex;
+
+ /**
+ * the TokenRow above this RowBase, populated on demand.
+ */
+ TokenRow tokenRow = null;
+
+ RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
+ this.index = index;
+ this.thisRowIndex = thisRowIndex; // where this was inside the list.
+ this.referenceIndex = raf.readInt(); // what this points to.
+ }
+
+ public void write(RandomAccessFile raf) throws IOException {
+ raf.writeInt(referenceIndex);
+ }
+
+ /**
+ * @return the TokenRow that this row is "filed under".
+ */
+ public TokenRow getTokenRow(final boolean search) {
+ if (tokenRow == null && search) {
+ int r = thisRowIndex - 1;
+ while (r >= 0) {
+ final RowBase 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;
+ }
+
+ public void setTokenRow(TokenRow tokenRow) {
+ assert this.tokenRow == null;
+ assert tokenRow != null;
+ this.tokenRow = tokenRow;
+ }
+
+
+ public abstract Object draw(final String searchText);
+
+
+ // RowBase must manage "disk-based" polymorphism. All other polymorphism is
+ // dealt with in the normal manner.
+ static class Serializer implements RAFListSerializer<RowBase> {
+
+ final Index index;
+
+ Serializer(final Index index) {
+ this.index = index;
+ }
+
+ @Override
+ public RowBase 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, RowBase t) throws IOException {
+ if (t instanceof PairEntry.Row) {
+ raf.writeByte(0);
+ } else if (t instanceof TokenRow) {
+ raf.writeByte(1);
+ }
+ t.write(raf);
+ }
+ };
+
+
+}
+++ /dev/null
-package com.hughes.android.dictionary.engine;
-
-import java.io.IOException;
-import java.io.RandomAccessFile;
-
-public abstract class RowWithIndex implements Row {
- final Index index;
- int thisRowIndex;
- int referenceIndex;
-
- TokenRow tokenRow = null;
-
- RowWithIndex(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
- this.index = index;
- this.thisRowIndex = thisRowIndex; // where this was inside the list.
- this.referenceIndex = raf.readInt(); // what this points to.
- }
-
- @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;
- }
-
-
-}
import java.io.IOException;
import java.io.RandomAccessFile;
-public class TokenRow extends RowWithIndex {
+public class TokenRow extends RowBase {
TokenRow(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
super(raf, thisRowIndex, index);
@Override
public void setTokenRow(TokenRow tokenRow) {
- throw new RuntimeException("Shouldn't be setting TokenRow!");
+ throw new RuntimeException("Shouldn't be setting TokenRow's TokenRow!");
}
public String getToken() {
return index.sortedIndexEntries.get(referenceIndex).token;
}
+ @Override
+ public Object draw(String searchText) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
}