1 /** 2 * Copyright (C) 2009 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.inject.assistedinject; 17 18 import com.google.common.collect.ImmutableSet; 19 import com.google.common.collect.Maps; 20 import com.google.inject.ConfigurationException; 21 import com.google.inject.Key; 22 import com.google.inject.TypeLiteral; 23 import com.google.inject.spi.Message; 24 25 import java.util.Collections; 26 import java.util.Map; 27 28 /** 29 * Utility class for collecting factory bindings. Used for configuring {@link FactoryProvider2}. 30 * 31 * @author schmitt (at) google.com (Peter Schmitt) 32 */ 33 class BindingCollector { 34 35 private final Map<Key<?>, TypeLiteral<?>> bindings = Maps.newHashMap(); 36 37 public BindingCollector addBinding(Key<?> key, TypeLiteral<?> target) { 38 if (bindings.containsKey(key)) { 39 throw new ConfigurationException(ImmutableSet.of( 40 new Message("Only one implementation can be specified for " + key))); 41 } 42 43 bindings.put(key, target); 44 45 return this; 46 } 47 48 public Map<Key<?>, TypeLiteral<?>> getBindings() { 49 return Collections.unmodifiableMap(bindings); 50 } 51 52 @Override 53 public int hashCode() { 54 return bindings.hashCode(); 55 } 56 57 @Override 58 public boolean equals(Object obj) { 59 return (obj instanceof BindingCollector) && bindings.equals(((BindingCollector) obj).bindings); 60 } 61 } 62