MentaContainer

Rev

Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 soliveira 1
package org.mentacontainer.impl;
2
 
3
import java.lang.reflect.Method;
4
import java.util.HashMap;
5
import java.util.Map;
6
import java.util.WeakHashMap;
7
 
8
import org.mentacontainer.util.InjectionUtils;
9
 
10
public class Dependency {
11
 
12
        private final String target;
13
 
14
        private String source;
15
 
16
        private final Class<? extends Object> klass;
17
 
18
        private Map<String, Method> cache = new HashMap<String, Method>();
19
 
20
        private Map<Object, Object> received = new WeakHashMap<Object, Object>();
21
 
22
        public Dependency(String target, Class<? extends Object> klass, String source) {
23
 
24
                this.klass = klass;
25
 
26
                this.target = target;
27
 
28
                this.source = source;
29
        }
30
 
31
        public Dependency(String target, Class<? extends Object> klass) {
32
 
33
                this(target, klass, target);
34
        }
35
 
36
        public String getTarget() {
37
 
38
                return target;
39
        }
40
 
41
        public String getSource() {
42
 
43
                return source;
44
        }
45
 
46
        public Class<? extends Object> getDependencyClass() {
47
 
48
                return klass;
49
        }
50
 
51
        public int hashCode() {
52
 
53
                return klass.hashCode() * 62 + target.hashCode() * 31 + source.hashCode();
54
        }
55
 
56
        public boolean equals(Object obj) {
57
 
58
                if (!(obj instanceof Dependency)) return false;
59
 
60
                Dependency d = (Dependency) obj;
61
 
62
                if (!d.klass.equals(this.klass)) return false;
63
 
64
                return true;
65
        }
66
 
67
    public Method getMethod(Class<? extends Object> targetClass) {
68
 
69
        String className = targetClass.getName();
70
 
71
        // first check cache...
72
 
73
        Method m = null;
74
 
75
        synchronized(cache) {
76
 
77
                m = cache.get(className);
78
 
79
        }
80
 
81
        if (m == null && cache.containsKey(className)) return null; // it is null...
82
 
83
        if (m != null) return m;
84
 
85
        m = InjectionUtils.findMethodToInject(targetClass, target, klass);
86
 
87
        if (m != null) {
88
 
89
                synchronized(cache) {
90
 
91
                        cache.put(className, m);
92
 
93
                }
94
 
95
            return m;
96
        }
97
 
98
        synchronized(cache) {
99
 
100
                // save null to indicate there is no method here... (so you don't do again to find null !!!
101
 
102
                cache.put(className, null);
103
 
104
        }
105
 
106
        return null;
107
    }
108
 
109
    /*
110
     * Check if this object has already received the dependency, because we don't want to
111
     * inject every time. Injecting just once is the correct behavior.
112
     */
113
    public boolean hasAlreadyReceived(Object target) {
114
 
115
        synchronized(received) {
116
 
117
            return received.containsKey(target);
118
 
119
        }
120
    }
121
 
122
    /*
123
     * Flag that this object has already received the dependency.
124
     */
125
    public void setAlreadyReceived(Object target) {
126
 
127
        synchronized(received) {
128
 
129
            received.put(target, null);
130
 
131
        }
132
    }
133
 
134
}