Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / java / src / com / sleepycat / bind / tuple / BigDecimalBinding.java
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7
8 package com.sleepycat.bind.tuple;
9
10 import java.math.BigDecimal;
11
12 import com.sleepycat.db.DatabaseEntry;
13
14 /**
15  * A concrete <code>TupleBinding</code> for an unsorted <code>BigDecimal</code>
16  * value.
17  *
18  * <p>There are two ways to use this class:</p>
19  * <ol>
20  * <li>When using the {@link com.sleepycat.db} package directly, the static
21  * methods in this class can be used to convert between primitive values and
22  * {@link DatabaseEntry} objects.</li>
23  * <li>When using the {@link com.sleepycat.collections} package, an instance of
24  * this class can be used with any stored collection.</li>
25  * </ol>
26  *
27  * @see <a href="package-summary.html#bigDecimalFormats">BigDecimal Formats</a>
28  */
29 public class BigDecimalBinding extends TupleBinding<BigDecimal> {
30
31     // javadoc is inherited
32     public BigDecimal entryToObject(TupleInput input) {
33
34         return input.readBigDecimal();
35     }
36
37     // javadoc is inherited
38     public void objectToEntry(BigDecimal object, TupleOutput output) {
39
40         output.writeBigDecimal(object);
41     }
42
43     // javadoc is inherited
44     protected TupleOutput getTupleOutput(BigDecimal object) {
45
46         return sizedOutput(object);
47     }
48
49     /**
50      * Converts an entry buffer into a <code>BigDecimal</code> value.
51      *
52      * @param entry is the source entry buffer.
53      *
54      * @return the resulting value.
55      */
56     public static BigDecimal entryToBigDecimal(DatabaseEntry entry) {
57
58         return entryToInput(entry).readBigDecimal();
59     }
60
61     /**
62      * Converts a <code>BigDecimal</code> value into an entry buffer.
63      *
64      * @param val is the source value.
65      *
66      * @param entry is the destination entry buffer.
67      */
68     public static void bigDecimalToEntry(BigDecimal val, DatabaseEntry entry) {
69
70         outputToEntry(sizedOutput(val).writeBigDecimal(val), entry);
71     }
72
73     /**
74      * Returns a tuple output object of the maximum size needed, to avoid
75      * wasting space when a single primitive is output.
76      */
77     private static TupleOutput sizedOutput(BigDecimal val) {
78
79         int len = TupleOutput.getBigDecimalMaxByteLength(val);
80         return new TupleOutput(new byte[len]);
81     }
82 }