Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 java.util.Map;

A type-to-instance map backed by an com.google.common.collect.ImmutableMap. See also MutableTypeToInstanceMap.

Author(s):
Ben Yu
Since:
13.0
 
 public final class ImmutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B>
     implements TypeToInstanceMap<B> {

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

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

  
A builder for creating immutable type-to-instance maps. Example:
   static final ImmutableTypeToInstanceMap<Handler<?>> HANDLERS =
       ImmutableTypeToInstanceMap.<Handler<?>>builder()
           .put(new TypeToken<Handler<Foo>>() {, new FooHandler())
           .put(new TypeToken<Handler<Bar>>() {}, new SubBarHandler())
           .build();}
After invoking 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<? extends B>, B> mapBuilder
         = ImmutableMap.builder();
 
     private Builder() {}

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

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

    
Returns a new immutable type-to-instance map containing the entries provided to this builder.

Throws:
java.lang.IllegalArgumentException if duplicate keys were added
 
     public ImmutableTypeToInstanceMap<B> build() {
       return new ImmutableTypeToInstanceMap<B>(.build());
     }
   }
 
   private final ImmutableMap<TypeToken<? extends B>, B> delegate;
 
  private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken<? extends B>, B> delegate) {
    this. = delegate;
  }
  @Override public <T extends B> T getInstance(TypeToken<T> type) {
    return trustedGet(type.rejectTypeVariables());
  }

  
Guaranteed to throw an exception and leave the map unmodified.

  @Override public <T extends B> T putInstance(TypeToken<T> type, T value) {
    throw new UnsupportedOperationException();
  }
  @Override public <T extends B> T getInstance(Class<T> type) {
    return trustedGet(TypeToken.of(type));
  }

  
Guaranteed to throw an exception and leave the map unmodified.

  @Override public <T extends B> T putInstance(Class<T> type, T value) {
    throw new UnsupportedOperationException();
  }
  @Override protected Map<TypeToken<? extends B>, B> delegate() {
    return ;
  }
  @SuppressWarnings("unchecked"// value could not get in if not a T
  private <T extends B> T trustedGet(TypeToken<T> type) {
    return (T) .get(type);
  }
New to GrepCode? Check out our FAQ X