| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.impl; |
| 9 |
|
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.LinkedHashMap; |
| 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 |
public final void initializeProxy(Map<K, V> map) { |
| 32 |
int size = map.size(); |
| 33 |
keys = (K[]) new Object[size]; |
| 34 |
values = (V[]) new Object[size]; |
| 35 |
int i = 0; |
| 36 |
for (Map.Entry<K, V> entry : map.entrySet()) { |
| 37 |
keys[i] = entry.getKey(); |
| 38 |
values[i] = entry.getValue(); |
| 39 |
i += 1; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public final Map<K, V> convertProxy() { |
| 44 |
int size = values.length; |
| 45 |
Map<K, V> map = newInstance(size); |
| 46 |
for (int i = 0; i < size; i += 1) { |
| 47 |
map.put(keys[i], values[i]); |
| 48 |
} |
| 49 |
return map; |
| 50 |
} |
| 51 |
|
| 52 |
protected abstract Map<K, V> newInstance(int size); |
| 53 |
|
| 54 |
@Persistent(proxyFor=HashMap.class) |
| 55 |
static class HashMapProxy<K, V> extends MapProxy<K, V> { |
| 56 |
|
| 57 |
protected HashMapProxy() {} |
| 58 |
|
| 59 |
protected Map<K, V> newInstance(int size) { |
| 60 |
return new HashMap<K, V>(size); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@Persistent(proxyFor=TreeMap.class) |
| 65 |
static class TreeMapProxy<K, V> extends MapProxy<K, V> { |
| 66 |
|
| 67 |
protected TreeMapProxy() {} |
| 68 |
|
| 69 |
protected Map<K, V> newInstance(int size) { |
| 70 |
return new TreeMap<K, V>(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@Persistent(proxyFor=LinkedHashMap.class) |
| 75 |
static class LinkedHashMapProxy<K, V> extends MapProxy<K, V> { |
| 76 |
|
| 77 |
protected LinkedHashMapProxy() {} |
| 78 |
|
| 79 |
protected Map<K, V> newInstance(int size) { |
| 80 |
return new LinkedHashMap<K, V>(); |
| 81 |
} |
| 82 |
} |
| 83 |
} |