MentaContainer

Rev

Rev 5 | Details | Compare with Previous | 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
 
20 soliveira 7
import org.mentacontainer.Dependency;
2 soliveira 8
import org.mentacontainer.util.InjectionUtils;
9
 
20 soliveira 10
/**
11
 * A simple implementation of the Dependency interface.
12
 *
13
 * @author sergio.oliveira.jr@gmail.com
14
 */
15
public class MentaDependency implements Dependency {
2 soliveira 16
 
17
        private final String target;
18
 
19
        private String source;
20
 
21
        private final Class<? extends Object> klass;
22
 
23
        private Map<String, Method> cache = new HashMap<String, Method>();
24
 
20 soliveira 25
        public MentaDependency(String target, Class<? extends Object> klass, String source) {
2 soliveira 26
 
27
                this.klass = klass;
28
 
29
                this.target = target;
30
 
31
                this.source = source;
32
        }
33
 
20 soliveira 34
        public MentaDependency(String target, Class<? extends Object> klass) {
2 soliveira 35
 
36
                this(target, klass, target);
37
        }
38
 
39
        public String getTarget() {
40
 
41
                return target;
42
        }
43
 
44
        public String getSource() {
45
 
46
                return source;
47
        }
48
 
20 soliveira 49
        public Class<? extends Object> getType() {
2 soliveira 50
 
51
                return klass;
52
        }
53
 
54
        public int hashCode() {
55
 
56
                return klass.hashCode() * 62 + target.hashCode() * 31 + source.hashCode();
57
        }
58
 
59
        public boolean equals(Object obj) {
60
 
20 soliveira 61
                if (!(obj instanceof MentaDependency)) return false;
2 soliveira 62
 
20 soliveira 63
                MentaDependency d = (MentaDependency) obj;
2 soliveira 64
 
20 soliveira 65
                if (!d.target.equals(this.target)) return false;
66
 
2 soliveira 67
                if (!d.klass.equals(this.klass)) return false;
68
 
69
                return true;
70
        }
71
 
20 soliveira 72
    public Method check(Class<? extends Object> targetClass) {
2 soliveira 73
 
74
        String className = targetClass.getName();
75
 
76
        // first check cache...
77
 
78
        Method m = null;
79
 
80
        synchronized(cache) {
81
 
82
                m = cache.get(className);
83
 
84
        }
85
 
86
        if (m == null && cache.containsKey(className)) return null; // it is null...
87
 
88
        if (m != null) return m;
89
 
90
        m = InjectionUtils.findMethodToInject(targetClass, target, klass);
91
 
92
        if (m != null) {
93
 
94
                synchronized(cache) {
95
 
96
                        cache.put(className, m);
97
 
98
                }
99
 
100
            return m;
101
        }
102
 
103
        synchronized(cache) {
104
 
105
                // save null to indicate there is no method here... (so you don't do again to find null !!!
106
 
107
                cache.put(className, null);
108
 
109
        }
110
 
111
        return null;
112
    }
113
}