/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2002,2008 Oracle. All rights reserved. * * $Id: MapProxy.java,v 1.1 2008/02/07 17:12:27 mark Exp $ */ package com.sleepycat.persist.impl; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import com.sleepycat.persist.model.Persistent; import com.sleepycat.persist.model.PersistentProxy; /** * Proxy for a Map. * * @author Mark Hayes */ @Persistent abstract class MapProxy implements PersistentProxy> { private K[] keys; private V[] values; protected MapProxy() {} public final void initializeProxy(Map map) { int size = map.size(); keys = (K[]) new Object[size]; values = (V[]) new Object[size]; int i = 0; for (Map.Entry entry : map.entrySet()) { keys[i] = entry.getKey(); values[i] = entry.getValue(); i += 1; } } public final Map convertProxy() { int size = values.length; Map map = newInstance(size); for (int i = 0; i < size; i += 1) { map.put(keys[i], values[i]); } return map; } protected abstract Map newInstance(int size); @Persistent(proxyFor=HashMap.class) static class HashMapProxy extends MapProxy { protected HashMapProxy() {} protected Map newInstance(int size) { return new HashMap(size); } } @Persistent(proxyFor=TreeMap.class) static class TreeMapProxy extends MapProxy { protected TreeMapProxy() {} protected Map newInstance(int size) { return new TreeMap(); } } }