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 */
013package 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 */
020public 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    @Override
034    AbstractBucket<K, V> getNext() {
035      return next;
036    }
037
038    @Override
039    AbstractBucket<K, V> setNext(AbstractBucket<K, V> n) {
040      if (next == n) {
041        return this;
042      } else {
043        return new Bucket<K, V>(key, value, n);
044      }
045    }
046
047    @Override
048    K getKey() {
049      return key;
050    }
051
052    @Override
053    V getValue() {
054      return value;
055    }
056
057    @Override
058    void setValue(V v) {
059      throw new UnsupportedOperationException();
060    }
061  }
062
063  @Override
064  AbstractBucket<K,V> createNewBucket(K key, V value, AbstractBucket<K, V> next) {
065    return new Bucket<K,V>(key, value, next);
066  }
067
068  public ImmutableEntryHashMapRVM() {
069    super(DEFAULT_SIZE);
070  }
071
072  public ImmutableEntryHashMapRVM(int size) {
073    super(size);
074  }
075
076  @Override
077  public V remove(K key) {
078    throw new UnsupportedOperationException();
079  }
080
081  @Override
082  public void removeAll() {
083    throw new UnsupportedOperationException();
084  }
085
086  @Override
087  protected boolean same(K k1, K k2) {
088    return k1.equals(k2);
089  }
090
091  @Override
092  protected int hashTheKey(K key) {
093    return key.hashCode();
094  }
095}