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;
45 * dictFileVersion 1 adds:
46 * <li> counts of tokens in indices.
47 * <li> links to sources?
50 public Dictionary(final String dictInfo) {
51 this.dictFileVersion = 1;
52 this.creationMillis = System.currentTimeMillis();
53 this.dictInfo = dictInfo;
54 pairEntries = new ArrayList<PairEntry>();
55 textEntries = new ArrayList<TextEntry>();
56 sources = new ArrayList<EntrySource>();
57 indices = new ArrayList<Index>();
60 public Dictionary(final RandomAccessFile raf) throws IOException {
61 dictFileVersion = raf.readInt();
62 if (dictFileVersion < 0 || dictFileVersion > 1) {
63 throw new IOException("Invalid dictionary version: " + dictFileVersion);
65 creationMillis = raf.readLong();
66 dictInfo = raf.readUTF();
68 // Load the sources, then seek past them, because reading them later disrupts the offset.
69 final RAFList<EntrySource> rafSources = RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer());
70 sources = new ArrayList<EntrySource>(rafSources);
71 raf.seek(rafSources.getEndOffset());
73 pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
74 textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
75 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
76 final String end = raf.readUTF();
77 if (!end.equals(END_OF_DICTIONARY)) {
78 throw new IOException("Dictionary seems corrupt: " + end);
83 public void write(RandomAccessFile raf) throws IOException {
84 raf.writeInt(dictFileVersion);
85 raf.writeLong(creationMillis);
86 raf.writeUTF(dictInfo);
87 RAFList.write(raf, sources, EntrySource.SERIALIZER);
88 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
89 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
90 RAFList.write(raf, indices, indexSerializer);
91 raf.writeUTF(END_OF_DICTIONARY);
94 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
96 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
97 return new Index(Dictionary.this, raf);
100 public void write(RandomAccessFile raf, Index t) throws IOException {
104 public void print(final PrintStream out) {
105 out.println("dictInfo=" + dictInfo);
106 for (final Index index : indices) {
107 out.printf("Index: %s %s\n", index.shortName, index.longName);