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

com.sri.ai.util.collect.CopyOnWriteMap Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package com.sri.ai.util.collect;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A {@link Map} implementation that directs methods to another map,
 * which is either a map received at construction, or a copy of it
 * to a new {@link HashMap} made upon the first writing (or potentially writing} operation.
 */
public class CopyOnWriteMap implements Map {

	private Map base;
	private boolean ownsBase;
	
	public CopyOnWriteMap(Map base) {
		this.base = base;
		this.ownsBase = false;
	}
	
	private void copy() {
		if ( ! ownsBase) {
			base = new HashMap(base);
			ownsBase = true;
		}
	}

	@Override
	public void clear() {
		base = new HashMap();
	}

	@Override
	public boolean containsKey(Object arg0) {
		return base.containsKey(arg0);
	}

	@Override
	public boolean containsValue(Object arg0) {
		return base.containsValue(arg0);
	}

	@Override
	public Set> entrySet() {
		copy();
		return base.entrySet();
	}

	@Override
	public V get(Object arg0) {
		return base.get(arg0);
	}

	@Override
	public boolean isEmpty() {
		return base.isEmpty();
	}

	@Override
	public Set keySet() {
		copy();
		return base.keySet();
	}

	@Override
	public V put(K arg0, V arg1) {
		copy();
		return base.put(arg0, arg1);
	}

	@Override
	public void putAll(Map arg0) {
		copy();
		base.putAll(arg0);
	}

	@Override
	public V remove(Object arg0) {
		V result = null;
		Map newBase = new HashMap();
		for (Map.Entry entry : base.entrySet()) {
			if (entry.getKey().equals(arg0)) {
				result = entry.getValue();
			}
			else {
				newBase.put(entry.getKey(), entry.getValue());
			}
		}
		base = newBase;
		return result;
	}

	@Override
	public int size() {
		return base.size();
	}

	@Override
	public Collection values() {
		copy();
		return base.values();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy