01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: TestEntityBinding.java,v 1.23.2.2 2008/01/07 15:14:24 cwl Exp $
07: */
08:
09: 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 entryToObject(DatabaseEntry key, DatabaseEntry value) {
28:
29: byte keyByte;
30: if (isRecNum) {
31: if (key.getSize() != 4) {
32: throw new IllegalStateException();
33: }
34: keyByte = (byte) RecordNumberBinding
35: .entryToRecordNumber(key);
36: } else {
37: if (key.getSize() != 1) {
38: throw new IllegalStateException();
39: }
40: keyByte = key.getData()[key.getOffset()];
41: }
42: if (value.getSize() != 1) {
43: throw new IllegalStateException();
44: }
45: byte valByte = value.getData()[value.getOffset()];
46: return new TestEntity(keyByte, valByte);
47: }
48:
49: public void objectToKey(Object object, DatabaseEntry key) {
50:
51: byte val = (byte) ((TestEntity) object).key;
52: if (isRecNum) {
53: RecordNumberBinding.recordNumberToEntry(val, key);
54: } else {
55: key.setData(new byte[] { val }, 0, 1);
56: }
57: }
58:
59: public void objectToData(Object object, DatabaseEntry value) {
60:
61: byte val = (byte) ((TestEntity) object).value;
62: value.setData(new byte[] { val }, 0, 1);
63: }
64: }
|