2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.inject;
19 import java.util.concurrent.ConcurrentHashMap;
22 * Cache of {@link CdiInjector}s for efficiency to avoid duplicate analysis of a
25 * @author Erik Brakkee
27 public class InjectorCache {
29 private Map<String, Injector> injectors;
31 private InjectorFactory injectorFactory;
34 * Constructs an empty cache.
36 * @param aInjectorFactory Injector factory to create Injectors.
38 public InjectorCache(InjectorFactory aInjectorFactory) {
39 injectorFactory = aInjectorFactory;
40 injectors = new ConcurrentHashMap<String, Injector>();
44 * Gets the injector for a given class. This returns a cached injector or
45 * creates a new injector and caches it.
48 * Class to find injector for.
51 public Injector getInjector(Class aClass) {
52 Injector injector = injectors.get(aClass.getName());
53 if (injector == null) {
54 // create and add injector
55 // NOTE: in rare circumstances this will lead to parallel
57 // an injector for the same class. However, only one of them
58 // will be the final one
59 // in the map. There are no side effects of this duplicate
60 // creation of injectors.
61 injector = injectorFactory.create(aClass);
62 injectors.put(aClass.getName(), injector);