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.CachingList;
24 import com.hughes.util.raf.RAFList;
25 import com.hughes.util.raf.RAFListSerializer;
26 import com.hughes.util.raf.RAFSerializable;
29 public class Dictionary implements RAFSerializable<Dictionary> {
31 static final int CACHE_SIZE = 5000;
33 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
36 final int dictFileVersion;
37 final long creationMillis;
38 public final String dictInfo;
39 public final List<PairEntry> pairEntries;
40 public final List<TextEntry> textEntries;
41 public final List<EntrySource> sources;
42 public final List<Index> indices;
44 public Dictionary(final String dictInfo) {
45 this.dictFileVersion = 0;
46 this.creationMillis = System.currentTimeMillis();
47 this.dictInfo = dictInfo;
48 pairEntries = new ArrayList<PairEntry>();
49 textEntries = new ArrayList<TextEntry>();
50 sources = new ArrayList<EntrySource>();
51 indices = new ArrayList<Index>();
54 public Dictionary(final RandomAccessFile raf) throws IOException {
55 dictFileVersion = raf.readInt();
56 if (dictFileVersion != 0) {
57 throw new IOException("Invalid dictionary version: " + dictFileVersion);
59 creationMillis = raf.readLong();
60 dictInfo = raf.readUTF();
61 sources = CachingList.createFullyCached(RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer()));
62 pairEntries = CachingList.create(RAFList.create(raf, PairEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE);
63 textEntries = CachingList.create(RAFList.create(raf, TextEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE);
64 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
65 final String end = raf.readUTF();
66 if (!end.equals(END_OF_DICTIONARY)) {
67 throw new IOException("Dictionary seems corrupt: " + end);
72 public void write(RandomAccessFile raf) throws IOException {
73 raf.writeInt(dictFileVersion);
74 raf.writeLong(creationMillis);
75 raf.writeUTF(dictInfo);
76 RAFList.write(raf, sources, EntrySource.SERIALIZER);
77 RAFList.write(raf, pairEntries, PairEntry.SERIALIZER);
78 RAFList.write(raf, textEntries, TextEntry.SERIALIZER);
79 RAFList.write(raf, indices, indexSerializer);
80 raf.writeUTF(END_OF_DICTIONARY);
83 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
85 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
86 return new Index(Dictionary.this, raf);
89 public void write(RandomAccessFile raf, Index t) throws IOException {
93 public void print(final PrintStream out) {
94 out.println("dictInfo=" + dictInfo);
95 for (final Index index : indices) {
96 out.printf("Index: %s %s\n", index.shortName, index.longName);