2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
11 package edu.uci.ics.jung.algorithms.util;
13 import java.util.Collection;
14 import java.util.Collections;
19 * An implementation of <code>Map</code> that returns the constructor-supplied
20 * value for any input.
22 * @param <K> the key type
23 * @param <V> the value type
25 public class ConstantMap<K,V> implements Map<K,V> {
27 private Map<K,V> delegate;
30 * Creates an instance whose {@code get} method always returns {@code value}.
32 public ConstantMap(V value) {
33 delegate = Collections.<K,V>unmodifiableMap(Collections.<K,V>singletonMap(null, value));
36 public V get(Object key) {
37 return delegate.get(null);
44 public boolean containsKey(Object key) {
48 public boolean containsValue(Object value) {
49 return delegate.containsValue(value);
52 public Set<Entry<K, V>> entrySet() {
53 return delegate.entrySet();
57 public boolean equals(Object o) {
58 return delegate.equals(o);
62 public int hashCode() {
63 return delegate.hashCode();
66 public boolean isEmpty() {
67 return delegate.isEmpty();
70 public Set<K> keySet() {
71 return delegate.keySet();
74 public V put(K key, V value) {
75 return delegate.put(key, value);
78 public void putAll(Map<? extends K, ? extends V> t) {
82 public V remove(Object key) {
83 return delegate.remove(key);
87 return delegate.size();
90 public Collection<V> values() {
91 return delegate.values();