01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2000,2008 Oracle. All rights reserved.
05: *
06: * $Id: RecordNumberBinding.java,v 1.28.2.2 2008/01/07 15:14:05 cwl Exp $
07: */
08:
09: package com.sleepycat.bind;
10:
11: import com.sleepycat.compat.DbCompat;
12: import com.sleepycat.je.DatabaseEntry;
13:
14: /**
15: * An <code>EntryBinding</code> that treats a record number key entry as a
16: * <code>Long</code> key object.
17: *
18: * <p>Record numbers are returned as <code>Long</code> objects, although on
19: * input any <code>Number</code> object may be used.</p>
20: *
21: * @author Mark Hayes
22: */
23: public class RecordNumberBinding implements EntryBinding {
24:
25: /**
26: * Creates a byte array binding.
27: */
28: public RecordNumberBinding() {
29: }
30:
31: // javadoc is inherited
32: public Object entryToObject(DatabaseEntry entry) {
33:
34: return new Long(entryToRecordNumber(entry));
35: }
36:
37: // javadoc is inherited
38: public void objectToEntry(Object object, DatabaseEntry entry) {
39:
40: recordNumberToEntry(((Number) object).longValue(), entry);
41: }
42:
43: /**
44: * Utility method for use by bindings to translate a entry buffer to an
45: * record number integer.
46: *
47: * @param entry the entry buffer.
48: *
49: * @return the record number.
50: */
51: public static long entryToRecordNumber(DatabaseEntry entry) {
52:
53: return DbCompat.getRecordNumber(entry) & 0xFFFFFFFFL;
54: }
55:
56: /**
57: * Utility method for use by bindings to translate a record number integer
58: * to a entry buffer.
59: *
60: * @param recordNumber the record number.
61: *
62: * @param entry the entry buffer to hold the record number.
63: */
64: public static void recordNumberToEntry(long recordNumber,
65: DatabaseEntry entry) {
66: entry.setData(new byte[4], 0, 4);
67: DbCompat.setRecordNumber(entry, (int) recordNumber);
68: }
69: }
|