/*
 * Copyright (C) 2008-2015 by Holger Arndt
 *
 * This file is part of the Universal Java Matrix Package (UJMP).
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * UJMP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * UJMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with UJMP; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package org.ujmp.core.collections.map;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.ujmp.core.interfaces.Wrapper;

public class CachedMap extends AbstractMap implements Wrapper>, Flushable,
		Closeable {
	private static final long serialVersionUID = 1383398694858918398L;

	private transient Map source = null;

	private transient Map cache = null;

	public CachedMap(Map source) {
		setWrappedObject(source);
	}

	public CachedMap(Map source, Map cache) {
		setWrappedObject(source);
		this.cache = cache;
	}

	public void clear() {
		getCache().clear();
		getWrappedObject().clear();
	}

	public boolean containsKey(Object key) {
		if (getCache().containsKey(key)) {
			return true;
		}
		return getWrappedObject().containsKey(key);
	}

	public boolean containsValue(Object value) {
		if (getCache().containsValue(value)) {
			return true;
		}
		return getWrappedObject().containsValue(value);
	}

	@SuppressWarnings("unchecked")
	public V get(Object key) {
		V value = getCache().get(key);
		if (value == null) {
			value = getWrappedObject().get(key);
			if (value != null) {
				getCache().put((K) key, value);
			}
		}
		return value;
	}

	public boolean isEmpty() {
		if (!getCache().isEmpty()) {
			return false;
		}
		return getWrappedObject().isEmpty();
	}

	public Set keySet() {
		return getWrappedObject().keySet();
	}

	public V put(K key, V value) {
		getCache().put(key, value);
		return getWrappedObject().put(key, value);
	}

	public void putAll(Map m) {
		for (K k : m.keySet()) {
			put(k, m.get(k));
		}
	}

	public V remove(Object key) {
		getCache().remove(key);
		return getWrappedObject().remove(key);
	}

	public int size() {
		return getWrappedObject().size();
	}

	public Map getWrappedObject() {
		if (source == null) {
			source = new HashMap();
		}
		return source;
	}

	public void setWrappedObject(Map object) {
		getCache().clear();
		this.source = object;
	}

	public Map getCache() {
		if (cache == null) {
			cache = new SoftHashMap();
		}
		return cache;
	}

	public void close() throws IOException {
		if (source instanceof Closeable) {
			((Closeable) source).close();
		}
		if (cache instanceof Closeable) {
			((Closeable) cache).close();
		}
	}

	public void flush() throws IOException {
		if (source instanceof Flushable) {
			((Flushable) source).flush();
		}
		if (cache instanceof Flushable) {
			((Flushable) cache).flush();
		}
	}

}

 

Ads help maintain this website.