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 import java.util.Collections;
25 import java.util.Map;
26 
27 /**
28  * Utility class for collecting factory bindings. Used for configuring {@link FactoryProvider2}.
29  *
30  * @author schmitt@google.com (Peter Schmitt)
31  */
32 class BindingCollector {
33 
34   private final Map<Key<?>, TypeLiteral<?>> bindings = Maps.newHashMap();
35 
addBinding(Key<?> key, TypeLiteral<?> target)36   public BindingCollector addBinding(Key<?> key, TypeLiteral<?> target) {
37     if (bindings.containsKey(key)) {
38       throw new ConfigurationException(
39           ImmutableSet.of(new Message("Only one implementation can be specified for " + key)));
40     }
41 
42     bindings.put(key, target);
43 
44     return this;
45   }
46 
getBindings()47   public Map<Key<?>, TypeLiteral<?>> getBindings() {
48     return Collections.unmodifiableMap(bindings);
49   }
50 
51   @Override
hashCode()52   public int hashCode() {
53     return bindings.hashCode();
54   }
55 
56   @Override
equals(Object obj)57   public boolean equals(Object obj) {
58     return (obj instanceof BindingCollector) && bindings.equals(((BindingCollector) obj).bindings);
59   }
60 }
61