]> 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.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   public final Index index;
15   
16   /**
17    * Where this RowBase points to.
18    */
19   public final int referenceIndex;
20
21   /**
22    * the TokenRow above this RowBase, populated on demand.
23    */
24   private 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       int rUp = index() + 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 <= index(); ++r) {
50             index.rows.get(r).setTokenRow(candidate);
51           }
52           break;
53         }
54         if (rUp < index.rows.size()) {
55           final RowBase rowUp = index.rows.get(rUp);
56           final TokenRow candidateUp = rowUp.getTokenRow(false);
57           if (candidateUp != null) {
58             for (--rUp; rUp >= index(); --rUp) {
59               index.rows.get(rUp).setTokenRow(candidateUp);
60             }
61             break;
62           }
63           rUp++;
64         }
65         --r;
66       }
67       assert tokenRow != null;
68     }
69     return tokenRow;
70   }
71   
72   public void setTokenRow(TokenRow tokenRow) {
73     assert this.tokenRow == null;
74     assert tokenRow != null;
75     this.tokenRow = tokenRow;
76   }
77
78   public abstract void print(PrintStream out);
79
80   public abstract String getRawText(final boolean compact);
81
82   // RowBase must manage "disk-based" polymorphism.  All other polymorphism is
83   // dealt with in the normal manner.
84   static class Serializer implements RAFListSerializer<RowBase> {
85     
86     final Index index;
87     
88     Serializer(final Index index) {
89       this.index = index;
90     }
91
92     @Override
93     public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
94       final byte rowType = raf.readByte();
95       if (rowType == 0) {
96         return new PairEntry.Row(raf, listIndex, index);
97       } else if (rowType == 1) {
98         return new TokenRow(raf, listIndex, index);
99       }
100       throw new RuntimeException("Invalid rowType:" + rowType);
101     }
102
103     @Override
104     public void write(RandomAccessFile raf, RowBase t) throws IOException {
105       if (t instanceof PairEntry.Row) {
106         raf.writeByte(0);
107       } else if (t instanceof TokenRow) {
108         raf.writeByte(1);
109       }
110       raf.writeInt(t.referenceIndex);
111     }
112   }
113
114 }