01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: MapProxy.java,v 1.4.2.2 2008/01/07 15:14:19 cwl Exp $
07: */
08:
09: package com.sleepycat.persist.impl;
10:
11: import java.util.HashMap;
12: import java.util.Map;
13: import java.util.TreeMap;
14:
15: import com.sleepycat.persist.model.Persistent;
16: import com.sleepycat.persist.model.PersistentProxy;
17:
18: /**
19: * Proxy for a Map.
20: *
21: * @author Mark Hayes
22: */
23: @Persistent
24: abstract class MapProxy<K, V> implements PersistentProxy<Map<K, V>> {
25:
26: private K[] keys;
27: private V[] values;
28:
29: protected MapProxy() {
30: }
31:
32: public final void initializeProxy(Map<K, V> map) {
33: int size = map.size();
34: keys = (K[]) new Object[size];
35: values = (V[]) new Object[size];
36: int i = 0;
37: for (Map.Entry<K, V> entry : map.entrySet()) {
38: keys[i] = entry.getKey();
39: values[i] = entry.getValue();
40: i += 1;
41: }
42: }
43:
44: public final Map<K, V> convertProxy() {
45: int size = values.length;
46: Map<K, V> map = newInstance(size);
47: for (int i = 0; i < size; i += 1) {
48: map.put(keys[i], values[i]);
49: }
50: return map;
51: }
52:
53: protected abstract Map<K, V> newInstance(int size);
54:
55: @Persistent(proxyFor=HashMap.class)
56: static class HashMapProxy<K, V> extends MapProxy<K, V> {
57:
58: protected HashMapProxy() {
59: }
60:
61: protected Map<K, V> newInstance(int size) {
62: return new HashMap<K, V>(size);
63: }
64: }
65:
66: @Persistent(proxyFor=TreeMap.class)
67: static class TreeMapProxy<K, V> extends MapProxy<K, V> {
68:
69: protected TreeMapProxy() {
70: }
71:
72: protected Map<K, V> newInstance(int size) {
73: return new TreeMap<K, V>();
74: }
75: }
76: }
|