2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2000, 2012 Oracle and/or its affiliates. All rights reserved.
8 package com.sleepycat.bind.tuple;
10 import java.math.BigDecimal;
12 import com.sleepycat.db.DatabaseEntry;
15 * A concrete <code>TupleBinding</code> for an unsorted <code>BigDecimal</code>
18 * <p>There are two ways to use this class:</p>
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>
27 * @see <a href="package-summary.html#bigDecimalFormats">BigDecimal Formats</a>
29 public class BigDecimalBinding extends TupleBinding<BigDecimal> {
31 // javadoc is inherited
32 public BigDecimal entryToObject(TupleInput input) {
34 return input.readBigDecimal();
37 // javadoc is inherited
38 public void objectToEntry(BigDecimal object, TupleOutput output) {
40 output.writeBigDecimal(object);
43 // javadoc is inherited
44 protected TupleOutput getTupleOutput(BigDecimal object) {
46 return sizedOutput(object);
50 * Converts an entry buffer into a <code>BigDecimal</code> value.
52 * @param entry is the source entry buffer.
54 * @return the resulting value.
56 public static BigDecimal entryToBigDecimal(DatabaseEntry entry) {
58 return entryToInput(entry).readBigDecimal();
62 * Converts a <code>BigDecimal</code> value into an entry buffer.
64 * @param val is the source value.
66 * @param entry is the destination entry buffer.
68 public static void bigDecimalToEntry(BigDecimal val, DatabaseEntry entry) {
70 outputToEntry(sizedOutput(val).writeBigDecimal(val), entry);
74 * Returns a tuple output object of the maximum size needed, to avoid
75 * wasting space when a single primitive is output.
77 private static TupleOutput sizedOutput(BigDecimal val) {
79 int len = TupleOutput.getBigDecimalMaxByteLength(val);
80 return new TupleOutput(new byte[len]);