1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: KeyValueAdapter.java,v 1.1 2008/02/07 17:12:26 mark Exp $
7 */
8
9package com.sleepycat.persist;
10
11import com.sleepycat.bind.EntryBinding;
12import com.sleepycat.db.DatabaseEntry;
13
14/**
15 * A ValueAdapter where the "value" is the key (the primary key in a primary
16 * index or the secondary key in a secondary index).
17 *
18 * @author Mark Hayes
19 */
20class KeyValueAdapter<V> implements ValueAdapter<V> {
21
22    private EntryBinding keyBinding;
23
24    KeyValueAdapter(Class<V> keyClass, EntryBinding keyBinding) {
25        this.keyBinding = keyBinding;
26    }
27
28    public DatabaseEntry initKey() {
29        return new DatabaseEntry();
30    }
31
32    public DatabaseEntry initPKey() {
33        return null;
34    }
35
36    public DatabaseEntry initData() {
37        return BasicIndex.NO_RETURN_ENTRY;
38    }
39
40    public void clearEntries(DatabaseEntry key,
41                             DatabaseEntry pkey,
42                             DatabaseEntry data) {
43        key.setData(null);
44    }
45
46    public V entryToValue(DatabaseEntry key,
47                          DatabaseEntry pkey,
48                          DatabaseEntry data) {
49        return (V) keyBinding.entryToObject(key);
50    }
51
52    public void valueToData(V value, DatabaseEntry data) {
53        throw new UnsupportedOperationException
54            ("Cannot change the data in a key-only index");
55    }
56}
57