KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.bind.tuple;
10
11 import com.sleepycat.je.DatabaseEntry;
12
13 /**
14  * A concrete <code>TupleBinding</code> for a <code>Float</code> primitive
15  * wrapper or a <code>float</code> primitive.
16  *
17  * <p><em>Note:</em> This class produces byte array values that by default
18  * (without a custom comparator) do <em>not</em> sort correctly for negative
19  * values. Only non-negative values are sorted correctly by default. To sort
20  * all values correctly by default, use {@link SortedFloatBinding}.</p>
21  *
22  * <p>There are two ways to use this class:</p>
23  * <ol>
24  * <li>When using the {@link com.sleepycat.je} package directly, the static
25  * methods in this class can be used to convert between primitive values and
26  * {@link DatabaseEntry} objects.</li>
27  * <li>When using the {@link com.sleepycat.collections} package, an instance of
28  * this class can be used with any stored collection. The easiest way to
29  * obtain a binding instance is with the {@link
30  * TupleBinding#getPrimitiveBinding} method.</li>
31  * </ol>
32  */

33 public class FloatBinding extends TupleBinding {
34
35     private static final int FLOAT_SIZE = 4;
36
37     // javadoc is inherited
38
public Object JavaDoc entryToObject(TupleInput input) {
39
40         return new Float JavaDoc(input.readFloat());
41     }
42
43     // javadoc is inherited
44
public void objectToEntry(Object JavaDoc object, TupleOutput output) {
45
46         output.writeFloat(((Number JavaDoc) object).floatValue());
47     }
48
49     // javadoc is inherited
50
protected TupleOutput getTupleOutput(Object JavaDoc object) {
51
52         return sizedOutput();
53     }
54
55     /**
56      * Converts an entry buffer into a simple <code>float</code> value.
57      *
58      * @param entry is the source entry buffer.
59      *
60      * @return the resulting value.
61      */

62     public static float entryToFloat(DatabaseEntry entry) {
63
64         return entryToInput(entry).readFloat();
65     }
66
67     /**
68      * Converts a simple <code>float</code> value into an entry buffer.
69      *
70      * @param val is the source value.
71      *
72      * @param entry is the destination entry buffer.
73      */

74     public static void floatToEntry(float val, DatabaseEntry entry) {
75
76         outputToEntry(sizedOutput().writeFloat(val), entry);
77     }
78
79     /**
80      * Returns a tuple output object of the exact size needed, to avoid
81      * wasting space when a single primitive is output.
82      */

83     static TupleOutput sizedOutput() {
84
85         return new TupleOutput(new byte[FLOAT_SIZE]);
86     }
87 }
88
Popular Tags