]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
go
authorThad Hughes <thad.hughes@gmail.com>
Sat, 24 Jul 2010 00:57:40 +0000 (17:57 -0700)
committerThad Hughes <thad.hughes@gmail.com>
Sat, 24 Jul 2010 00:57:40 +0000 (17:57 -0700)
src/com/hughes/android/dictionary/engine/Dictionary.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/Entry.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/EntrySource.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/PairEntry.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/Row.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/RowWithIndex.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/TokenRow.java [new file with mode: 0644]

diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java
new file mode 100644 (file)
index 0000000..8f1db2d
--- /dev/null
@@ -0,0 +1,44 @@
+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
diff --git a/src/com/hughes/android/dictionary/engine/Entry.java b/src/com/hughes/android/dictionary/engine/Entry.java
new file mode 100644 (file)
index 0000000..af279c1
--- /dev/null
@@ -0,0 +1,12 @@
+package com.hughes.android.dictionary.engine;
+
+import java.util.List;
+
+public abstract class Entry {
+  
+  EntrySource entrySource;
+  
+  abstract List<String> getMainTokens();
+  abstract List<String> getOtherTokens();
+  
+}
diff --git a/src/com/hughes/android/dictionary/engine/EntrySource.java b/src/com/hughes/android/dictionary/engine/EntrySource.java
new file mode 100644 (file)
index 0000000..07e825f
--- /dev/null
@@ -0,0 +1,5 @@
+package com.hughes.android.dictionary.engine;
+
+public class EntrySource {
+
+}
diff --git a/src/com/hughes/android/dictionary/engine/PairEntry.java b/src/com/hughes/android/dictionary/engine/PairEntry.java
new file mode 100644 (file)
index 0000000..40282a0
--- /dev/null
@@ -0,0 +1,29 @@
+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);
+    }
+  }
+
+}
diff --git a/src/com/hughes/android/dictionary/engine/Row.java b/src/com/hughes/android/dictionary/engine/Row.java
new file mode 100644 (file)
index 0000000..7c2c927
--- /dev/null
@@ -0,0 +1,50 @@
+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);
+      }
+    }
+  };
+}
diff --git a/src/com/hughes/android/dictionary/engine/RowWithIndex.java b/src/com/hughes/android/dictionary/engine/RowWithIndex.java
new file mode 100644 (file)
index 0000000..00d7db8
--- /dev/null
@@ -0,0 +1,50 @@
+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;
+  }
+
+
+}
diff --git a/src/com/hughes/android/dictionary/engine/TokenRow.java b/src/com/hughes/android/dictionary/engine/TokenRow.java
new file mode 100644 (file)
index 0000000..61a35d2
--- /dev/null
@@ -0,0 +1,27 @@
+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;
+  }
+
+
+}