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