1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.PrintStream;
5 import java.io.RandomAccessFile;
6 import java.util.ArrayList;
9 import com.hughes.util.CachingList;
10 import com.hughes.util.raf.RAFList;
11 import com.hughes.util.raf.RAFListSerializer;
12 import com.hughes.util.raf.RAFSerializable;
15 public class Dictionary implements RAFSerializable<Dictionary> {
18 final String dictInfo;
19 final List<PairEntry> pairEntries;
20 final List<TextEntry> textEntries;
21 final List<EntrySource> sources;
22 final List<Index> indices;
24 public Dictionary(final String dictInfo) {
25 this.dictInfo = dictInfo;
26 pairEntries = new ArrayList<PairEntry>();
27 textEntries = new ArrayList<TextEntry>();
28 sources = new ArrayList<EntrySource>();
29 indices = new ArrayList<Index>();
32 public Dictionary(final RandomAccessFile raf) throws IOException {
33 dictInfo = raf.readUTF();
35 sources = RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer());
38 pairEntries = RAFList.create(raf, PairEntry.SERIALIZER, raf.getFilePointer());
41 textEntries = RAFList.create(raf, TextEntry.SERIALIZER, raf.getFilePointer());
43 final List<Index> rawIndices = RAFList.create(raf, indexSerializer, raf.getFilePointer());
44 indices = CachingList.create(rawIndices, rawIndices.size());
47 public void print(final PrintStream out) {
48 out.println("dictInfo=" + dictInfo);
49 for (final Index index : indices) {
56 public void write(RandomAccessFile raf) throws IOException {
57 raf.writeUTF(dictInfo);
58 RAFList.write(raf, sources, EntrySource.SERIALIZER);
59 RAFList.write(raf, pairEntries, PairEntry.SERIALIZER);
60 RAFList.write(raf, textEntries, TextEntry.SERIALIZER);
61 RAFList.write(raf, indices, indexSerializer);
64 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
66 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
67 return new Index(Dictionary.this, raf);
70 public void write(RandomAccessFile raf, Index t) throws IOException {