001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: TupleBinding.java,v 1.30.2.2 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.tuple;
010:
011: import java.util.HashMap;
012: import java.util.Map;
013:
014: import com.sleepycat.bind.EntryBinding;
015: import com.sleepycat.je.DatabaseEntry;
016:
017: /**
018: * An abstract <code>EntryBinding</code> that treats a key or data entry as a
019: * tuple; it includes predefined bindings for Java primitive types.
020: *
021: * <p>This class takes care of converting the entries to/from {@link
022: * TupleInput} and {@link TupleOutput} objects. Its two abstract methods must
023: * be implemented by a concrete subclass to convert between tuples and key or
024: * data objects.</p>
025: * <ul>
026: * <li> {@link #entryToObject(TupleInput)} </li>
027: * <li> {@link #objectToEntry(Object,TupleOutput)} </li>
028: * </ul>
029: *
030: * <p>For key or data entries which are Java primitive classes (String,
031: * Integer, etc) {@link #getPrimitiveBinding} may be used to return a builtin
032: * tuple binding. A custom tuple binding for these types is not needed.
033: * <em>Note:</em> {@link #getPrimitiveBinding} returns bindings that do not
034: * sort negative floating point numbers correctly by default. See {@link
035: * SortedFloatBinding} and {@link SortedDoubleBinding} for details.</p>
036: *
037: * <p>When a tuple binding is used as a key binding, it produces key values
038: * with a reasonable default sort order. For more information on the default
039: * sort order, see {@link com.sleepycat.bind.tuple.TupleOutput}.</p>
040: *
041: * @author Mark Hayes
042: */
043: public abstract class TupleBinding extends TupleBase implements
044: EntryBinding {
045:
046: private static final Map primitives = new HashMap();
047: static {
048: addPrimitive(String.class, String.class, new StringBinding());
049: addPrimitive(Character.class, Character.TYPE,
050: new CharacterBinding());
051: addPrimitive(Boolean.class, Boolean.TYPE, new BooleanBinding());
052: addPrimitive(Byte.class, Byte.TYPE, new ByteBinding());
053: addPrimitive(Short.class, Short.TYPE, new ShortBinding());
054: addPrimitive(Integer.class, Integer.TYPE, new IntegerBinding());
055: addPrimitive(Long.class, Long.TYPE, new LongBinding());
056: addPrimitive(Float.class, Float.TYPE, new FloatBinding());
057: addPrimitive(Double.class, Double.TYPE, new DoubleBinding());
058: }
059:
060: private static void addPrimitive(Class cls1, Class cls2,
061: TupleBinding binding) {
062: primitives.put(cls1, binding);
063: primitives.put(cls2, binding);
064: }
065:
066: /**
067: * Creates a tuple binding.
068: */
069: public TupleBinding() {
070: }
071:
072: // javadoc is inherited
073: public Object entryToObject(DatabaseEntry entry) {
074:
075: return entryToObject(entryToInput(entry));
076: }
077:
078: // javadoc is inherited
079: public void objectToEntry(Object object, DatabaseEntry entry) {
080:
081: TupleOutput output = getTupleOutput(object);
082: objectToEntry(object, output);
083: outputToEntry(output, entry);
084: }
085:
086: /**
087: * Constructs a key or data object from a {@link TupleInput} entry.
088: *
089: * @param input is the tuple key or data entry.
090: *
091: * @return the key or data object constructed from the entry.
092: */
093: public abstract Object entryToObject(TupleInput input);
094:
095: /**
096: * Converts a key or data object to a tuple entry.
097: *
098: * @param object is the key or data object.
099: *
100: * @param output is the tuple entry to which the key or data should be
101: * written.
102: */
103: public abstract void objectToEntry(Object object, TupleOutput output);
104:
105: /**
106: * Creates a tuple binding for a primitive Java class. The following
107: * Java classes are supported.
108: * <ul>
109: * <li><code>String</code></li>
110: * <li><code>Character</code></li>
111: * <li><code>Boolean</code></li>
112: * <li><code>Byte</code></li>
113: * <li><code>Short</code></li>
114: * <li><code>Integer</code></li>
115: * <li><code>Long</code></li>
116: * <li><code>Float</code></li>
117: * <li><code>Double</code></li>
118: * </ul>
119: *
120: * <p><em>Note:</em> {@link #getPrimitiveBinding} returns bindings that do
121: * not sort negative floating point numbers correctly by default. See
122: * {@link SortedFloatBinding} and {@link SortedDoubleBinding} for
123: * details.</p>
124: *
125: * @param cls is the primitive Java class.
126: *
127: * @return a new binding for the primitive class or null if the cls
128: * parameter is not one of the supported classes.
129: */
130: public static TupleBinding getPrimitiveBinding(Class cls) {
131:
132: return (TupleBinding) primitives.get(cls);
133: }
134: }
|