1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: TupleTupleMarshalledBinding.java,v 12.8 2008/02/07 17:12:25 mark Exp $ 7 */ 8 9package com.sleepycat.bind.tuple; 10 11import com.sleepycat.util.RuntimeExceptionWrapper; 12 13/** 14 * A concrete <code>TupleTupleBinding</code> that delegates to the 15 * <code>MarshalledTupleEntry</code> and 16 * <code>MarshalledTupleKeyEntity</code> interfaces of the entity class. 17 * 18 * <p>This class calls the methods of the {@link MarshalledTupleEntry} 19 * interface to convert between the data entry and entity object. It calls the 20 * methods of the {@link MarshalledTupleKeyEntity} interface to convert between 21 * the key entry and the entity object. These two interfaces must both be 22 * implemented by the entity class.</p> 23 * 24 * @author Mark Hayes 25 */ 26public class TupleTupleMarshalledBinding extends TupleTupleBinding { 27 28 private Class cls; 29 30 /** 31 * Creates a tuple-tuple marshalled binding object. 32 * 33 * <p>The given class is used to instantiate entity objects using 34 * {@link Class#forName}, and therefore must be a public class and have a 35 * public no-arguments constructor. It must also implement the {@link 36 * MarshalledTupleEntry} and {@link MarshalledTupleKeyEntity} 37 * interfaces.</p> 38 * 39 * @param cls is the class of the entity objects. 40 */ 41 public TupleTupleMarshalledBinding(Class cls) { 42 43 this.cls = cls; 44 45 // The entity class will be used to instantiate the entity object. 46 // 47 if (!MarshalledTupleKeyEntity.class.isAssignableFrom(cls)) { 48 throw new IllegalArgumentException(cls.toString() + 49 " does not implement MarshalledTupleKeyEntity"); 50 } 51 if (!MarshalledTupleEntry.class.isAssignableFrom(cls)) { 52 throw new IllegalArgumentException(cls.toString() + 53 " does not implement MarshalledTupleEntry"); 54 } 55 } 56 57 // javadoc is inherited 58 public Object entryToObject(TupleInput keyInput, TupleInput dataInput) { 59 60 // This "tricky" binding returns the stored data as the entity, but 61 // first it sets the transient key fields from the stored key. 62 MarshalledTupleEntry obj; 63 try { 64 obj = (MarshalledTupleEntry) cls.newInstance(); 65 } catch (IllegalAccessException e) { 66 throw new RuntimeExceptionWrapper(e); 67 } catch (InstantiationException e) { 68 throw new RuntimeExceptionWrapper(e); 69 } 70 if (dataInput != null) { // may be null if used by key extractor 71 obj.unmarshalEntry(dataInput); 72 } 73 MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) obj; 74 if (keyInput != null) { // may be null if used by key extractor 75 entity.unmarshalPrimaryKey(keyInput); 76 } 77 return entity; 78 } 79 80 // javadoc is inherited 81 public void objectToKey(Object object, TupleOutput output) { 82 83 MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) object; 84 entity.marshalPrimaryKey(output); 85 } 86 87 // javadoc is inherited 88 public void objectToData(Object object, TupleOutput output) { 89 90 MarshalledTupleEntry entity = (MarshalledTupleEntry) object; 91 entity.marshalEntry(output); 92 } 93} 94