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

Java example source code file (ImmutableTypeToInstanceMap.java)

This example Java source code file (ImmutableTypeToInstanceMap.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

beta, builder, canignorereturnvalue, deprecated, forwardingmap, immutablemap, immutabletypetoinstancemap, override, suppresswarnings, typetoken, unsupportedoperationexception, util

The ImmutableTypeToInstanceMap.java Java example source code

/*
 * 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 com.google.common.annotations.Beta;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.Map;

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

  /** Returns an empty type to instance map. */
  public static <B> ImmutableTypeToInstanceMap of() {
    return new ImmutableTypeToInstanceMap<B>(ImmutableMap., B>of());
  }

  /** Returns a new builder. */
  public static <B> Builder builder() {
    return new Builder<B>();
  }

  /**
   * A builder for creating immutable type-to-instance maps. Example:
   * <pre>   {@code
   *
   *   static final ImmutableTypeToInstanceMap<Handler HANDLERS =
   *       ImmutableTypeToInstanceMap.<Handlerbuilder()
   *           .put(new TypeToken<Handler() {}, new FooHandler())
   *           .put(new TypeToken<Handler() {}, new SubBarHandler())
   *           .build();}</pre>
   *
   * <p>After invoking {@link #build()} it is still possible to add more entries and build again.
   * Thus each map generated by this builder will be a superset of any map generated before it.
   *
   * @since 13.0
   */
  @Beta
  public static final class Builder<B> {
    private final ImmutableMap.Builder<TypeToken mapBuilder =
        ImmutableMap.builder();

    private Builder() {}

    /**
     * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
     * and will cause {@link #build} to fail.
     */
    @CanIgnoreReturnValue
    public <T extends B> Builder put(Class key, T value) {
      mapBuilder.put(TypeToken.of(key), value);
      return this;
    }

    /**
     * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
     * and will cause {@link #build} to fail.
     */
    @CanIgnoreReturnValue
    public <T extends B> Builder put(TypeToken key, T value) {
      mapBuilder.put(key.rejectTypeVariables(), value);
      return this;
    }

    /**
     * Returns a new immutable type-to-instance map containing the entries provided to this builder.
     *
     * @throws IllegalArgumentException if duplicate keys were added
     */
    public ImmutableTypeToInstanceMap<B> build() {
      return new ImmutableTypeToInstanceMap<B>(mapBuilder.build());
    }
  }

  private final ImmutableMap<TypeToken delegate;

  private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken delegate) {
    this.delegate = delegate;
  }

  @Override
  public <T extends B> T getInstance(TypeToken type) {
    return trustedGet(type.rejectTypeVariables());
  }

  /**
   * Guaranteed to throw an exception and leave the map unmodified.
   *
   * @deprecated unsupported operation
   * @throws UnsupportedOperationException always
   */
  @CanIgnoreReturnValue
  @Deprecated
  @Override
  public <T extends B> T putInstance(TypeToken type, T value) {
    throw new UnsupportedOperationException();
  }

  @Override
  public <T extends B> T getInstance(Class type) {
    return trustedGet(TypeToken.of(type));
  }

  /**
   * Guaranteed to throw an exception and leave the map unmodified.
   *
   * @deprecated unsupported operation
   * @throws UnsupportedOperationException always
   */
  @CanIgnoreReturnValue
  @Deprecated
  @Override
  public <T extends B> T putInstance(Class type, T value) {
    throw new UnsupportedOperationException();
  }

  /**
   * Guaranteed to throw an exception and leave the map unmodified.
   *
   * @deprecated unsupported operation
   * @throws UnsupportedOperationException always
   */
  @CanIgnoreReturnValue
  @Deprecated
  @Override
  public B put(TypeToken<? extends B> key, B value) {
    throw new UnsupportedOperationException();
  }

  /**
   * Guaranteed to throw an exception and leave the map unmodified.
   *
   * @deprecated unsupported operation
   * @throws UnsupportedOperationException always
   */
  @Deprecated
  @Override
  public void putAll(Map<? extends TypeToken map) {
    throw new UnsupportedOperationException();
  }

  @Override
  protected Map<TypeToken delegate() {
    return delegate;
  }

  @SuppressWarnings("unchecked") // value could not get in if not a T
  private <T extends B> T trustedGet(TypeToken type) {
    return (T) delegate.get(type);
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java ImmutableTypeToInstanceMap.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.