KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: DoubleBinding.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>Double</code> primitive
15  * wrapper or a <code>double</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 SortedDoubleBinding}.</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 DoubleBinding extends TupleBinding {
34
35     private static final int DOUBLE_SIZE = 8;
36
37     // javadoc is inherited
38
public Object JavaDoc entryToObject(TupleInput input) {
39
40         return new Double JavaDoc(input.readDouble());
41     }
42
43     // javadoc is inherited
44
public void objectToEntry(Object JavaDoc object, TupleOutput output) {
45
46         output.writeDouble(((Number JavaDoc) object).doubleValue());
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>double</code> value.
57      *
58      * @param entry is the source entry buffer.
59      *
60      * @return the resulting value.
61      */

62     public static double entryToDouble(DatabaseEntry entry) {
63
64         return entryToInput(entry).readDouble();
65     }
66
67     /**
68      * Converts a simple <code>double</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 doubleToEntry(double val, DatabaseEntry entry) {
75
76         outputToEntry(sizedOutput().writeDouble(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[DOUBLE_SIZE]);
86     }
87 }
88
Popular Tags