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