]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Switching to WebView!
[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.util.CachingList;
24 import com.hughes.util.raf.RAFList;
25 import com.hughes.util.raf.RAFListSerializer;
26 import com.hughes.util.raf.RAFSerializable;
27
28
29 public class Dictionary implements RAFSerializable<Dictionary> {
30   
31   static final int CACHE_SIZE = 5000;
32   
33   static final String END_OF_DICTIONARY = "END OF DICTIONARY";
34   
35   // persisted
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;
43   
44   /**
45    * dictFileVersion 1 adds:
46    * <li> counts of tokens in indices.
47    * <li> links to sources?
48    */
49   
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>();
58   }
59
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);
64     }
65     creationMillis = raf.readLong();
66     dictInfo = raf.readUTF();
67     
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());
72     
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);
79     }
80   }
81   
82   @Override
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);
92   }
93
94   private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
95     @Override
96     public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
97       return new Index(Dictionary.this, raf);
98     }
99     @Override
100     public void write(RandomAccessFile raf, Index t) throws IOException {
101       t.write(raf);
102     }};
103     
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);
108         index.print(out);
109         out.println();
110       }
111     }
112
113
114 }