Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 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 static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.Map;
 
A class-to-instance map backed by an ImmutableMap. See also MutableClassToInstanceMap.

Author(s):
Kevin Bourrillion
Since:
2.0 (imported from Google Collections Library)
 
 public final class ImmutableClassToInstanceMap<B> 
     extends ForwardingMap<Class<? extends B>, B> 
     implements ClassToInstanceMap<B>, Serializable {
  
  
Returns a new builder. The generated builder is equivalent to the builder created by the ImmutableClassToInstanceMap.Builder constructor.
 
   public static <B> Builder<B> builder() {
     return new Builder<B>();
   }

  
A builder for creating immutable class-to-instance maps. Example:
   static final ImmutableClassToInstanceMap<Handler> HANDLERS =
       new ImmutableClassToInstanceMap.Builder<Handler>()
           .put(FooHandler.class, new FooHandler())
           .put(BarHandler.class, new SubBarHandler())
           .put(Handler.class, new QuuxHandler())
           .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:
2.0 (imported from Google Collections Library)
 
   public static final class Builder<B> {
     private final ImmutableMap.Builder<Class<? extends B>, B> mapBuilder
         = ImmutableMap.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(keyvalue);
       return this;
     }

    
Associates all of map's keys and values in the built map. Duplicate keys are not allowed, and will cause build() to fail.

Throws:
java.lang.NullPointerException if any key or value in map is null
java.lang.ClassCastException if any value is not an instance of the type specified by its key
 
     public <T extends B> Builder<B> putAll(
         Map<? extends Class<? extends T>, ? extends T> map) {
       for (Entry<? extends Class<? extends T>, ? extends T> entry
           : map.entrySet()) {
         Class<? extends T> type = entry.getKey();
         T value = entry.getValue();
         .put(typecast(typevalue));
       }
       return this;
     }
 
     private static <B, T extends B> T cast(Class<T> type, B value) {
       return Primitives.wrap(type).cast(value);
     }

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

Throws:
java.lang.IllegalArgumentException if duplicate keys were added
    public ImmutableClassToInstanceMap<B> build() {
      return new ImmutableClassToInstanceMap<B>(.build());
    }
  }

  
Returns an immutable map containing the same entries as map. If map somehow contains entries with duplicate keys (for example, if it is a SortedMap whose comparator is not consistent with equals), the results of this method are undefined.

Note: Despite what the method name suggests, if map is an ImmutableClassToInstanceMap, no copy will actually be performed.

Throws:
java.lang.NullPointerException if any key or value in map is null
java.lang.ClassCastException if any value is not an instance of the type specified by its key
  public static <B, S extends B> ImmutableClassToInstanceMap<B> copyOf(
      Map<? extends Class<? extends S>, ? extends S> map) {
    if (map instanceof ImmutableClassToInstanceMap) {
      @SuppressWarnings("unchecked"// covariant casts safe (unmodifiable)
      // Eclipse won't compile if we cast to the parameterized type.
      return cast;
    }
    return new Builder<B>().putAll(map).build();
  }
  private final ImmutableMap<Class<? extends B>, B> delegate;
      ImmutableMap<Class<? extends B>, B> delegate) {
    this. = delegate;
  }
  @Override protected Map<Class<? extends B>, B> delegate() {
    return ;
  }
  @SuppressWarnings("unchecked"// value could not get in if not a T
  public <T extends B> T getInstance(Class<T> type) {
    return (T) .get(checkNotNull(type));
  }

  
Guaranteed to throw an exception and leave the map unmodified.

Deprecated:
Unsupported operation.
Throws:
java.lang.UnsupportedOperationException always
  public <T extends B> T putInstance(Class<T> type, T value) {
    throw new UnsupportedOperationException();
  }
New to GrepCode? Check out our FAQ X