001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Eclipse Public License (EPL);
005 * You may not use this file except in compliance with the License. You
006 * may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.jikesrvm.util;
014
015 /**
016 * A hash map with entirely immutable buckets. It doesn't correctly support
017 * remove, and its values cannot be mutated by a put with the same key - use
018 * with care.
019 */
020 public final class ImmutableEntryHashMapRVM<K, V> extends AbstractHashMapRVM<K,V> {
021
022 static final class Bucket<K, V> extends AbstractBucket<K,V> {
023 private final AbstractBucket<K, V> next;
024 private final K key;
025 private final V value;
026
027 Bucket(K k, V v, AbstractBucket<K, V> n) {
028 key = k;
029 value = v;
030 next = n;
031 }
032
033 AbstractBucket<K, V> getNext() {
034 return next;
035 }
036
037 AbstractBucket<K, V> setNext(AbstractBucket<K, V> n) {
038 if (next == n) {
039 return this;
040 } else {
041 return new Bucket<K, V>(key, value, n);
042 }
043 }
044
045 K getKey() {
046 return key;
047 }
048
049 V getValue() {
050 return value;
051 }
052
053 void setValue(V v) {
054 throw new UnsupportedOperationException();
055 }
056 }
057
058 @Override
059 AbstractBucket<K,V> createNewBucket(K key, V value, AbstractBucket<K, V> next) {
060 return new Bucket<K,V>(key, value, next);
061 }
062
063 public ImmutableEntryHashMapRVM() {
064 super(DEFAULT_SIZE);
065 }
066
067 public ImmutableEntryHashMapRVM(int size) {
068 super(size);
069 }
070
071 public V remove(K key) {
072 throw new UnsupportedOperationException();
073 }
074
075 @Override
076 protected boolean same(K k1, K k2) {
077 return k1.equals(k2);
078 }
079
080 @Override
081 protected int hashTheKey(K key) {
082 return key.hashCode();
083 }
084 }