import java.util.*; // not final to allow for subclasses // but clear et al ... public class ReadOnlyMap implements Map { private Map map; public ReadOnlyMap(Map map) { this.map = map; } // this should map: // keys[0] -> values[0] // keys[1] -> values[1] // and so on ... public ReadOnlyMap(K[] keys, V[] values) { map = new HashMap(); for(int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } } // final ensures that no sub class will override this final public void clear() throws UnsupportedOperationException { throw new UnsupportedOperationException("ImmutableMap does not support clear() method"); } public boolean containsKey(Object key) { return map.containsKey(key); } public boolean containsValue(Object value) { return map.containsValue(value); } // final ensures that no sub class will override this // make sure values cannot be modified final public Set> entrySet() { return Collections.unmodifiableSet( map.entrySet()); } public boolean equals(Object o) { return map.equals(o); } public V get(Object key) { return map.get(key); } public int hashCode() { return map.hashCode(); } public boolean isEmpty() { return map.isEmpty(); } public Set keySet() { //return new HashSet(map.keySet()); return Collections.unmodifiableSet(map.keySet()); } // final ensures that no sub class will override this final public V put(K key, V value) throws UnsupportedOperationException { throw new UnsupportedOperationException("ImmutableMap does not support put() method"); } // final ensures that no sub class will override this final public void putAll(Map map) throws UnsupportedOperationException { throw new UnsupportedOperationException("ImmutableMap does not support putAll() method"); } // final ensures that no sub class will override this final public V remove(Object key) throws UnsupportedOperationException { throw new UnsupportedOperationException("ImmutableMap does not support remove() method"); } public int size() { return map.size(); } // final ensures that no sub class will override this // make sure values cannot be modified final public Collection values() { //return new ArrayList(map.values()); return Collections.unmodifiableCollection(map.values()); } public String toString() { return ""; } }