1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: TupleMarshalledBinding.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>TupleBinding</code> that delegates to the 15 * <code>MarshalledTupleEntry</code> interface of the data or key object. 16 * 17 * <p>This class works by calling the methods of the {@link 18 * MarshalledTupleEntry} interface, which must be implemented by the key or 19 * data class, to convert between the key or data entry and the object.</p> 20 * 21 * @author Mark Hayes 22 */ 23public class TupleMarshalledBinding extends TupleBinding { 24 25 private Class cls; 26 27 /** 28 * Creates a tuple marshalled binding object. 29 * 30 * <p>The given class is used to instantiate key or data objects using 31 * {@link Class#forName}, and therefore must be a public class and have a 32 * public no-arguments constructor. It must also implement the {@link 33 * MarshalledTupleEntry} interface.</p> 34 * 35 * @param cls is the class of the key or data objects. 36 */ 37 public TupleMarshalledBinding(Class cls) { 38 39 this.cls = cls; 40 41 /* The class will be used to instantiate the object. */ 42 if (!MarshalledTupleEntry.class.isAssignableFrom(cls)) { 43 throw new IllegalArgumentException(cls.toString() + 44 " does not implement MarshalledTupleEntry"); 45 } 46 } 47 48 // javadoc is inherited 49 public Object entryToObject(TupleInput input) { 50 51 try { 52 MarshalledTupleEntry obj = 53 (MarshalledTupleEntry) cls.newInstance(); 54 obj.unmarshalEntry(input); 55 return obj; 56 } catch (IllegalAccessException e) { 57 throw new RuntimeExceptionWrapper(e); 58 } catch (InstantiationException e) { 59 throw new RuntimeExceptionWrapper(e); 60 } 61 } 62 63 // javadoc is inherited 64 public void objectToEntry(Object object, TupleOutput output) { 65 66 MarshalledTupleEntry obj = (MarshalledTupleEntry) object; 67 obj.marshalEntry(output); 68 } 69} 70