1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 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<E extends MarshalledTupleEntry> 24 extends TupleBinding<E> { 25 26 private Class<E> cls; 27 28 /** 29 * Creates a tuple marshalled binding object. 30 * 31 * <p>The given class is used to instantiate key or data objects using 32 * {@link Class#forName}, and therefore must be a public class and have a 33 * public no-arguments constructor. It must also implement the {@link 34 * MarshalledTupleEntry} interface.</p> 35 * 36 * @param cls is the class of the key or data objects. 37 */ 38 public TupleMarshalledBinding(Class<E> cls) { 39 40 this.cls = cls; 41 42 /* The class will be used to instantiate the object. */ 43 if (!MarshalledTupleEntry.class.isAssignableFrom(cls)) { 44 throw new IllegalArgumentException(cls.toString() + 45 " does not implement MarshalledTupleEntry"); 46 } 47 } 48 49 // javadoc is inherited 50 public E entryToObject(TupleInput input) { 51 52 try { 53 E obj = 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(E object, TupleOutput output) { 65 66 object.marshalEntry(output); 67 } 68} 69