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.android.dictionary.DictionaryInfo;
24 import com.hughes.util.CachingList;
25 import com.hughes.util.raf.RAFList;
26 import com.hughes.util.raf.RAFListSerializer;
27 import com.hughes.util.raf.RAFSerializable;
30 public class Dictionary implements RAFSerializable<Dictionary> {
32 static final int CACHE_SIZE = 5000;
34 static final int CURRENT_DICT_VERSION = 2;
35 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
38 final int dictFileVersion;
39 final long creationMillis;
40 public final String dictInfo;
41 public final List<PairEntry> pairEntries;
42 public final List<TextEntry> textEntries;
43 public final List<EntrySource> sources;
44 public final List<Index> indices;
47 * dictFileVersion 1 adds:
48 * <li> links to sources?
50 * dictFileVersion 2 adds:
51 * <li> counts of tokens in indices.
54 public Dictionary(final String dictInfo) {
55 this.dictFileVersion = CURRENT_DICT_VERSION;
56 this.creationMillis = System.currentTimeMillis();
57 this.dictInfo = dictInfo;
58 pairEntries = new ArrayList<PairEntry>();
59 textEntries = new ArrayList<TextEntry>();
60 sources = new ArrayList<EntrySource>();
61 indices = new ArrayList<Index>();
64 public Dictionary(final RandomAccessFile raf) throws IOException {
65 dictFileVersion = raf.readInt();
66 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
67 throw new IOException("Invalid dictionary version: " + dictFileVersion);
69 creationMillis = raf.readLong();
70 dictInfo = raf.readUTF();
72 // Load the sources, then seek past them, because reading them later disrupts the offset.
73 final RAFList<EntrySource> rafSources = RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer());
74 sources = new ArrayList<EntrySource>(rafSources);
75 raf.seek(rafSources.getEndOffset());
77 pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
78 textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
79 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
80 final String end = raf.readUTF();
81 if (!end.equals(END_OF_DICTIONARY)) {
82 throw new IOException("Dictionary seems corrupt: " + end);
87 public void write(RandomAccessFile raf) throws IOException {
88 raf.writeInt(dictFileVersion);
89 raf.writeLong(creationMillis);
90 raf.writeUTF(dictInfo);
91 RAFList.write(raf, sources, EntrySource.SERIALIZER);
92 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
93 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
94 RAFList.write(raf, indices, indexSerializer);
95 raf.writeUTF(END_OF_DICTIONARY);
98 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
100 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
101 return new Index(Dictionary.this, raf);
104 public void write(RandomAccessFile raf, Index t) throws IOException {
108 public void print(final PrintStream out) {
109 out.println("dictInfo=" + dictInfo);
110 for (final Index index : indices) {
111 out.printf("Index: %s %s\n", index.shortName, index.longName);
117 public DictionaryInfo getDictionaryInfo() {
118 final DictionaryInfo result = new DictionaryInfo();
119 result.creationMillis = this.creationMillis;
120 result.dictInfo = this.dictInfo;
121 for (final Index index : indices) {
122 result.indexInfos.add(index.getIndexInfo());