]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/RowBase.java
Clean up order of imports.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / RowBase.java
index d76eb30bc6c6455a217280f62054a45c40a95cdb..6f49e00d69ac5046b98ab2bf57cd7c9626f747c2 100644 (file)
 
 package com.hughes.android.dictionary.engine;
 
-import com.hughes.util.IndexedObject;
-import com.hughes.util.raf.RAFListSerializer;
-import com.ibm.icu.text.Transliterator;
-
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.io.RandomAccessFile;
 import java.util.Comparator;
 import java.util.List;
 import java.util.regex.Pattern;
 
+import com.hughes.util.IndexedObject;
+import com.hughes.util.raf.RAFListSerializer;
+import com.ibm.icu.text.Transliterator;
+
 public abstract class RowBase extends IndexedObject {
     /**
      * the Index owning this RowBase.
      */
-    public final Index index;
+    final Index index;
 
     /**
      * Where this RowBase points to.
@@ -41,14 +42,14 @@ public abstract class RowBase extends IndexedObject {
      */
     private TokenRow tokenRow = null;
 
-    RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index)
-            throws IOException {
+    RowBase(final DataInput raf, final int thisRowIndex, final Index index, final int extra)
+    throws IOException {
         super(thisRowIndex);
         this.index = index;
-        this.referenceIndex = raf.readInt(); // what this points to.
+        this.referenceIndex = extra == -1 ? raf.readInt() : ((extra << 16) + raf.readUnsignedShort()); // what this points to.
     }
 
-    public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
+    RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
         super(thisRowIndex);
         this.index = index;
         this.referenceIndex = referenceIndex;
@@ -70,7 +71,7 @@ public abstract class RowBase extends IndexedObject {
             }
             final RowKey that = (RowKey) o;
             return this.referenceIndex == that.referenceIndex
-                    && this.rowClass.equals(that.rowClass);
+                   && this.rowClass.equals(that.rowClass);
         }
 
         @Override
@@ -106,7 +107,7 @@ public abstract class RowBase extends IndexedObject {
                         // Did we hit the next set of TokenRows?
                         if (candidateUp.index() > this.index()) {
                             final int tokenIndex = index.sortedIndexEntries
-                                    .get(candidateUp.referenceIndex - 1).startRow;
+                                                   .get(candidateUp.referenceIndex - 1).startRow;
                             candidateUp = (TokenRow) index.rows.get(tokenIndex);
                         }
                         for (--rUp; rUp >= index(); --rUp) {
@@ -123,7 +124,7 @@ public abstract class RowBase extends IndexedObject {
         return tokenRow;
     }
 
-    public void setTokenRow(TokenRow tokenRow) {
+    void setTokenRow(TokenRow tokenRow) {
         assert this.tokenRow == null;
         assert tokenRow != null;
         this.tokenRow = tokenRow;
@@ -134,7 +135,7 @@ public abstract class RowBase extends IndexedObject {
     public abstract String getRawText(final boolean compact);
 
     public abstract RowMatchType matches(final List<String> searchTokens,
-            final Pattern orderedMatch, final Transliterator normalizer, boolean swapPairEntries);
+                                         final Pattern orderedMatch, final Transliterator normalizer, boolean swapPairEntries);
 
     // RowBase must manage "disk-based" polymorphism. All other polymorphism is
     // dealt with in the normal manner.
@@ -147,33 +148,45 @@ public abstract class RowBase extends IndexedObject {
         }
 
         @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 || rowType == 3) {
-                return new TokenRow(raf, listIndex, index, /* hasMainEntry */rowType == 1);
-            } else if (rowType == 2) {
-                return new TextEntry.Row(raf, listIndex, index);
-            } else if (rowType == 4) {
-                return new HtmlEntry.Row(raf, listIndex, index);
+        public RowBase read(DataInput raf, final int listIndex) throws IOException {
+            int rowType = raf.readUnsignedByte();
+            int extra = -1;
+            if (rowType >= 0x20) {
+                extra = rowType & 0x1f;
+                rowType = (rowType >> 5) - 1;
+            }
+            switch (rowType) {
+                case 0:
+                    return new PairEntry.Row(raf, listIndex, index, extra);
+                case 1:
+                case 3:
+                    return new TokenRow(raf, listIndex, index, /* hasMainEntry */rowType == 1, extra);
+                case 2:
+                    return new TextEntry.Row(raf, listIndex, index, extra);
+                case 4:
+                    return new HtmlEntry.Row(raf, listIndex, index, extra);
             }
             throw new RuntimeException("Invalid rowType:" + rowType);
         }
 
         @Override
-        public void write(RandomAccessFile raf, RowBase t) throws IOException {
+        public void write(DataOutput raf, RowBase t) throws IOException {
+            int type = 0;
             if (t instanceof PairEntry.Row) {
-                raf.writeByte(0);
+                type = 0;
             } else if (t instanceof TokenRow) {
                 final TokenRow tokenRow = (TokenRow) t;
-                raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
+                type = tokenRow.hasMainEntry ? 1 : 3;
             } else if (t instanceof TextEntry.Row) {
-                raf.writeByte(2);
+                type = 2;
             } else if (t instanceof HtmlEntry.Row) {
-                raf.writeByte(4);
+                type = 4;
             }
-            raf.writeInt(t.referenceIndex);
+            assert t.referenceIndex < (1 << 21);
+            if ((t.referenceIndex >> 16) >= (1 << 5))
+                throw new RuntimeException("referenceIndex larger than supported max");
+            raf.writeByte(((type + 1) << 5) + (t.referenceIndex >> 16));
+            raf.writeShort(t.referenceIndex);
         }
     }
 
@@ -193,7 +206,7 @@ public abstract class RowBase extends IndexedObject {
         }
     }
 
-    public int getSideLength(boolean swapPairEntries) {
+    int getSideLength(boolean swapPairEntries) {
         return getRawText(false).length();
     }