KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > EntityValueAdapter


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: EntityValueAdapter.java,v 1.5 2006/10/30 21:14:30 bostic Exp $
7  */

8
9 package com.sleepycat.persist;
10
11 import com.sleepycat.bind.EntityBinding;
12 import com.sleepycat.je.DatabaseEntry;
13
14 /**
15  * A ValueAdapter where the "value" is the entity.
16  *
17  * @author Mark Hayes
18  */

19 class EntityValueAdapter<V> implements ValueAdapter<V> {
20
21     private EntityBinding entityBinding;
22     private boolean isSecondary;
23
24     EntityValueAdapter(Class JavaDoc<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
Popular Tags