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;
31 pairs = new ArrayList<Pair>(1);
34 public PairEntry(final String lang1, final String lang2) {
35 pairs = new ArrayList<Pair>(1);
36 this.pairs.add(new Pair(lang1, lang2));
39 public PairEntry(final RandomAccessFile raf) throws IOException {
40 final int size = raf.readInt();
41 pairs = new ArrayList<PairEntry.Pair>(size);
42 for (int i = 0; i < size; ++i) {
43 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
47 public void write(RandomAccessFile raf) throws IOException {
48 // TODO: this could be a short.
49 raf.writeInt(pairs.size());
50 for (int i = 0; i < pairs.size(); ++i) {
51 raf.writeUTF(pairs.get(i).lang1);
52 raf.writeUTF(pairs.get(i).lang2);
56 static final RAFSerializer<PairEntry> SERIALIZER = new RAFSerializer<PairEntry>() {
58 public PairEntry read(RandomAccessFile raf) throws IOException {
59 return new PairEntry(raf);
63 public void write(RandomAccessFile raf, PairEntry t) throws IOException {
69 public int addToDictionary(final Dictionary dictionary) {
70 dictionary.pairEntries.add(this);
71 return dictionary.pairEntries.size() - 1;
75 // --------------------------------------------------------------------
78 public static class Row extends RowBase {
80 Row(final RandomAccessFile raf, final int thisRowIndex,
81 final Index index) throws IOException {
82 super(raf, thisRowIndex, index);
85 Row(final int referenceIndex, final int thisRowIndex,
87 super(referenceIndex, thisRowIndex, index);
90 public PairEntry getEntry() {
91 return index.dict.pairEntries.get(referenceIndex);
95 public void print(PrintStream out) {
96 final PairEntry pairEntry = getEntry();
97 for (int i = 0; i < pairEntry.pairs.size(); ++i) {
98 out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
104 public String getRawText(boolean compact) {
105 final PairEntry pairEntry = getEntry();
106 return pairEntry.getRawText(compact);
111 public String getRawText(final boolean compact) {
113 return this.pairs.get(0).toStringTab();
115 final StringBuilder builder = new StringBuilder();
116 for (int i = 0; i < this.pairs.size(); ++i) {
118 builder.append(" | ");
120 builder.append(this.pairs.get(i).lang1);
122 builder.append("\t");
123 for (int i = 0; i < this.pairs.size(); ++i) {
125 builder.append(" | ");
127 builder.append(this.pairs.get(i).lang2);
129 return builder.toString();
133 public int compareTo(final PairEntry that) {
134 return this.getRawText(false).compareTo(that.getRawText(false));
138 public String toString() {
139 return getRawText(false);
142 // -----------------------------------------------------------------------
144 public static final class Pair {
146 public final String lang1;
147 public final String lang2;
149 public Pair(final String lang1, final String lang2) {
152 if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
153 System.err.println("poop");
155 assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
156 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
159 public Pair(final String lang1, final String lang2, final boolean swap) {
160 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
163 public String toString() {
164 return lang1 + " :: " + lang2;
167 public String toStringTab() {
168 return lang1 + "\t" + lang2;
171 public String get(int i) {
177 throw new IllegalArgumentException();