1 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 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.entryToRecordNumber(key); 35 } else { 36 if (key.getSize() != 1) { 37 throw new IllegalStateException (); 38 } 39 keyByte = key.getData()[key.getOffset()]; 40 } 41 if (value.getSize() != 1) { 42 throw new IllegalStateException (); 43 } 44 byte valByte = value.getData()[value.getOffset()]; 45 return new TestEntity(keyByte, valByte); 46 } 47 48 public void objectToKey(Object 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 object, DatabaseEntry value) { 59 60 byte val = (byte) ((TestEntity) object).value; 61 value.setData(new byte[] { val }, 0, 1); 62 } 63 } 64 | Popular Tags |