01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2000,2008 Oracle. All rights reserved.
05: *
06: * $Id: TupleMarshalledBinding.java,v 1.23.2.3 2008/01/07 15:14:06 cwl Exp $
07: */
08:
09: package com.sleepycat.bind.tuple;
10:
11: import 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: */
23: public 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 = (MarshalledTupleEntry) cls
53: .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: }
|