]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/PairEntry.java
WebView links starting to work (still timing problem).
[Dictionary.git] / src / com / hughes / android / dictionary / engine / PairEntry.java
index a84bc2a102c033f76221cee22cfb429b37648d84..3453dc357f12e329f5536d023d860612c90fc31f 100644 (file)
@@ -1,28 +1,46 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 package com.hughes.android.dictionary.engine;
 
+import com.hughes.util.raf.RAFListSerializer;
+import com.hughes.util.raf.RAFSerializable;
+import com.ibm.icu.text.Transliterator;
+
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.RandomAccessFile;
 import java.util.ArrayList;
 import java.util.List;
-
-import com.hughes.util.raf.RAFSerializable;
-import com.hughes.util.raf.RAFSerializer;
+import java.util.regex.Pattern;
 
 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>, Comparable<PairEntry> {
   
   public final List<Pair> pairs;
 
-  public PairEntry() {
-    pairs = new ArrayList<Pair>(1);
+  public PairEntry(final EntrySource entrySource) {
+    super(entrySource);
+    pairs = new ArrayList<Pair>(1);    
   }
 
-  public PairEntry(final String lang1, final String lang2) {
-    pairs = new ArrayList<Pair>(1);
+  public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
+    this(entrySource);
     this.pairs.add(new Pair(lang1, lang2));
   }
   
-  public PairEntry(final RandomAccessFile raf) throws IOException {
+  public PairEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index) throws IOException {
+    super(dictionary, raf, index);
     final int size = raf.readInt();
     pairs = new ArrayList<PairEntry.Pair>(size);
     for (int i = 0; i < size; ++i) {
@@ -31,18 +49,27 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
   }
   @Override
   public void write(RandomAccessFile raf) throws IOException {
+    super.write(raf);
     // TODO: this could be a short.
     raf.writeInt(pairs.size());
     for (int i = 0; i < pairs.size(); ++i) {
+      assert pairs.get(i).lang1.length() > 0;
       raf.writeUTF(pairs.get(i).lang1);
       raf.writeUTF(pairs.get(i).lang2);
     }
   }
   
-  static final RAFSerializer<PairEntry> SERIALIZER = new RAFSerializer<PairEntry>() {
+  static final class Serializer implements RAFListSerializer<PairEntry> {
+    
+    final Dictionary dictionary;
+    
+    Serializer(Dictionary dictionary) {
+      this.dictionary = dictionary;
+    }
+
     @Override
-    public PairEntry read(RandomAccessFile raf) throws IOException {
-      return new PairEntry(raf);
+    public PairEntry read(RandomAccessFile raf, int index) throws IOException {
+      return new PairEntry(dictionary, raf, index);
     }
 
     @Override
@@ -52,11 +79,17 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
   };
   
   @Override
-  public int addToDictionary(final Dictionary dictionary) {
+  public void addToDictionary(final Dictionary dictionary) {
+    assert index == -1;
     dictionary.pairEntries.add(this);
-    return dictionary.pairEntries.size() - 1;
+    index = dictionary.pairEntries.size() - 1;
   }
   
+  @Override
+  public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
+    return new Row(this.index, rowIndex, dictionaryIndex);
+  }
+
 
   // --------------------------------------------------------------------
   
@@ -72,6 +105,11 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
         final Index index) {
       super(referenceIndex, thisRowIndex, index);
     }
+    
+    @Override
+    public String toString() {
+      return getRawText(false);
+    }
 
     public PairEntry getEntry() {
       return index.dict.pairEntries.get(referenceIndex);
@@ -91,6 +129,42 @@ public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntr
       final PairEntry pairEntry = getEntry();
       return pairEntry.getRawText(compact);
     }
+
+    @Override
+    public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
+      final int side = swapPairEntries ? 1 : 0;
+      final List<Pair> pairs = getEntry().pairs;
+      final String[] pairSides = new String[pairs.size()];
+      for (int i = 0; i < pairs.size(); ++i) {
+        pairSides[i] = normalizer.transform(pairs.get(i).get(side));
+      }
+      for (int i = searchTokens.size() - 1; i >= 0; --i) {
+        final String searchToken = searchTokens.get(i);
+        boolean found = false;
+        for (final String pairSide : pairSides) {
+          found |= pairSide.contains(searchToken);
+        }
+        if (!found) {
+          return RowMatchType.NO_MATCH;
+        }
+      }
+      for (final String pairSide : pairSides) {
+        if (orderedMatchPattern.matcher(pairSide).find()) {
+          return RowMatchType.ORDERED_MATCH;
+        }
+      }
+      return RowMatchType.BAG_OF_WORDS_MATCH;
+    }
+    
+    @Override
+    public int getSideLength(boolean swapPairEntries) {
+      int result = 0;
+      final int side = swapPairEntries ? 1 : 0;
+      for (final Pair pair : getEntry().pairs) {
+        result += pair.get(side).length();
+      }
+      return result;
+    }
   
   }