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.tuple; 10 11import java.util.HashMap; 12import java.util.Map; 13 14import com.sleepycat.bind.EntryBinding; 15import com.sleepycat.db.DatabaseEntry; 16 17/** 18 * An abstract <code>EntryBinding</code> that treats a key or data entry as a 19 * tuple; it includes predefined bindings for Java primitive types. 20 * 21 * <p>This class takes care of converting the entries to/from {@link 22 * TupleInput} and {@link TupleOutput} objects. Its two abstract methods must 23 * be implemented by a concrete subclass to convert between tuples and key or 24 * data objects.</p> 25 * <ul> 26 * <li> {@link #entryToObject(TupleInput)} </li> 27 * <li> {@link #objectToEntry(Object,TupleOutput)} </li> 28 * </ul> 29 * 30 * <p>For key or data entries which are Java primitive classes (String, 31 * Integer, etc) {@link #getPrimitiveBinding} may be used to return a built in 32 * tuple binding. A custom tuple binding for these types is not needed. 33 * <em>Note:</em> {@link #getPrimitiveBinding} returns bindings that do not 34 * sort negative floating point numbers correctly by default. See {@link 35 * SortedFloatBinding} and {@link SortedDoubleBinding} for details.</p> 36 * 37 * <p>When a tuple binding is used as a key binding, it produces key values 38 * with a reasonable default sort order. For more information on the default 39 * sort order, see {@link com.sleepycat.bind.tuple.TupleOutput}.</p> 40 * 41 * @author Mark Hayes 42 */ 43public abstract class TupleBinding<E> 44 extends TupleBase<E> 45 implements EntryBinding<E> { 46 47 private static final Map<Class,TupleBinding> primitives = 48 new HashMap<Class,TupleBinding>(); 49 static { 50 addPrimitive(String.class, String.class, new StringBinding()); 51 addPrimitive(Character.class, Character.TYPE, new CharacterBinding()); 52 addPrimitive(Boolean.class, Boolean.TYPE, new BooleanBinding()); 53 addPrimitive(Byte.class, Byte.TYPE, new ByteBinding()); 54 addPrimitive(Short.class, Short.TYPE, new ShortBinding()); 55 addPrimitive(Integer.class, Integer.TYPE, new IntegerBinding()); 56 addPrimitive(Long.class, Long.TYPE, new LongBinding()); 57 addPrimitive(Float.class, Float.TYPE, new FloatBinding()); 58 addPrimitive(Double.class, Double.TYPE, new DoubleBinding()); 59 } 60 61 private static void addPrimitive(Class cls1, Class cls2, 62 TupleBinding binding) { 63 primitives.put(cls1, binding); 64 primitives.put(cls2, binding); 65 } 66 67 /** 68 * Creates a tuple binding. 69 */ 70 public TupleBinding() { 71 } 72 73 // javadoc is inherited 74 public E entryToObject(DatabaseEntry entry) { 75 76 return entryToObject(entryToInput(entry)); 77 } 78 79 // javadoc is inherited 80 public void objectToEntry(E object, DatabaseEntry entry) { 81 82 TupleOutput output = getTupleOutput(object); 83 objectToEntry(object, output); 84 outputToEntry(output, entry); 85 } 86 87 /** 88 * Constructs a key or data object from a {@link TupleInput} entry. 89 * 90 * @param input is the tuple key or data entry. 91 * 92 * @return the key or data object constructed from the entry. 93 */ 94 public abstract E entryToObject(TupleInput input); 95 96 /** 97 * Converts a key or data object to a tuple entry. 98 * 99 * @param object is the key or data object. 100 * 101 * @param output is the tuple entry to which the key or data should be 102 * written. 103 */ 104 public abstract void objectToEntry(E object, TupleOutput output); 105 106 /** 107 * Creates a tuple binding for a primitive Java class. The following 108 * Java classes are supported. 109 * <ul> 110 * <li><code>String</code></li> 111 * <li><code>Character</code></li> 112 * <li><code>Boolean</code></li> 113 * <li><code>Byte</code></li> 114 * <li><code>Short</code></li> 115 * <li><code>Integer</code></li> 116 * <li><code>Long</code></li> 117 * <li><code>Float</code></li> 118 * <li><code>Double</code></li> 119 * </ul> 120 * 121 * <p><em>Note:</em> {@link #getPrimitiveBinding} returns bindings that do 122 * not sort negative floating point numbers correctly by default. See 123 * {@link SortedFloatBinding} and {@link SortedDoubleBinding} for 124 * details.</p> 125 * 126 * @param cls is the primitive Java class. 127 * 128 * @return a new binding for the primitive class or null if the cls 129 * parameter is not one of the supported classes. 130 */ 131 public static <T> TupleBinding<T> getPrimitiveBinding(Class<T> cls) { 132 133 return primitives.get(cls); 134 } 135} 136