]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/RowBase.java
UI changes, (experiments), Arabic rendering changes, changing the way
[Dictionary.git] / src / com / hughes / android / dictionary / engine / RowBase.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
21 import com.hughes.util.IndexedObject;
22 import com.hughes.util.raf.RAFListSerializer;
23
24 public abstract class RowBase extends IndexedObject {
25   /**
26    * the Index owning this RowBase.
27    */
28   public final Index index;
29   
30   /**
31    * Where this RowBase points to.
32    */
33   public final int referenceIndex;
34
35   /**
36    * the TokenRow above this RowBase, populated on demand.
37    */
38   private TokenRow tokenRow = null;
39   
40   RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
41     super(thisRowIndex);
42     this.index = index;
43     this.referenceIndex = raf.readInt();  // what this points to.
44   }
45
46   public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
47     super(thisRowIndex);
48     this.index = index;
49     this.referenceIndex = referenceIndex;
50   }
51
52   /**
53    * @return the TokenRow that this row is "filed under".
54    */
55   public TokenRow getTokenRow(final boolean search) {
56     if (tokenRow == null && search) {
57       int r = index() - 1;
58       int rUp = index() + 1;
59       while (r >= 0) {
60         final RowBase row = index.rows.get(r);
61         final TokenRow candidate = row.getTokenRow(false);
62         if (candidate != null) {
63           for (++r; r <= index(); ++r) {
64             index.rows.get(r).setTokenRow(candidate);
65           }
66           break;
67         }
68         if (rUp < index.rows.size()) {
69           final RowBase rowUp = index.rows.get(rUp);
70           TokenRow candidateUp = rowUp.getTokenRow(false);
71           if (candidateUp != null) {
72             // Did we hit the next set of TokenRows?
73             if (candidateUp.index() > this.index()) {  
74               final int tokenIndex = index.sortedIndexEntries.get(candidateUp.referenceIndex - 1).startRow;
75               candidateUp = (TokenRow) index.rows.get(tokenIndex);
76             }
77             for (--rUp; rUp >= index(); --rUp) {
78               index.rows.get(rUp).setTokenRow(candidateUp);
79             }
80             break;
81           }
82           rUp++;
83         }
84         --r;
85       }
86       assert tokenRow != null;
87     }
88     return tokenRow;
89   }
90   
91   public void setTokenRow(TokenRow tokenRow) {
92     assert this.tokenRow == null;
93     assert tokenRow != null;
94     this.tokenRow = tokenRow;
95   }
96
97   public abstract void print(PrintStream out);
98
99   public abstract String getRawText(final boolean compact);
100
101   // RowBase must manage "disk-based" polymorphism.  All other polymorphism is
102   // dealt with in the normal manner.
103   static class Serializer implements RAFListSerializer<RowBase> {
104     
105     final Index index;
106     
107     Serializer(final Index index) {
108       this.index = index;
109     }
110
111     @Override
112     public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
113       final byte rowType = raf.readByte();
114       if (rowType == 0) {
115         return new PairEntry.Row(raf, listIndex, index);
116       } else if (rowType == 1 || rowType == 3) {
117         return new TokenRow(raf, listIndex, index, rowType == 1);
118       } else if (rowType == 2) {
119         return new TextEntry.Row(raf, listIndex, index);
120       }
121       throw new RuntimeException("Invalid rowType:" + rowType);
122     }
123
124     @Override
125     public void write(RandomAccessFile raf, RowBase t) throws IOException {
126       if (t instanceof PairEntry.Row) {
127         raf.writeByte(0);
128       } else if (t instanceof TokenRow) {
129         final TokenRow tokenRow = (TokenRow) t;
130         raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
131       } else if (t instanceof TextEntry.Row) {
132         raf.writeByte(2);
133       }
134       raf.writeInt(t.referenceIndex);
135     }
136   }
137
138 }