1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
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;
23 import com.hughes.util.raf.RAFSerializable;
24 import com.hughes.util.raf.RAFSerializer;
26 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>, Comparable<PairEntry> {
28 public final List<Pair> pairs;
30 public PairEntry(final EntrySource entrySource) {
32 pairs = new ArrayList<Pair>(1);
35 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
37 this.pairs.add(new Pair(lang1, lang2));
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()));
49 public void write(RandomAccessFile raf) throws IOException {
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);
60 static final class Serializer implements RAFSerializer<PairEntry> {
62 final Dictionary dictionary;
64 Serializer(Dictionary dictionary) {
65 this.dictionary = dictionary;
69 public PairEntry read(RandomAccessFile raf) throws IOException {
70 return new PairEntry(dictionary, raf);
74 public void write(RandomAccessFile raf, PairEntry t) throws IOException {
80 public int addToDictionary(final Dictionary dictionary) {
81 dictionary.pairEntries.add(this);
82 return dictionary.pairEntries.size() - 1;
86 // --------------------------------------------------------------------
89 public static class Row extends RowBase {
91 Row(final RandomAccessFile raf, final int thisRowIndex,
92 final Index index) throws IOException {
93 super(raf, thisRowIndex, index);
96 Row(final int referenceIndex, final int thisRowIndex,
98 super(referenceIndex, thisRowIndex, index);
101 public PairEntry getEntry() {
102 return index.dict.pairEntries.get(referenceIndex);
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));
115 public String getRawText(boolean compact) {
116 final PairEntry pairEntry = getEntry();
117 return pairEntry.getRawText(compact);
122 public String getRawText(final boolean compact) {
124 return this.pairs.get(0).toStringTab();
126 final StringBuilder builder = new StringBuilder();
127 for (int i = 0; i < this.pairs.size(); ++i) {
129 builder.append(" | ");
131 builder.append(this.pairs.get(i).lang1);
133 builder.append("\t");
134 for (int i = 0; i < this.pairs.size(); ++i) {
136 builder.append(" | ");
138 builder.append(this.pairs.get(i).lang2);
140 return builder.toString();
144 public int compareTo(final PairEntry that) {
145 return this.getRawText(false).compareTo(that.getRawText(false));
149 public String toString() {
150 return getRawText(false);
153 // -----------------------------------------------------------------------
155 public static final class Pair {
157 public final String lang1;
158 public final String lang2;
160 public Pair(final String lang1, final String lang2) {
163 if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
164 System.err.println("poop");
166 assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
167 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
170 public Pair(final String lang1, final String lang2, final boolean swap) {
171 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
174 public String toString() {
175 return lang1 + " :: " + lang2;
178 public String toStringTab() {
179 return lang1 + "\t" + lang2;
182 public String get(int i) {
188 throw new IllegalArgumentException();