]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/RowBase.java
d62edf16a49ac550df3a7866c52962e124f19ee0
[Dictionary.git] / src / com / hughes / android / dictionary / engine / RowBase.java
1 package com.hughes.android.dictionary.engine;
2
3 import java.io.IOException;
4 import java.io.PrintStream;
5 import java.io.RandomAccessFile;
6
7 import com.hughes.util.IndexedObject;
8 import com.hughes.util.raf.RAFListSerializer;
9
10 public abstract class RowBase extends IndexedObject {
11   /**
12    * the Index owning this RowBase.
13    */
14   final Index index;
15   
16   /**
17    * Where this RowBase points to.
18    */
19   int referenceIndex;
20
21   /**
22    * the TokenRow above this RowBase, populated on demand.
23    */
24   TokenRow tokenRow = null;
25   
26   RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
27     super(thisRowIndex);
28     this.index = index;
29     this.referenceIndex = raf.readInt();  // what this points to.
30   }
31
32   public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
33     super(thisRowIndex);
34     this.index = index;
35     this.referenceIndex = referenceIndex;
36   }
37
38   /**
39    * @return the TokenRow that this row is "filed under".
40    */
41   public TokenRow getTokenRow(final boolean search) {
42     if (tokenRow == null && search) {
43       int r = index() - 1;
44       while (r >= 0) {
45         final RowBase row = index.rows.get(r);
46         final TokenRow candidate = row.getTokenRow(false);
47         if (candidate != null) {
48           for (++r; r <= index(); ++r) {
49             index.rows.get(r).setTokenRow(candidate);
50           }
51           break;
52         }
53         --r;
54       }
55       assert tokenRow != null;
56     }
57     return tokenRow;
58   }
59   
60   public void setTokenRow(TokenRow tokenRow) {
61     assert this.tokenRow == null;
62     assert tokenRow != null;
63     this.tokenRow = tokenRow;
64   }
65
66
67   public abstract void print(PrintStream out);
68   
69   public abstract Object draw(final String searchText);
70
71
72   // RowBase must manage "disk-based" polymorphism.  All other polymorphism is
73   // dealt with in the normal manner.
74   static class Serializer implements RAFListSerializer<RowBase> {
75     
76     final Index index;
77     
78     Serializer(final Index index) {
79       this.index = index;
80     }
81
82     @Override
83     public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
84       final byte rowType = raf.readByte();
85       if (rowType == 0) {
86         return new PairEntry.Row(raf, listIndex, index);
87       } else if (rowType == 1) {
88         return new TokenRow(raf, listIndex, index);
89       }
90       throw new RuntimeException("Invalid rowType:" + rowType);
91     }
92
93     @Override
94     public void write(RandomAccessFile raf, RowBase t) throws IOException {
95       if (t instanceof PairEntry.Row) {
96         raf.writeByte(0);
97       } else if (t instanceof TokenRow) {
98         raf.writeByte(1);
99       }
100       raf.writeInt(t.referenceIndex);
101     }
102   }
103
104
105 }