]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/PairEntry.java
Switching to WebView!
[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 java.io.IOException;
18 import java.io.PrintStream;
19 import java.io.RandomAccessFile;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import com.hughes.util.raf.RAFSerializable;
24 import com.hughes.util.raf.RAFSerializer;
25
26 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>, Comparable<PairEntry> {
27   
28   public final List<Pair> pairs;
29
30   public PairEntry(final EntrySource entrySource) {
31     super(entrySource);
32     pairs = new ArrayList<Pair>(1);    
33   }
34
35   public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
36     this(entrySource);
37     this.pairs.add(new Pair(lang1, lang2));
38   }
39   
40   public PairEntry(final Dictionary dictionary, final RandomAccessFile raf) throws IOException {
41     super(dictionary, raf);
42     final int size = raf.readInt();
43     pairs = new ArrayList<PairEntry.Pair>(size);
44     for (int i = 0; i < size; ++i) {
45       pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
46     }
47   }
48   @Override
49   public void write(RandomAccessFile raf) throws IOException {
50     super.write(raf);
51     // TODO: this could be a short.
52     raf.writeInt(pairs.size());
53     for (int i = 0; i < pairs.size(); ++i) {
54       assert pairs.get(i).lang1.length() > 0;
55       raf.writeUTF(pairs.get(i).lang1);
56       raf.writeUTF(pairs.get(i).lang2);
57     }
58   }
59   
60   static final class Serializer implements RAFSerializer<PairEntry> {
61     
62     final Dictionary dictionary;
63     
64     Serializer(Dictionary dictionary) {
65       this.dictionary = dictionary;
66     }
67
68     @Override
69     public PairEntry read(RandomAccessFile raf) throws IOException {
70       return new PairEntry(dictionary, raf);
71     }
72
73     @Override
74     public void write(RandomAccessFile raf, PairEntry t) throws IOException {
75       t.write(raf);
76     }
77   };
78   
79   @Override
80   public int addToDictionary(final Dictionary dictionary) {
81     dictionary.pairEntries.add(this);
82     return dictionary.pairEntries.size() - 1;
83   }
84   
85
86   // --------------------------------------------------------------------
87   
88
89   public static class Row extends RowBase {
90     
91     Row(final RandomAccessFile raf, final int thisRowIndex,
92         final Index index) throws IOException {
93       super(raf, thisRowIndex, index);
94     }
95
96     Row(final int referenceIndex, final int thisRowIndex,
97         final Index index) {
98       super(referenceIndex, thisRowIndex, index);
99     }
100
101     public PairEntry getEntry() {
102       return index.dict.pairEntries.get(referenceIndex);
103     }
104     
105     @Override
106     public void print(PrintStream out) {
107       final PairEntry pairEntry = getEntry();
108       for (int i = 0; i < pairEntry.pairs.size(); ++i) {
109         out.print((i == 0 ? "  " : "    ") + pairEntry.pairs.get(i));
110         out.println();
111       }
112     }
113
114     @Override
115     public String getRawText(boolean compact) {
116       final PairEntry pairEntry = getEntry();
117       return pairEntry.getRawText(compact);
118     }
119   
120   }
121
122   public String getRawText(final boolean compact) {
123     if (compact) {
124       return this.pairs.get(0).toStringTab();
125     }
126     final StringBuilder builder = new StringBuilder();
127     for (int i = 0; i < this.pairs.size(); ++i) {
128       if (i > 0) {
129         builder.append(" | ");
130       }
131       builder.append(this.pairs.get(i).lang1);
132     }
133     builder.append("\t");
134     for (int i = 0; i < this.pairs.size(); ++i) {
135       if (i > 0) {
136         builder.append(" | ");
137       }
138       builder.append(this.pairs.get(i).lang2);
139     }
140     return builder.toString();
141   }
142
143   @Override
144   public int compareTo(final PairEntry that) {
145     return this.getRawText(false).compareTo(that.getRawText(false));
146   }
147   
148   @Override
149   public String toString() {
150     return getRawText(false);
151   }
152
153   // -----------------------------------------------------------------------
154   
155   public static final class Pair {
156     
157     public final String lang1;
158     public final String lang2;
159     
160     public Pair(final String lang1, final String lang2) {
161       this.lang1 = lang1;
162       this.lang2 = lang2;
163       if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
164         System.err.println("poop");
165       }
166       assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
167       assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
168     }
169
170     public Pair(final String lang1, final String lang2, final boolean swap) {
171       this(swap ? lang2 : lang1, swap ? lang1 : lang2);
172     }
173
174     public String toString() {
175       return lang1 + " :: " + lang2;
176     }
177
178     public String toStringTab() {
179       return lang1 + "\t" + lang2;
180     }
181
182     public String get(int i) {
183       if (i == 0) {
184         return lang1;
185       } else if (i == 1) {
186         return lang2;
187       }
188       throw new IllegalArgumentException();
189     }
190
191   }
192 }