1 /******************************************************************************
2 * Copyright (C) PicoContainer Organization. All rights reserved. *
3 * ------------------------------------------------------------------------- *
4 * The software in this package is published under the terms of the BSD *
5 * style license a copy of which has been included with this distribution in *
6 * the LICENSE.txt file. *
7 * *
8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9 *****************************************************************************/
10
11 package picocontainer.aggregated;
12
13 import picocontainer.defaults.DefaultPicoContainer;
14 import picocontainer.defaults.DefaultComponentFactory;
15 import picocontainer.PicoContainer;
16 import picocontainer.PicoInitializationException;
17
18 import java.util.Arrays;
19 import java.util.Set;
20 import java.util.HashSet;
21
22 /***
23 * AggregatedContainersContainer aggregates the the contents of more
24 * than one container together for the sake of a single list of
25 * components. This list may be used as the parent container for
26 * another PicoContainer. This will result in directive graphs of
27 * containers/components rather than just trees.
28 *
29 * It is not in itself, a Pico component (the array in the
30 * constructor puts paid to that).
31 *
32 */
33 public class AggregatedContainersContainer extends DefaultPicoContainer {
34
35 private final PicoContainer[] containers;
36
37 public AggregatedContainersContainer(final PicoContainer[] containers) {
38 super(new DefaultComponentFactory());
39 if( containers == null ) {
40 throw new NullPointerException("containers can't be null");
41 }
42 for (int i = 0; i < containers.length; i++) {
43 PicoContainer container = containers[i];
44 if( container == null ) {
45 throw new NullPointerException("PicoContainer at position " + i + " was null");
46 }
47 }
48 this.containers = containers;
49 }
50
51 public static class Filter extends AggregatedContainersContainer {
52 private final PicoContainer subject;
53
54 public Filter(final PicoContainer container) {
55 super(new PicoContainer[]{container});
56 subject = container;
57 }
58
59 public PicoContainer getSubject() {
60 return subject;
61 }
62 }
63
64 public boolean hasComponent(Class compType) {
65 for (int i = 0; i < containers.length; i++) {
66 PicoContainer container = containers[i];
67 if (container.hasComponent(compType)) {
68 return true;
69 }
70 }
71 return false;
72 }
73
74 public Object getComponent(Class compType) {
75 for (int i = 0; i < containers.length; i++) {
76 PicoContainer container = containers[i];
77 if (container.hasComponent(compType)) {
78 return container.getComponent(compType);
79 }
80 }
81 return null;
82 }
83
84 public Class[] getComponentTypes() {
85 Set componentTypes = new HashSet();
86 for (int i = 0; i < containers.length; i++) {
87 PicoContainer container = containers[i];
88 componentTypes.addAll(Arrays.asList(container.getComponentTypes()));
89 }
90 return (Class[]) componentTypes.toArray(new Class[containers.length]);
91 }
92
93 public void instantiateComponents() throws PicoInitializationException {
94 }
95 }
This page was automatically generated by Maven