com.sri.ai.util.collect.AbstractStackedMap Maven / Gradle / Ivy
/*
* Copyright (c) 2013, SRI International
* All rights reserved.
* Licensed under the The BSD 3-Clause License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://opensource.org/licenses/BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the aic-expresso nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.sri.ai.util.collect;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import com.google.common.annotations.Beta;
/**
* A partial implementation of {@link StackedMap}.
*
* IMPORTANT: this currently does not fulfill the Map contract perfectly because keySet() and entrySet()
* are NOT backed by the map.
*
* @author braz
*
* @param
* the type of the keys.
* @param
* the type of the values.
*/
@Beta
public abstract class AbstractStackedMap implements StackedMap, Serializable {
private static final long serialVersionUID = 1L;
/** Top map. */
protected Map top = null;
/** Base map. */
protected Map base = null;
abstract Map makeMap();
abstract Set makeSetForKeySet();
public AbstractStackedMap() {
top = makeMap();
}
/**
* Constructor receiving a base map.
*
* @param base
* the base Map that this Map stacks itself on top of.
*/
public AbstractStackedMap(Map base) {
this();
this.base = base;
}
/**
* Constructor receiving top and base maps
*
* @param top
* the top Map that this Map has on its stack.
* @param base
* the base Map that this Map stacks itself on top of.
*/
public AbstractStackedMap(Map top, Map base) {
this.top = top;
this.base = base;
}
@Override
public Map getTop() {
return top;
}
@Override
public void setTop(Map top) {
this.top = top;
}
@Override
public Map getBase() {
return base;
}
@Override
public void setBase(Map base) {
this.base = base;
}
@Override
public void clear() {
top.clear();
if (base != null) {
base.clear();
}
}
@Override
public V put(K key, V value) {
return top.put(key, value);
}
@Override
public void putAll(Map extends K, ? extends V> map) {
top.putAll(map);
}
@Override
public V get(Object key) {
V result = top.get(key);
if (result == null) {
if ( ! top.containsKey(key) && base != null) {
result = base.get(key);
}
}
return result;
}
@Override
public boolean containsKey(Object key) {
boolean result = top.containsKey(key) || (base != null && base.containsKey(key));
return result;
}
@Override
public boolean containsValue(Object value) {
boolean result = top.containsValue(value) || (base != null && base.containsValue(value));
return result;
}
@Override
public boolean isEmpty() {
boolean result = top.isEmpty() && (base == null || base.isEmpty());
return result;
}
@Override
public V remove(Object key) {
if (top.containsKey(key)) {
return top.remove(key);
}
if (base != null) {
return base.remove(key);
}
return null;
}
@Override
public int size() {
return keySet().size();
}
@Override
public Set> entrySet() {
Map result = makeMap();
result.putAll(top);
if (base != null) {
for (Map.Entry entry : base.entrySet()) {
if ( ! top.containsKey(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
}
}
}
return result.entrySet();
}
@Override
public Set keySet() {
Set result = makeSetForKeySet();
result.addAll(top.keySet());
if (base != null) {
result.addAll(base.keySet());
}
return result;
}
/**
* Very slow: copies the whole map into a regular map and returns values().
*/
@Override
public Collection values() {
Map map = makeMap();
map.putAll(this);
Collection result = map.values();
return result;
}
@Override
public boolean equals(Object other) {
if ( ! Map.class.isInstance(other)) {
return false;
}
@SuppressWarnings("unchecked")
Map otherMap = (Map) other;
boolean result = entrySet().equals(otherMap.entrySet());
return result;
}
@Override
public int hashCode() {
int result = 0;
for (Map.Entry entry : entrySet()) {
result += entry.hashCode();
}
return result;
}
@Override
public String toString() {
Map auxiliary = new LinkedHashMap(this);
return auxiliary.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy