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