]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/RowWithIndex.java
go
[Dictionary.git] / src / com / hughes / android / dictionary / engine / RowWithIndex.java
1 package com.hughes.android.dictionary.engine;
2
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5
6 public abstract class RowWithIndex implements Row {
7   final Dictionary.Index index;
8   int thisRowIndex;
9   int referenceIndex;
10
11   TokenRow tokenRow = null;
12   
13   RowWithIndex(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException {
14     this.index = index;
15     this.thisRowIndex = thisRowIndex;
16     this.referenceIndex = raf.readInt();
17   }
18
19   @Override
20   public void write(RandomAccessFile raf) throws IOException {
21     raf.writeInt(referenceIndex);
22   }
23   
24   @Override
25   public TokenRow getTokenRow(final boolean search) {
26     if (tokenRow == null && search) {
27       int r = thisRowIndex - 1;
28       while (r >= 0) {
29         final Row row = index.rows.get(r);
30         final TokenRow candidate = row.getTokenRow(false);
31         if (candidate != null) {
32           for (++r; r <= thisRowIndex; ++r) {
33             index.rows.get(r).setTokenRow(candidate);
34           }
35         }
36       }
37       assert tokenRow != null;
38     }
39     return tokenRow;
40   }
41   
42   @Override
43   public void setTokenRow(TokenRow tokenRow) {
44     assert this.tokenRow == null;
45     assert tokenRow != null;
46     this.tokenRow = tokenRow;
47   }
48
49
50 }