]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
go
authorThad Hughes <thad.hughes@gmail.com>
Sun, 8 Aug 2010 12:55:25 +0000 (05:55 -0700)
committerThad Hughes <thad.hughes@gmail.com>
Sun, 8 Aug 2010 12:55:25 +0000 (05:55 -0700)
src/com/hughes/android/dictionary/Dictionary.java
src/com/hughes/android/dictionary/engine/Dictionary.java
src/com/hughes/android/dictionary/engine/EntrySource.java
src/com/hughes/android/dictionary/engine/Index.java
src/com/hughes/android/dictionary/engine/PairEntry.java
src/com/hughes/android/dictionary/engine/Row.java [deleted file]
src/com/hughes/android/dictionary/engine/RowBase.java [new file with mode: 0644]
src/com/hughes/android/dictionary/engine/RowWithIndex.java [deleted file]
src/com/hughes/android/dictionary/engine/TokenRow.java

index d8fd3c6b3bd95d6bc8e5d7e1cc806d4ca7944cbb..3133e281dcdd82b33436361ede4f92939ec40296 100755 (executable)
@@ -7,12 +7,12 @@ import java.util.List;
 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
@@ -40,8 +40,8 @@ public final class Dictionary implements RAFSerializable<Dictionary> {
 \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
@@ -53,8 +53,8 @@ public final class Dictionary implements RAFSerializable<Dictionary> {
 \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
@@ -82,16 +82,16 @@ public final class Dictionary implements RAFSerializable<Dictionary> {
         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
index 861ba4373b511a691f7944f9368240b5d3f815e7..cea0c7d22aa64a3ee4b957faee9200149a2ac78e 100644 (file)
@@ -1,7 +1,13 @@
 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 {
   
@@ -11,7 +17,32 @@ 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
index 07e825ff4baa879b918a018ec48f71c03fb33b56..f773f6096fe2b3b60223e3bf7d89e98e9797305b 100644 (file)
@@ -1,5 +1,17 @@
 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;
+  }
+  
 }
index 62c33248c12c43bb1e67cd76fb21ea0a5aff9c05..9185d9d4623a32378eb8b40bf9a7cc352d412f9f 100644 (file)
@@ -5,30 +5,67 @@ package com.hughes.android.dictionary.engine;
 
 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
index efdfa002c800861ab0ed5988b2ef692edfb8d1d2..e22969e3fe21dc1b41ea13fa4f7af32b07892057 100644 (file)
@@ -4,7 +4,29 @@ import java.io.IOException;
 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() {
@@ -15,16 +37,28 @@ public class PairEntry extends Entry {
   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;
+    }
   }
 
+
+
 }
diff --git a/src/com/hughes/android/dictionary/engine/Row.java b/src/com/hughes/android/dictionary/engine/Row.java
deleted file mode 100644 (file)
index fd10d5b..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-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);
-    }
-  };
-}
diff --git a/src/com/hughes/android/dictionary/engine/RowBase.java b/src/com/hughes/android/dictionary/engine/RowBase.java
new file mode 100644 (file)
index 0000000..95c2267
--- /dev/null
@@ -0,0 +1,102 @@
+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);
+    }
+  };
+
+
+}
diff --git a/src/com/hughes/android/dictionary/engine/RowWithIndex.java b/src/com/hughes/android/dictionary/engine/RowWithIndex.java
deleted file mode 100644 (file)
index b947ff8..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-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;
-  }
-
-
-}
index 3335d285aecb010082a5fd1ffe72ffa4860a027c..7a9f5d977f886b39121959e013b0bca17fce4b34 100644 (file)
@@ -3,7 +3,7 @@ package com.hughes.android.dictionary.engine;
 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);
@@ -16,12 +16,18 @@ public class TokenRow extends RowWithIndex {
 
   @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;
+  }
+
 
 }