1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: TupleSerialBinding.java,v 12.7 2008/01/08 20:58:35 bostic Exp $ 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 * @author Mark Hayes 32 */ 33public abstract class TupleSerialBinding extends TupleBase 34 implements EntityBinding { 35 36 protected SerialBinding dataBinding; 37 38 /** 39 * Creates a tuple-serial entity binding. 40 * 41 * @param classCatalog is the catalog to hold shared class information and 42 * for a database should be a {@link StoredClassCatalog}. 43 * 44 * @param baseClass is the base class. 45 */ 46 public TupleSerialBinding(ClassCatalog classCatalog, 47 Class baseClass) { 48 49 this(new SerialBinding(classCatalog, baseClass)); 50 } 51 52 /** 53 * Creates a tuple-serial entity binding. 54 * 55 * @param dataBinding is the data binding. 56 */ 57 public TupleSerialBinding(SerialBinding dataBinding) { 58 59 this.dataBinding = dataBinding; 60 } 61 62 // javadoc is inherited 63 public Object entryToObject(DatabaseEntry key, DatabaseEntry data) { 64 65 return entryToObject(entryToInput(key), 66 dataBinding.entryToObject(data)); 67 } 68 69 // javadoc is inherited 70 public void objectToKey(Object object, DatabaseEntry key) { 71 72 TupleOutput output = getTupleOutput(object); 73 objectToKey(object, output); 74 outputToEntry(output, key); 75 } 76 77 // javadoc is inherited 78 public void objectToData(Object object, DatabaseEntry data) { 79 80 object = objectToData(object); 81 dataBinding.objectToEntry(object, data); 82 } 83 84 /** 85 * Constructs an entity object from {@link TupleInput} key entry and 86 * deserialized data entry objects. 87 * 88 * @param keyInput is the {@link TupleInput} key entry object. 89 * 90 * @param dataInput is the deserialized data entry object. 91 * 92 * @return the entity object constructed from the key and data. 93 */ 94 public abstract Object entryToObject(TupleInput keyInput, 95 Object dataInput); 96 97 /** 98 * Extracts a key tuple from an entity object. 99 * 100 * @param object is the entity object. 101 * 102 * @param keyOutput is the {@link TupleOutput} to which the key should be 103 * written. 104 */ 105 public abstract void objectToKey(Object object, 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} 116