All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sleepycat.persist.impl.MapProxy Maven / Gradle / Ivy

The newest version!
/*-
 * Copyright (C) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
 *
 * This file was distributed by Oracle as part of a version of Oracle Berkeley
 * DB Java Edition made available at:
 *
 * http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html
 *
 * Please see the LICENSE file included in the top-level directory of the
 * appropriate version of Oracle Berkeley DB Java Edition for a copy of the
 * license and additional information.
 */

package com.sleepycat.persist.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
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();
        }
    }
     
    @Persistent(proxyFor=LinkedHashMap.class) 
    static class LinkedHashMapProxy extends MapProxy { 
 
        protected LinkedHashMapProxy() {} 
 
        protected Map newInstance(int size) { 
            return new LinkedHashMap(); 
        } 
    } 
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy