01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2000,2008 Oracle. All rights reserved.
05: *
06: * $Id: TupleTupleMarshalledBinding.java,v 1.22.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>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: */
26: public 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,
59: TupleInput dataInput) {
60:
61: // This "tricky" binding returns the stored data as the entity, but
62: // first it sets the transient key fields from the stored key.
63: MarshalledTupleEntry obj;
64: try {
65: obj = (MarshalledTupleEntry) cls.newInstance();
66: } catch (IllegalAccessException e) {
67: throw new RuntimeExceptionWrapper(e);
68: } catch (InstantiationException e) {
69: throw new RuntimeExceptionWrapper(e);
70: }
71: if (dataInput != null) { // may be null if used by key extractor
72: obj.unmarshalEntry(dataInput);
73: }
74: MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) obj;
75: if (keyInput != null) { // may be null if used by key extractor
76: entity.unmarshalPrimaryKey(keyInput);
77: }
78: return entity;
79: }
80:
81: // javadoc is inherited
82: public void objectToKey(Object object, TupleOutput output) {
83:
84: MarshalledTupleKeyEntity entity = (MarshalledTupleKeyEntity) object;
85: entity.marshalPrimaryKey(output);
86: }
87:
88: // javadoc is inherited
89: public void objectToData(Object object, TupleOutput output) {
90:
91: MarshalledTupleEntry entity = (MarshalledTupleEntry) object;
92: entity.marshalEntry(output);
93: }
94: }
|