001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: TupleSerialBinding.java,v 1.24.2.2 2008/01/07 15:14:05 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.serial;
010:
011: import com.sleepycat.bind.EntityBinding;
012: import com.sleepycat.bind.tuple.TupleBase;
013: import com.sleepycat.bind.tuple.TupleInput;
014: import com.sleepycat.bind.tuple.TupleOutput;
015: import com.sleepycat.je.DatabaseEntry;
016:
017: /**
018: * An abstract <code>EntityBinding</code> that treats an entity's key entry as
019: * a tuple and its data entry as a serialized object.
020: *
021: * <p>This class takes care of serializing and deserializing the data entry,
022: * and converting the key entry to/from {@link TupleInput} and {@link
023: * TupleOutput} objects. Its three abstract methods must be implemented by a
024: * concrete subclass to convert these objects to/from an entity object.</p>
025: * <ul>
026: * <li> {@link #entryToObject(TupleInput,Object)} </li>
027: * <li> {@link #objectToKey(Object,TupleOutput)} </li>
028: * <li> {@link #objectToData(Object)} </li>
029: * </ul>
030: *
031: * @author Mark Hayes
032: */
033: public abstract class TupleSerialBinding extends TupleBase implements
034: EntityBinding {
035:
036: protected SerialBinding dataBinding;
037:
038: /**
039: * Creates a tuple-serial entity binding.
040: *
041: * @param classCatalog is the catalog to hold shared class information and
042: * for a database should be a {@link StoredClassCatalog}.
043: *
044: * @param baseClass is the base class.
045: */
046: public TupleSerialBinding(ClassCatalog classCatalog, Class baseClass) {
047:
048: this (new SerialBinding(classCatalog, baseClass));
049: }
050:
051: /**
052: * Creates a tuple-serial entity binding.
053: *
054: * @param dataBinding is the data binding.
055: */
056: public TupleSerialBinding(SerialBinding dataBinding) {
057:
058: this .dataBinding = dataBinding;
059: }
060:
061: // javadoc is inherited
062: public Object entryToObject(DatabaseEntry key, DatabaseEntry data) {
063:
064: return entryToObject(entryToInput(key), dataBinding
065: .entryToObject(data));
066: }
067:
068: // javadoc is inherited
069: public void objectToKey(Object object, DatabaseEntry key) {
070:
071: TupleOutput output = getTupleOutput(object);
072: objectToKey(object, output);
073: outputToEntry(output, key);
074: }
075:
076: // javadoc is inherited
077: public void objectToData(Object object, DatabaseEntry data) {
078:
079: object = objectToData(object);
080: dataBinding.objectToEntry(object, data);
081: }
082:
083: /**
084: * Constructs an entity object from {@link TupleInput} key entry and
085: * deserialized data entry objects.
086: *
087: * @param keyInput is the {@link TupleInput} key entry object.
088: *
089: * @param dataInput is the deserialized data entry object.
090: *
091: * @return the entity object constructed from the key and data.
092: */
093: public abstract Object entryToObject(TupleInput keyInput,
094: Object dataInput);
095:
096: /**
097: * Extracts a key tuple from an entity object.
098: *
099: * @param object is the entity object.
100: *
101: * @param keyOutput is the {@link TupleOutput} to which the key should be
102: * written.
103: */
104: public abstract void objectToKey(Object object,
105: TupleOutput keyOutput);
106:
107: /**
108: * Extracts a data object from an entity object.
109: *
110: * @param object is the entity object.
111: *
112: * @return the deserialized data object.
113: */
114: public abstract Object objectToData(Object object);
115: }
|