KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > bind > tuple > TupleBinding


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: TupleBinding.java,v 1.30 2006/10/30 21:14:08 bostic Exp $
7  */

8
9 package com.sleepycat.bind.tuple;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import com.sleepycat.bind.EntryBinding;
15 import com.sleepycat.je.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 builtin
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  */

43 public abstract class TupleBinding extends TupleBase implements EntryBinding {
44
45     private static final Map JavaDoc primitives = new HashMap JavaDoc();
46     static {
47         addPrimitive(String JavaDoc.class, String JavaDoc.class, new StringBinding());
48         addPrimitive(Character JavaDoc.class, Character.TYPE, new CharacterBinding());
49         addPrimitive(Boolean JavaDoc.class, Boolean.TYPE, new BooleanBinding());
50         addPrimitive(Byte JavaDoc.class, Byte.TYPE, new ByteBinding());
51         addPrimitive(Short JavaDoc.class, Short.TYPE, new ShortBinding());
52         addPrimitive(Integer JavaDoc.class, Integer.TYPE, new IntegerBinding());
53         addPrimitive(Long JavaDoc.class, Long.TYPE, new LongBinding());
54         addPrimitive(Float JavaDoc.class, Float.TYPE, new FloatBinding());
55         addPrimitive(Double JavaDoc.class, Double.TYPE, new DoubleBinding());
56     }
57
58     private static void addPrimitive(Class JavaDoc cls1, Class JavaDoc cls2,
59                                      TupleBinding binding) {
60         primitives.put(cls1, binding);
61         primitives.put(cls2, binding);
62     }
63
64     /**
65      * Creates a tuple binding.
66      */

67     public TupleBinding() {
68     }
69
70     // javadoc is inherited
71
public Object JavaDoc entryToObject(DatabaseEntry entry) {
72
73         return entryToObject(entryToInput(entry));
74     }
75
76     // javadoc is inherited
77
public void objectToEntry(Object JavaDoc object, DatabaseEntry entry) {
78
79         TupleOutput output = getTupleOutput(object);
80         objectToEntry(object, output);
81         outputToEntry(output, entry);
82     }
83
84     /**
85      * Constructs a key or data object from a {@link TupleInput} entry.
86      *
87      * @param input is the tuple key or data entry.
88      *
89      * @return the key or data object constructed from the entry.
90      */

91     public abstract Object JavaDoc entryToObject(TupleInput input);
92
93     /**
94      * Converts a key or data object to a tuple entry.
95      *
96      * @param object is the key or data object.
97      *
98      * @param output is the tuple entry to which the key or data should be
99      * written.
100      */

101     public abstract void objectToEntry(Object JavaDoc object, TupleOutput output);
102
103     /**
104      * Creates a tuple binding for a primitive Java class. The following
105      * Java classes are supported.
106      * <ul>
107      * <li><code>String</code></li>
108      * <li><code>Character</code></li>
109      * <li><code>Boolean</code></li>
110      * <li><code>Byte</code></li>
111      * <li><code>Short</code></li>
112      * <li><code>Integer</code></li>
113      * <li><code>Long</code></li>
114      * <li><code>Float</code></li>
115      * <li><code>Double</code></li>
116      * </ul>
117      *
118      * <p><em>Note:</em> {@link #getPrimitiveBinding} returns bindings that do
119      * not sort negative floating point numbers correctly by default. See
120      * {@link SortedFloatBinding} and {@link SortedDoubleBinding} for
121      * details.</p>
122      *
123      * @param cls is the primitive Java class.
124      *
125      * @return a new binding for the primitive class or null if the cls
126      * parameter is not one of the supported classes.
127      */

128     public static TupleBinding getPrimitiveBinding(Class JavaDoc cls) {
129
130         return (TupleBinding) primitives.get(cls);
131     }
132 }
133
Popular Tags