View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.serialization;
    17
    18  import java.lang.ref.Reference;
    19  import java.util.Collection;
    20  import java.util.Map;
    21  import java.util.Set;
    22
    23  abstract class ReferenceMap<K, V> implements Map<K, V> {
    24
    25      private final Map<K, Reference<V>> delegate;
    26
    27      protected ReferenceMap(Map<K, Reference<V>> delegate) {
    28          this.delegate = delegate;
    29      }
    30
    31      abstract Reference<V> fold(V value);
    32
    33      private V unfold(Reference<V> ref) {
    34          if (ref == null) {
    35              return null;
    36          }
    37
    38          return ref.get();
    39      }
    40
    41      public int size() {
    42          return delegate.size();
    43      }
    44
    45      public boolean isEmpty() {
    46          return delegate.isEmpty();
    47      }
    48
    49      public boolean containsKey(Object key) {
    50          return delegate.containsKey(key);
    51      }
    52
    53      public boolean containsValue(Object value) {
    54          throw new UnsupportedOperationException();
    55      }
    56
    57      public V get(Object key) {
    58          return unfold(delegate.get(key));
    59      }
    60
    61      public V put(K key, V value) {
    62          return unfold(delegate.put(key, fold(value)));
    63      }
    64
    65      public V remove(Object key) {
    66          return unfold(delegate.remove(key));
    67      }
    68
    69      public void putAll(Map<? extends K, ? extends V> m) {
    70          for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
    71              delegate.put(entry.getKey(), fold(entry.getValue()));
    72          }
    73      }
    74
    75      public void clear() {
    76          delegate.clear();
    77      }
    78
    79      public Set<K> keySet() {
    80          return delegate.keySet();
    81      }
    82
    83      public Collection<V> values() {
    84          throw new UnsupportedOperationException();
    85      }
    86
    87      public Set<Entry<K, V>> entrySet() {
    88          throw new UnsupportedOperationException();
    89      }
    90  }