00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 2000-2005 00005 * Sleepycat Software. All rights reserved. 00006 * 00007 * $Id: TupleTupleMarshalledBinding.java,v 12.1 2005/01/31 19:27:32 mark Exp $ 00008 */ 00009 00010 package com.sleepycat.bind.tuple; 00011 00012 import com.sleepycat.util.RuntimeExceptionWrapper; 00013 00027 public class TupleTupleMarshalledBinding extends TupleTupleBinding { 00028 00029 private Class cls; 00030 00042 public TupleTupleMarshalledBinding(Class cls) { 00043 00044 this.cls = cls; 00045 00046 // The entity class will be used to instantiate the entity object. 00047 // 00048 if (!MarshalledTupleKeyEntity.class.isAssignableFrom(cls)) { 00049 throw new IllegalArgumentException(cls.toString() + 00050 " does not implement MarshalledTupleKeyEntity"); 00051 } 00052 if (!MarshalledTupleEntry.class.isAssignableFrom(cls)) { 00053 throw new IllegalArgumentException(cls.toString() + 00054 " does not implement MarshalledTupleEntry"); 00055 } 00056 } 00057 00058 // javadoc is inherited 00059 public Object entryToObject(TupleInput keyInput, TupleInput dataInput) { 00060 00061 // This "tricky" binding returns the stored data as the entity, but 00062 // first it sets the transient key fields from the stored key. 00063 MarshalledTupleEntry obj; 00064 try { 00065 obj = (MarshalledTupleEntry) cls.newInstance(); 00066 } catch (IllegalAccessException e) { 00067 throw new RuntimeExceptionWrapper(e); 00068 } catch (InstantiationException e) { 00069 throw new RuntimeExceptionWrapper(e); 00070 } 00071 if (dataInput != null) { // may be null if used by key extractor 00072 obj.unmarshalEntry(dataInput); 00073 } 00074 MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) obj; 00075 if (keyInput != null) { // may be null if used by key extractor 00076 entity.unmarshalPrimaryKey(keyInput); 00077 } 00078 return entity; 00079 } 00080 00081 // javadoc is inherited 00082 public void objectToKey(Object object, TupleOutput output) { 00083 00084 MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) object; 00085 entity.marshalPrimaryKey(output); 00086 } 00087 00088 // javadoc is inherited 00089 public void objectToData(Object object, TupleOutput output) { 00090 00091 MarshalledTupleEntry entity = (MarshalledTupleEntry) object; 00092 entity.marshalEntry(output); 00093 } 00094 }