]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/PairEntry.java
Apply result of "code cleanup" run.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / PairEntry.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import com.hughes.util.raf.RAFListSerializer;
18 import com.hughes.util.raf.RAFSerializable;
19 import com.ibm.icu.text.Transliterator;
20
21 import java.io.IOException;
22 import java.io.PrintStream;
23 import java.io.RandomAccessFile;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
27
28 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
29         Comparable<PairEntry> {
30
31     public final List<Pair> pairs;
32
33     public PairEntry(final EntrySource entrySource) {
34         super(entrySource);
35         pairs = new ArrayList<Pair>(1);
36     }
37
38     public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
39         this(entrySource);
40         this.pairs.add(new Pair(lang1, lang2));
41     }
42
43     public PairEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index)
44             throws IOException {
45         super(dictionary, raf, index);
46         final int size = raf.readInt();
47         pairs = new ArrayList<PairEntry.Pair>(size);
48         for (int i = 0; i < size; ++i) {
49             pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
50         }
51     }
52
53     @Override
54     public void write(RandomAccessFile raf) throws IOException {
55         super.write(raf);
56         // TODO: this could be a short.
57         raf.writeInt(pairs.size());
58         for (int i = 0; i < pairs.size(); ++i) {
59             assert pairs.get(i).lang1.length() > 0;
60             raf.writeUTF(pairs.get(i).lang1);
61             raf.writeUTF(pairs.get(i).lang2);
62         }
63     }
64
65     static final class Serializer implements RAFListSerializer<PairEntry> {
66
67         final Dictionary dictionary;
68
69         Serializer(Dictionary dictionary) {
70             this.dictionary = dictionary;
71         }
72
73         @Override
74         public PairEntry read(RandomAccessFile raf, int index) throws IOException {
75             return new PairEntry(dictionary, raf, index);
76         }
77
78         @Override
79         public void write(RandomAccessFile raf, PairEntry t) throws IOException {
80             t.write(raf);
81         }
82     }
83
84     @Override
85     public void addToDictionary(final Dictionary dictionary) {
86         assert index == -1;
87         dictionary.pairEntries.add(this);
88         index = dictionary.pairEntries.size() - 1;
89     }
90
91     @Override
92     public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
93         return new Row(this.index, rowIndex, dictionaryIndex);
94     }
95
96     // --------------------------------------------------------------------
97
98     public static class Row extends RowBase {
99
100         Row(final RandomAccessFile raf, final int thisRowIndex,
101                 final Index index) throws IOException {
102             super(raf, thisRowIndex, index);
103         }
104
105         Row(final int referenceIndex, final int thisRowIndex,
106                 final Index index) {
107             super(referenceIndex, thisRowIndex, index);
108         }
109
110         @Override
111         public String toString() {
112             return getRawText(false);
113         }
114
115         public PairEntry getEntry() {
116             return index.dict.pairEntries.get(referenceIndex);
117         }
118
119         @Override
120         public void print(PrintStream out) {
121             final PairEntry pairEntry = getEntry();
122             for (int i = 0; i < pairEntry.pairs.size(); ++i) {
123                 out.print((i == 0 ? "  " : "    ") + pairEntry.pairs.get(i));
124                 out.println();
125             }
126         }
127
128         @Override
129         public String getRawText(boolean compact) {
130             final PairEntry pairEntry = getEntry();
131             return pairEntry.getRawText(compact);
132         }
133
134         @Override
135         public RowMatchType matches(final List<String> searchTokens,
136                 final Pattern orderedMatchPattern, final Transliterator normalizer,
137                 final boolean swapPairEntries) {
138             final int side = swapPairEntries ? 1 : 0;
139             final List<Pair> pairs = getEntry().pairs;
140             final String[] pairSides = new String[pairs.size()];
141             for (int i = 0; i < pairs.size(); ++i) {
142                 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
143             }
144             for (int i = searchTokens.size() - 1; i >= 0; --i) {
145                 final String searchToken = searchTokens.get(i);
146                 boolean found = false;
147                 for (final String pairSide : pairSides) {
148                     found |= pairSide.contains(searchToken);
149                 }
150                 if (!found) {
151                     return RowMatchType.NO_MATCH;
152                 }
153             }
154             for (final String pairSide : pairSides) {
155                 if (orderedMatchPattern.matcher(pairSide).find()) {
156                     return RowMatchType.ORDERED_MATCH;
157                 }
158             }
159             return RowMatchType.BAG_OF_WORDS_MATCH;
160         }
161
162         @Override
163         public int getSideLength(boolean swapPairEntries) {
164             int result = 0;
165             final int side = swapPairEntries ? 1 : 0;
166             for (final Pair pair : getEntry().pairs) {
167                 result += pair.get(side).length();
168             }
169             return result;
170         }
171
172     }
173
174     public String getRawText(final boolean compact) {
175         if (compact) {
176             return this.pairs.get(0).toStringTab();
177         }
178         final StringBuilder builder = new StringBuilder();
179         for (int i = 0; i < this.pairs.size(); ++i) {
180             if (i > 0) {
181                 builder.append(" | ");
182             }
183             builder.append(this.pairs.get(i).lang1);
184         }
185         builder.append("\t");
186         for (int i = 0; i < this.pairs.size(); ++i) {
187             if (i > 0) {
188                 builder.append(" | ");
189             }
190             builder.append(this.pairs.get(i).lang2);
191         }
192         return builder.toString();
193     }
194
195     @Override
196     public int compareTo(final PairEntry that) {
197         return this.getRawText(false).compareTo(that.getRawText(false));
198     }
199
200     @Override
201     public String toString() {
202         return getRawText(false);
203     }
204
205     // -----------------------------------------------------------------------
206
207     public static final class Pair {
208
209         public final String lang1;
210         public final String lang2;
211
212         public Pair(final String lang1, final String lang2) {
213             this.lang1 = lang1;
214             this.lang2 = lang2;
215             if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
216                 System.err.println("poop");
217             }
218             assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
219             assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
220         }
221
222         public Pair(final String lang1, final String lang2, final boolean swap) {
223             this(swap ? lang2 : lang1, swap ? lang1 : lang2);
224         }
225
226         public String toString() {
227             return lang1 + " :: " + lang2;
228         }
229
230         public String toStringTab() {
231             return lang1 + "\t" + lang2;
232         }
233
234         public String get(int i) {
235             if (i == 0) {
236                 return lang1;
237             } else if (i == 1) {
238                 return lang2;
239             }
240             throw new IllegalArgumentException();
241         }
242
243     }
244 }