]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/RowBase.java
Optimize imports, update dictionary info.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / RowBase.java
index 09141746c29cc254c547e7c742a2175cfb4800af..2e094a46a108102d5cd1f899496487eafce5baa1 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.IOException;
 import java.io.PrintStream;
 import java.io.RandomAccessFile;
-
-import com.hughes.util.IndexedObject;
-import com.hughes.util.raf.RAFListSerializer;
+import java.util.Comparator;
+import java.util.List;
+import java.util.regex.Pattern;
 
 public abstract class RowBase extends IndexedObject {
   /**
@@ -48,6 +52,34 @@ public abstract class RowBase extends IndexedObject {
     this.index = index;
     this.referenceIndex = referenceIndex;
   }
+  
+  static final class RowKey {
+    final Class<? extends RowBase> rowClass;
+    final int referenceIndex;
+    
+    private RowKey(Class<? extends RowBase> rowClass, int referenceIndex) {
+      this.rowClass = rowClass;
+      this.referenceIndex = referenceIndex;
+    }
+    
+    @Override
+    public boolean equals(Object o) {
+      if (!(o instanceof RowKey)) {
+        return false;
+      }
+      final RowKey that = (RowKey) o;
+      return this.referenceIndex == that.referenceIndex && this.rowClass.equals(that.rowClass);
+    }
+    
+    @Override
+    public int hashCode() {
+      return rowClass.hashCode() + referenceIndex;
+    }
+  }
+  
+  public RowKey getRowKey() {
+    return new RowKey(this.getClass(), referenceIndex);
+  }
 
   /**
    * @return the TokenRow that this row is "filed under".
@@ -97,6 +129,8 @@ public abstract class RowBase extends IndexedObject {
   public abstract void print(PrintStream out);
 
   public abstract String getRawText(final boolean compact);
+  
+  public abstract RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatch, final Transliterator normalizer, boolean swapPairEntries);
 
   // RowBase must manage "disk-based" polymorphism.  All other polymorphism is
   // dealt with in the normal manner.
@@ -113,10 +147,12 @@ public abstract class RowBase extends IndexedObject {
       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);
+      } 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);
       }
       throw new RuntimeException("Invalid rowType:" + rowType);
     }
@@ -126,12 +162,35 @@ public abstract class RowBase extends IndexedObject {
       if (t instanceof PairEntry.Row) {
         raf.writeByte(0);
       } else if (t instanceof TokenRow) {
-        raf.writeByte(1);
+        final TokenRow tokenRow = (TokenRow) t;
+        raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
       } else if (t instanceof TextEntry.Row) {
         raf.writeByte(2);
+      } else if (t instanceof HtmlEntry.Row) {
+        raf.writeByte(4);
       }
       raf.writeInt(t.referenceIndex);
     }
   }
+  
+  public static final class LengthComparator implements Comparator<RowBase> {
+    
+    final boolean swapPairEntries;
 
+    public LengthComparator(boolean swapPairEntries) {
+      this.swapPairEntries = swapPairEntries;
+    }
+
+    @Override
+    public int compare(RowBase row1, RowBase row2) {
+      final int l1 = row1.getSideLength(swapPairEntries);
+      final int l2 = row2.getSideLength(swapPairEntries);
+      return l1 < l2 ? -1 : l1 == l2 ? 0 : 1;
+    }
+  }
+
+  public int getSideLength(boolean swapPairEntries) {
+    return getRawText(false).length();
+  }
+  
 }