KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > tuple > TupleMarshalledBinding


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: TupleMarshalledBinding.java,v 1.23 2006/10/30 21:14:08 bostic Exp $
7  */

8
9 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 JavaDoc 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 JavaDoc 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 JavaDoc(cls.toString() +
44                         " does not implement MarshalledTupleEntry");
45         }
46     }
47
48     // javadoc is inherited
49
public Object JavaDoc entryToObject(TupleInput input) {
50
51         try {
52             MarshalledTupleEntry obj =
53                 (MarshalledTupleEntry) cls.newInstance();
54             obj.unmarshalEntry(input);
55             return obj;
56         } catch (IllegalAccessException JavaDoc e) {
57             throw new RuntimeExceptionWrapper(e);
58         } catch (InstantiationException JavaDoc e) {
59             throw new RuntimeExceptionWrapper(e);
60         }
61     }
62
63     // javadoc is inherited
64
public void objectToEntry(Object JavaDoc object, TupleOutput output) {
65
66         MarshalledTupleEntry obj = (MarshalledTupleEntry) object;
67         obj.marshalEntry(output);
68     }
69 }
70
Popular Tags