limpbizkit | c1e65da | 2009-08-19 18:18:16 +0000 | [diff] [blame] | 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.inject.ConfigurationException; |
| 19 | import com.google.inject.Key; |
| 20 | import com.google.inject.TypeLiteral; |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Maps; |
limpbizkit | c1e65da | 2009-08-19 18:18:16 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.Message; |
| 24 | import java.util.Collections; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
limpbizkit | 6840fcb | 2009-08-19 18:31:04 +0000 | [diff] [blame] | 28 | * Utility class for collecting factory bindings. Used for configuring {@link FactoryProvider2}. |
limpbizkit | c1e65da | 2009-08-19 18:18:16 +0000 | [diff] [blame] | 29 | * |
| 30 | * @author schmitt@google.com (Peter Schmitt) |
| 31 | */ |
| 32 | class BindingCollector { |
| 33 | |
| 34 | private final Map<Key<?>, TypeLiteral<?>> bindings = Maps.newHashMap(); |
| 35 | |
| 36 | public BindingCollector addBinding(Key<?> key, TypeLiteral<?> target) { |
| 37 | if (bindings.containsKey(key)) { |
limpbizkit | 6840fcb | 2009-08-19 18:31:04 +0000 | [diff] [blame] | 38 | throw new ConfigurationException(ImmutableSet.of( |
limpbizkit | c1e65da | 2009-08-19 18:18:16 +0000 | [diff] [blame] | 39 | new Message("Only one implementation can be specified for " + key))); |
| 40 | } |
| 41 | |
| 42 | bindings.put(key, target); |
| 43 | |
| 44 | return this; |
| 45 | } |
| 46 | |
| 47 | public Map<Key<?>, TypeLiteral<?>> getBindings() { |
| 48 | return Collections.unmodifiableMap(bindings); |
| 49 | } |
| 50 | } |