Преглед на файлове

Allow multiple parents for SimpleInjectors

Change-Id: I7fe3eba4843cff80052156c6b1adc7eace216895
Reviewed-on: http://gerrit.dmdirc.com/2217
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.7rc1
Chris Smith преди 12 години
родител
ревизия
e9447070bc
променени са 1 файла, в които са добавени 12 реда и са изтрити 15 реда
  1. 12
    15
      src/com/dmdirc/util/SimpleInjector.java

+ 12
- 15
src/com/dmdirc/util/SimpleInjector.java Целия файл

29
 
29
 
30
 /**
30
 /**
31
  * Facilitates simple injection of parameters into a constructor.
31
  * Facilitates simple injection of parameters into a constructor.
32
+ * <p>
33
+ * Injectors may be given a set of parent injectors, from which they will
34
+ * inherit parameters. No checking is performed to prevent circular references
35
+ * within parents.
32
  */
36
  */
33
 public class SimpleInjector {
37
 public class SimpleInjector {
34
 
38
 
35
-    /** The parent injector, if any. */
36
-    private final SimpleInjector parent;
39
+    /** The parent injectors, if any. */
40
+    private final SimpleInjector[] parents;
37
 
41
 
38
     /** A mapping of known classes to the objects that should be injected. */
42
     /** A mapping of known classes to the objects that should be injected. */
39
     private final Map<Class<?>, Object> parameters
43
     private final Map<Class<?>, Object> parameters
40
             = new HashMap<Class<?>, Object>();
44
             = new HashMap<Class<?>, Object>();
41
 
45
 
42
-    /**
43
-     * Creates a new injector with no parent.
44
-     */
45
-    public SimpleInjector() {
46
-        this(null);
47
-    }
48
-
49
     /**
46
     /**
50
      * Creates a new injector which will inherit injection parameters from
47
      * Creates a new injector which will inherit injection parameters from
51
      * the specified parent. Parent graphs must be acyclic.
48
      * the specified parent. Parent graphs must be acyclic.
52
      *
49
      *
53
-     * @param parent The injector to inherit parameters from.
50
+     * @param parents The injectors to inherit parameters from.
54
      */
51
      */
55
-    public SimpleInjector(final SimpleInjector parent) {
56
-        this.parent = parent;
52
+    public SimpleInjector(final SimpleInjector ... parents) {
53
+        this.parents = parents;
57
     }
54
     }
58
 
55
 
59
     /**
56
     /**
93
 
90
 
94
     /**
91
     /**
95
      * Retrieves a mapping of known injectable types to their corresponding
92
      * Retrieves a mapping of known injectable types to their corresponding
96
-     * values. If this injector was created with a parent, this mapping will
97
-     * also include mappings defined in the parent.
93
+     * values. This mapping will also include mappings defined in all of this
94
+     * injector's parents.
98
      *
95
      *
99
      * @return A map of injectable parameters
96
      * @return A map of injectable parameters
100
      */
97
      */
102
         final Map<Class<?>, Object> localParams
99
         final Map<Class<?>, Object> localParams
103
                 = new HashMap<Class<?>, Object>(parameters.size());
100
                 = new HashMap<Class<?>, Object>(parameters.size());
104
 
101
 
105
-        if (parent != null) {
102
+        for (SimpleInjector parent : parents) {
106
             localParams.putAll(parent.getParameters());
103
             localParams.putAll(parent.getParameters());
107
         }
104
         }
108
 
105
 

Loading…
Отказ
Запис