<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2000-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

package com.sleepycat.bind.tuple;

import com.sleepycat.util.RuntimeExceptionWrapper;

/**
 * A concrete &lt;code&gt;TupleBinding&lt;/code&gt; that delegates to the
 * &lt;code&gt;MarshalledTupleEntry&lt;/code&gt; interface of the data or key object.
 *
 * &lt;p&gt;This class works by calling the methods of the {@link
 * MarshalledTupleEntry} interface, which must be implemented by the key or
 * data class, to convert between the key or data entry and the object.&lt;/p&gt;
 *
 * @author Mark Hayes
 */
public class TupleMarshalledBinding&lt;E extends MarshalledTupleEntry&gt;
    extends TupleBinding&lt;E&gt; {

    private Class&lt;E&gt; cls;

    /**
     * Creates a tuple marshalled binding object.
     *
     * &lt;p&gt;The given class is used to instantiate key or data 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} interface.&lt;/p&gt;
     *
     * @param cls is the class of the key or data objects.
     */
    public TupleMarshalledBinding(Class&lt;E&gt; cls) {

        this.cls = cls;

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

    // javadoc is inherited
    public E entryToObject(TupleInput input) {

        try {
            E obj = cls.newInstance();
            obj.unmarshalEntry(input);
            return obj;
        } catch (IllegalAccessException e) {
            throw new RuntimeExceptionWrapper(e);
        } catch (InstantiationException e) {
            throw new RuntimeExceptionWrapper(e);
        }
    }

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

        object.marshalEntry(output);
    }
}
</pre></body></html>