]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Fixed themes, fixing DictionaryInfo, DictionaryVersion 2. Better
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Dictionary.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
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;
22
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;
28
29
30 public class Dictionary implements RAFSerializable<Dictionary> {
31   
32   static final int CACHE_SIZE = 5000;
33   
34   static final int CURRENT_DICT_VERSION = 2;
35   static final String END_OF_DICTIONARY = "END OF DICTIONARY";
36   
37   // persisted
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;
45   
46   /**
47    * dictFileVersion 1 adds:
48    * <li> links to sources?
49    * 
50    * dictFileVersion 2 adds:
51    * <li> counts of tokens in indices.
52    */
53   
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>();
62   }
63
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);
68     }
69     creationMillis = raf.readLong();
70     dictInfo = raf.readUTF();
71     
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());
76     
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);
83     }
84   }
85   
86   @Override
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);
96   }
97
98   private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
99     @Override
100     public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
101       return new Index(Dictionary.this, raf);
102     }
103     @Override
104     public void write(RandomAccessFile raf, Index t) throws IOException {
105       t.write(raf);
106     }};
107     
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);
112         index.print(out);
113         out.println();
114       }
115     }
116
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());
123       }
124       return result;
125     }
126
127
128 }