Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FactoryCache |
|
| 1.0;1 | ||||
FactoryCache$1 |
|
| 1.0;1 |
1 | /* | |
2 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
3 | * you may not use this file except in compliance with the License. | |
4 | * You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
13 | */ | |
14 | package org.apache.commons.classscan.util; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.Collection; | |
18 | import java.util.Collections; | |
19 | import java.util.Iterator; | |
20 | ||
21 | /** | |
22 | * A cache of service providers. | |
23 | * | |
24 | * @param <T> The interface of the service provider | |
25 | */ | |
26 | public class FactoryCache<T> implements Iterable<T> { | |
27 | ||
28 | final private Collection<T> readOnly; | |
29 | ||
30 | /** | |
31 | * Find service providers available to the given classLoader | |
32 | * | |
33 | * @param serviceProvider The interface of the service provider | |
34 | * @param classLoader The ClassLoader to load the service provider instances | |
35 | */ | |
36 | 0 | public FactoryCache(Class<T> serviceProvider, ClassLoader classLoader) { |
37 | ||
38 | 0 | final ArrayList<T> instances = new ArrayList<T>(); |
39 | ||
40 | 0 | new ServiceVisitor<T>() { |
41 | @Override | |
42 | protected boolean visit(T factory) { | |
43 | 0 | instances.add(factory); |
44 | 0 | return true; |
45 | } | |
46 | }.visitProviders(serviceProvider, classLoader); | |
47 | ||
48 | 0 | instances.trimToSize(); |
49 | 0 | readOnly = Collections.unmodifiableCollection(instances); |
50 | 0 | } |
51 | ||
52 | @Override | |
53 | public Iterator<T> iterator() { | |
54 | 0 | return readOnly.iterator(); |
55 | } | |
56 | } |