]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Row.java
go
[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
20   // Row must manage "disk-based" polymorphism.  All other polymorphism is
21   // dealt with in the normal manner.
22   static class Serializer implements RAFListSerializer<Row> {
23     
24     final Dictionary.Index index;
25     
26     Serializer(final Dictionary.Index index) {
27       this.index = index;
28     }
29
30     @Override
31     public Row read(RandomAccessFile raf, final int listIndex) throws IOException {
32       final byte rowType = raf.readByte();
33       if (rowType == 0) {
34         return new PairEntry.Row(raf, listIndex, index);
35       } else if (rowType == 1) {
36         return new TokenRow(raf, listIndex, index);
37       }
38       throw new RuntimeException("Invalid rowType:" + rowType);
39     }
40
41     @Override
42     public void write(RandomAccessFile raf, Row t) throws IOException {
43       if (t instanceof PairEntry.Row) {
44         raf.writeByte(0);
45       } else if (t instanceof TokenRow) {
46         raf.writeByte(1);
47       }
48     }
49   };
50 }