All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.reflect.MutableTypeToInstanceMap Maven / Gradle / Ivy

Go to download

Guava is a suite of core and expanded libraries that include utility classes, google's collections, io classes, and much much more.

There is a newer version: 33.1.0-jre
Show newest version
/*
 * Copyright (C) 2012 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.reflect;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ForwardingMapEntry;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}.
 *
 * @author Ben Yu
 * @since 13.0
 */
@Beta
public final class MutableTypeToInstanceMap extends ForwardingMap, B>
    implements TypeToInstanceMap {

  private final Map, B> backingMap = Maps.newHashMap();

  @Override
  public  @Nullable T getInstance(Class type) {
    return trustedGet(TypeToken.of(type));
  }

  @Override
  public  @Nullable T getInstance(TypeToken type) {
    return trustedGet(type.rejectTypeVariables());
  }

  @Override
  @CanIgnoreReturnValue
  public  @Nullable T putInstance(Class type, @Nullable T value) {
    return trustedPut(TypeToken.of(type), value);
  }

  @Override
  @CanIgnoreReturnValue
  public  @Nullable T putInstance(TypeToken type, @Nullable T value) {
    return trustedPut(type.rejectTypeVariables(), value);
  }

  /**
   * Not supported. Use {@link #putInstance} instead.
   *
   * @deprecated unsupported operation
   * @throws UnsupportedOperationException always
   */
  @CanIgnoreReturnValue
  @Deprecated
  @Override
  public B put(TypeToken key, B value) {
    throw new UnsupportedOperationException("Please use putInstance() instead.");
  }

  /**
   * Not supported. Use {@link #putInstance} instead.
   *
   * @deprecated unsupported operation
   * @throws UnsupportedOperationException always
   */
  @Deprecated
  @Override
  public void putAll(Map, ? extends B> map) {
    throw new UnsupportedOperationException("Please use putInstance() instead.");
  }

  @Override
  public Set, B>> entrySet() {
    return UnmodifiableEntry.transformEntries(super.entrySet());
  }

  @Override
  protected Map, B> delegate() {
    return backingMap;
  }

  @SuppressWarnings("unchecked") // value could not get in if not a T
  private  @Nullable T trustedPut(TypeToken type, @Nullable T value) {
    return (T) backingMap.put(type, value);
  }

  @SuppressWarnings("unchecked") // value could not get in if not a T
  private  @Nullable T trustedGet(TypeToken type) {
    return (T) backingMap.get(type);
  }

  private static final class UnmodifiableEntry extends ForwardingMapEntry {

    private final Entry delegate;

    static  Set> transformEntries(final Set> entries) {
      return new ForwardingSet>() {
        @Override
        protected Set> delegate() {
          return entries;
        }

        @Override
        public Iterator> iterator() {
          return UnmodifiableEntry.transformEntries(super.iterator());
        }

        @Override
        public Object[] toArray() {
          return standardToArray();
        }

        @Override
        public  T[] toArray(T[] array) {
          return standardToArray(array);
        }
      };
    }

    private static  Iterator> transformEntries(Iterator> entries) {
      return Iterators.transform(
          entries,
          new Function, Entry>() {
            @Override
            public Entry apply(Entry entry) {
              return new UnmodifiableEntry<>(entry);
            }
          });
    }

    private UnmodifiableEntry(java.util.Map.Entry delegate) {
      this.delegate = checkNotNull(delegate);
    }

    @Override
    protected Entry delegate() {
      return delegate;
    }

    @Override
    public V setValue(V value) {
      throw new UnsupportedOperationException();
    }
  }
}