KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: LongBinding.java,v 1.10 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>Long</code> primitive
15  * wrapper or a <code>long</code> primitive.
16  *
17  * <p>There are two ways to use this class:</p>
18  * <ol>
19  * <li>When using the {@link com.sleepycat.je} package directly, the static
20  * methods in this class can be used to convert between primitive values and
21  * {@link DatabaseEntry} objects.</li>
22  * <li>When using the {@link com.sleepycat.collections} package, an instance of
23  * this class can be used with any stored collection. The easiest way to
24  * obtain a binding instance is with the {@link
25  * TupleBinding#getPrimitiveBinding} method.</li>
26  * </ol>
27  */

28 public class LongBinding extends TupleBinding {
29
30     private static final int LONG_SIZE = 8;
31
32     // javadoc is inherited
33
public Object JavaDoc entryToObject(TupleInput input) {
34
35         return new Long JavaDoc(input.readLong());
36     }
37
38     // javadoc is inherited
39
public void objectToEntry(Object JavaDoc object, TupleOutput output) {
40
41         output.writeLong(((Number JavaDoc) object).longValue());
42     }
43
44     // javadoc is inherited
45
protected TupleOutput getTupleOutput(Object JavaDoc object) {
46
47         return sizedOutput();
48     }
49
50     /**
51      * Converts an entry buffer into a simple <code>long</code> value.
52      *
53      * @param entry is the source entry buffer.
54      *
55      * @return the resulting value.
56      */

57     public static long entryToLong(DatabaseEntry entry) {
58
59         return entryToInput(entry).readLong();
60     }
61
62     /**
63      * Converts a simple <code>long</code> value into an entry buffer.
64      *
65      * @param val is the source value.
66      *
67      * @param entry is the destination entry buffer.
68      */

69     public static void longToEntry(long val, DatabaseEntry entry) {
70
71         outputToEntry(sizedOutput().writeLong(val), entry);
72     }
73
74     /**
75      * Returns a tuple output object of the exact size needed, to avoid
76      * wasting space when a single primitive is output.
77      */

78     private static TupleOutput sizedOutput() {
79
80         return new TupleOutput(new byte[LONG_SIZE]);
81     }
82 }
83
Popular Tags