blob: d464b6a869fb30796bc821002dcf5626bf8e7bee [file] [log] [blame]
limpbizkitc1e65da2009-08-19 18:18:16 +00001/**
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 */
16package com.google.inject.assistedinject;
17
18import com.google.inject.ConfigurationException;
19import com.google.inject.Key;
20import com.google.inject.TypeLiteral;
sberlind9c913a2011-06-26 21:02:54 +000021import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Maps;
limpbizkitc1e65da2009-08-19 18:18:16 +000023import com.google.inject.spi.Message;
24import java.util.Collections;
25import java.util.Map;
26
27/**
limpbizkit6840fcb2009-08-19 18:31:04 +000028 * Utility class for collecting factory bindings. Used for configuring {@link FactoryProvider2}.
limpbizkitc1e65da2009-08-19 18:18:16 +000029 *
30 * @author schmitt@google.com (Peter Schmitt)
31 */
32class 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)) {
limpbizkit6840fcb2009-08-19 18:31:04 +000038 throw new ConfigurationException(ImmutableSet.of(
limpbizkitc1e65da2009-08-19 18:18:16 +000039 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}