KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > TestEntityBinding


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: TestEntityBinding.java,v 1.23 2006/10/30 21:14:39 bostic Exp $
7  */

8
9 package com.sleepycat.collections.test;
10
11 import com.sleepycat.bind.EntityBinding;
12 import com.sleepycat.bind.RecordNumberBinding;
13 import com.sleepycat.je.DatabaseEntry;
14
15 /**
16  * @author Mark Hayes
17  */

18 class TestEntityBinding implements EntityBinding {
19
20     private boolean isRecNum;
21
22     TestEntityBinding(boolean isRecNum) {
23
24         this.isRecNum = isRecNum;
25     }
26
27     public Object JavaDoc entryToObject(DatabaseEntry key, DatabaseEntry value) {
28
29         byte keyByte;
30         if (isRecNum) {
31             if (key.getSize() != 4) {
32                 throw new IllegalStateException JavaDoc();
33             }
34             keyByte = (byte) RecordNumberBinding.entryToRecordNumber(key);
35         } else {
36             if (key.getSize() != 1) {
37                 throw new IllegalStateException JavaDoc();
38             }
39             keyByte = key.getData()[key.getOffset()];
40         }
41         if (value.getSize() != 1) {
42             throw new IllegalStateException JavaDoc();
43         }
44         byte valByte = value.getData()[value.getOffset()];
45         return new TestEntity(keyByte, valByte);
46     }
47
48     public void objectToKey(Object JavaDoc object, DatabaseEntry key) {
49
50         byte val = (byte) ((TestEntity) object).key;
51         if (isRecNum) {
52             RecordNumberBinding.recordNumberToEntry(val, key);
53         } else {
54             key.setData(new byte[] { val }, 0, 1);
55         }
56     }
57
58     public void objectToData(Object JavaDoc object, DatabaseEntry value) {
59
60         byte val = (byte) ((TestEntity) object).value;
61         value.setData(new byte[] { val }, 0, 1);
62     }
63 }
64
Popular Tags