1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000,2008 Oracle.  All rights reserved.
5 *
6 * $Id: SerialSerialBinding.java,v 12.6 2008/01/08 20:58:35 bostic Exp $
7 */
8
9package com.sleepycat.bind.serial;
10
11import com.sleepycat.bind.EntityBinding;
12import com.sleepycat.db.DatabaseEntry;
13
14/**
15 * An abstract <code>EntityBinding</code> that treats an entity's key entry and
16 * data entry as serialized objects.
17 *
18 * <p>This class takes care of serializing and deserializing the key and
19 * data entry automatically.  Its three abstract methods must be implemented by
20 * a concrete subclass to convert the deserialized objects to/from an entity
21 * object.</p>
22 * <ul>
23 * <li> {@link #entryToObject(Object,Object)} </li>
24 * <li> {@link #objectToKey(Object)} </li>
25 * <li> {@link #objectToData(Object)} </li>
26 * </ul>
27 *
28 * @author Mark Hayes
29 */
30public abstract class SerialSerialBinding implements EntityBinding {
31
32    private SerialBinding keyBinding;
33    private SerialBinding dataBinding;
34
35    /**
36     * Creates a serial-serial entity binding.
37     *
38     * @param classCatalog is the catalog to hold shared class information and
39     * for a database should be a {@link StoredClassCatalog}.
40     *
41     * @param keyClass is the key base class.
42     *
43     * @param dataClass is the data base class.
44     */
45    public SerialSerialBinding(ClassCatalog classCatalog,
46                               Class keyClass,
47                               Class dataClass) {
48
49        this(new SerialBinding(classCatalog, keyClass),
50             new SerialBinding(classCatalog, dataClass));
51    }
52
53    /**
54     * Creates a serial-serial entity binding.
55     *
56     * @param keyBinding is the key binding.
57     *
58     * @param dataBinding is the data binding.
59     */
60    public SerialSerialBinding(SerialBinding keyBinding,
61                               SerialBinding dataBinding) {
62
63        this.keyBinding = keyBinding;
64        this.dataBinding = dataBinding;
65    }
66
67    // javadoc is inherited
68    public Object entryToObject(DatabaseEntry key, DatabaseEntry data) {
69
70        return entryToObject(keyBinding.entryToObject(key),
71                             dataBinding.entryToObject(data));
72    }
73
74    // javadoc is inherited
75    public void objectToKey(Object object, DatabaseEntry key) {
76
77        object = objectToKey(object);
78        keyBinding.objectToEntry(object, key);
79    }
80
81    // javadoc is inherited
82    public void objectToData(Object object, DatabaseEntry data) {
83
84        object = objectToData(object);
85        dataBinding.objectToEntry(object, data);
86    }
87
88    /**
89     * Constructs an entity object from deserialized key and data objects.
90     *
91     * @param keyInput is the deserialized key object.
92     *
93     * @param dataInput is the deserialized data object.
94     *
95     * @return the entity object constructed from the key and data.
96     */
97    public abstract Object entryToObject(Object keyInput, Object dataInput);
98
99    /**
100     * Extracts a key object from an entity object.
101     *
102     * @param object is the entity object.
103     *
104     * @return the deserialized key object.
105     */
106    public abstract Object objectToKey(Object object);
107
108    /**
109     * Extracts a data object from an entity object.
110     *
111     * @param object is the entity object.
112     *
113     * @return the deserialized data object.
114     */
115    public abstract Object objectToData(Object object);
116}
117