]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Row.java
fd10d5b3ceba43d168647e52afa3fa88b8037edb
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Row.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 interface Row {
9   
10   public void write(RandomAccessFile raf) throws IOException;
11   
12   /**
13    * @return the TokenRow that this row is "filed under".
14    */
15   public TokenRow getTokenRow(final boolean search);
16   
17   public void setTokenRow(final TokenRow tokenRow);
18   
19   public Object draw(final String searchText);
20
21
22   // Row must manage "disk-based" polymorphism.  All other polymorphism is
23   // dealt with in the normal manner.
24   static class Serializer implements RAFListSerializer<Row> {
25     
26     final Index index;
27     
28     Serializer(final Index index) {
29       this.index = index;
30     }
31
32     @Override
33     public Row read(RandomAccessFile raf, final int listIndex) throws IOException {
34       final byte rowType = raf.readByte();
35       if (rowType == 0) {
36         return new PairEntry.Row(raf, listIndex, index);
37       } else if (rowType == 1) {
38         return new TokenRow(raf, listIndex, index);
39       }
40       throw new RuntimeException("Invalid rowType:" + rowType);
41     }
42
43     @Override
44     public void write(RandomAccessFile raf, Row t) throws IOException {
45       if (t instanceof PairEntry.Row) {
46         raf.writeByte(0);
47       } else if (t instanceof TokenRow) {
48         raf.writeByte(1);
49       }
50       t.write(raf);
51     }
52   };
53 }