File: TupleTupleMarshalledBinding.java

package info (click to toggle)
db 5.1.29-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 150,348 kB
  • sloc: ansic: 400,169; java: 94,399; tcl: 70,967; sh: 37,399; cs: 30,758; cpp: 21,132; perl: 14,227; xml: 9,854; makefile: 3,777; yacc: 1,003; awk: 942; sql: 801; erlang: 461; python: 216; php: 24; asm: 14
file content (94 lines) | stat: -rw-r--r-- 3,131 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2000, 2011 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package com.sleepycat.bind.tuple;

import com.sleepycat.util.RuntimeExceptionWrapper;

/**
 * A concrete <code>TupleTupleBinding</code> that delegates to the
 * <code>MarshalledTupleEntry</code> and
 * <code>MarshalledTupleKeyEntity</code> interfaces of the entity class.
 *
 * <p>This class calls the methods of the {@link MarshalledTupleEntry}
 * interface to convert between the data entry and entity object.  It calls the
 * methods of the {@link MarshalledTupleKeyEntity} interface to convert between
 * the key entry and the entity object.  These two interfaces must both be
 * implemented by the entity class.</p>
 *
 * @author Mark Hayes
 */
public class TupleTupleMarshalledBinding<E extends
    MarshalledTupleEntry & MarshalledTupleKeyEntity>
    extends TupleTupleBinding<E> {

    private Class<E> cls;

    /**
     * Creates a tuple-tuple marshalled binding object.
     *
     * <p>The given class is used to instantiate entity objects using
     * {@link Class#forName}, and therefore must be a public class and have a
     * public no-arguments constructor.  It must also implement the {@link
     * MarshalledTupleEntry} and {@link MarshalledTupleKeyEntity}
     * interfaces.</p>
     *
     * @param cls is the class of the entity objects.
     */
    public TupleTupleMarshalledBinding(Class<E> cls) {

        this.cls = cls;

        // The entity class will be used to instantiate the entity object.
        //
        if (!MarshalledTupleKeyEntity.class.isAssignableFrom(cls)) {
            throw new IllegalArgumentException
                (cls.toString() +
                 " does not implement MarshalledTupleKeyEntity");
        }
        if (!MarshalledTupleEntry.class.isAssignableFrom(cls)) {
            throw new IllegalArgumentException
                (cls.toString() + " does not implement MarshalledTupleEntry");
        }
    }

    // javadoc is inherited
    public E entryToObject(TupleInput keyInput, TupleInput dataInput) {

        /*
         * This "tricky" binding returns the stored data as the entity, but
         * first it sets the transient key fields from the stored key.
         */
        E obj;
        try {
            obj = cls.newInstance();
        } catch (IllegalAccessException e) {
            throw RuntimeExceptionWrapper.wrapIfNeeded(e);
        } catch (InstantiationException e) {
            throw RuntimeExceptionWrapper.wrapIfNeeded(e);
        }
        if (dataInput != null) { // may be null if used by key extractor
            obj.unmarshalEntry(dataInput);
        }
        if (keyInput != null) { // may be null if used by key extractor
            obj.unmarshalPrimaryKey(keyInput);
        }
        return obj;
    }

    // javadoc is inherited
    public void objectToKey(E object, TupleOutput output) {

        object.marshalPrimaryKey(output);
    }

    // javadoc is inherited
    public void objectToData(E object, TupleOutput output) {

        object.marshalEntry(output);
    }
}