Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TestEntityBinding.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TestEntityBinding.java,v 12.1 2005/01/31 19:27:35 mark Exp $
00008  */
00009 
00010 package com.sleepycat.collections.test;
00011 
00012 import com.sleepycat.bind.EntityBinding;
00013 import com.sleepycat.bind.RecordNumberBinding;
00014 import com.sleepycat.db.DatabaseEntry;
00015 
00019 class TestEntityBinding implements EntityBinding {
00020 
00021     private boolean isRecNum;
00022 
00023     TestEntityBinding(boolean isRecNum) {
00024 
00025         this.isRecNum = isRecNum;
00026     }
00027 
00028     public Object entryToObject(DatabaseEntry key, DatabaseEntry value) {
00029 
00030         byte keyByte;
00031         if (isRecNum) {
00032             if (key.getSize() != 4) {
00033                 throw new IllegalStateException();
00034             }
00035             keyByte = (byte) RecordNumberBinding.entryToRecordNumber(key);
00036         } else {
00037             if (key.getSize() != 1) {
00038                 throw new IllegalStateException();
00039             }
00040             keyByte = key.getData()[key.getOffset()];
00041         }
00042         if (value.getSize() != 1) {
00043             throw new IllegalStateException();
00044         }
00045         byte valByte = value.getData()[value.getOffset()];
00046         return new TestEntity(keyByte, valByte);
00047     }
00048 
00049     public void objectToKey(Object object, DatabaseEntry key) {
00050 
00051         byte val = (byte) ((TestEntity) object).key;
00052         if (isRecNum) {
00053             RecordNumberBinding.recordNumberToEntry(val, key);
00054         } else {
00055             key.setData(new byte[] { val }, 0, 1);
00056         }
00057     }
00058 
00059     public void objectToData(Object object, DatabaseEntry value) {
00060 
00061         byte val = (byte) ((TestEntity) object).value;
00062         value.setData(new byte[] { val }, 0, 1);
00063     }
00064 }

Generated on Sun Dec 25 12:14:55 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2