alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (WellBehavedMap.java)

This example Java source code file (WellBehavedMap.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractmapentry, entryset, forwardingmap, gwtcompatible, iterator, override, set, transformediterator, util, weakouter, wellbehavedmap

The WellBehavedMap.java Java example source code

/*
 * Copyright (C) 2011 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.j2objc.annotations.WeakOuter;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Workaround for
 * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312706">
 * EnumMap bug</a>. If you want to pass an {@code EnumMap}, with the
 * intention of using its {@code entrySet()} method, you should
 * wrap the {@code EnumMap} in this class instead.
 *
 * <p>This class is not thread-safe even if the underlying map is.
 *
 * @author Dimitris Andreou
 */
@GwtCompatible
final class WellBehavedMap<K, V> extends ForwardingMap {
  private final Map<K, V> delegate;
  private Set<Entry entrySet;

  private WellBehavedMap(Map<K, V> delegate) {
    this.delegate = delegate;
  }

  /**
   * Wraps the given map into a {@code WellBehavedEntriesMap}, which
   * intercepts its {@code entrySet()} method by taking the
   * {@code Set<K> keySet()} and transforming it to
   * {@code Set<Entry}. All other invocations are delegated as-is.
   */
  static <K, V> WellBehavedMap wrap(Map delegate) {
    return new WellBehavedMap<K, V>(delegate);
  }

  @Override
  protected Map<K, V> delegate() {
    return delegate;
  }

  @Override
  public Set<Entry entrySet() {
    Set<Entry es = entrySet;
    if (es != null) {
      return es;
    }
    return entrySet = new EntrySet();
  }

  @WeakOuter
  private final class EntrySet extends Maps.EntrySet<K, V> {
    @Override
    Map<K, V> map() {
      return WellBehavedMap.this;
    }

    @Override
    public Iterator<Entry iterator() {
      return new TransformedIterator<K, Entry(keySet().iterator()) {
        @Override
        Entry<K, V> transform(final K key) {
          return new AbstractMapEntry<K, V>() {
            @Override
            public K getKey() {
              return key;
            }

            @Override
            public V getValue() {
              return get(key);
            }

            @Override
            public V setValue(V value) {
              return put(key, value);
            }
          };
        }
      };
    }
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java WellBehavedMap.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.