1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.reflect;
21
22 import org.grouplens.grapht.*;
23
24 import javax.inject.Provider;
25 import java.lang.reflect.Type;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33
34
35
36
37
38 public class MockSatisfaction implements Satisfaction {
39 private static final long serialVersionUID = 1L;
40
41 private final Provider<?> provider;
42 private final Class<?> type;
43 private final List<Desire> dependencies;
44
45 public MockSatisfaction() {
46 this(Object.class, new Object(), new ArrayList<Desire>());
47 }
48
49 public MockSatisfaction(Class<?> type) {
50 this(type, new ArrayList<Desire>());
51 }
52
53 public MockSatisfaction(Class<?> type, List<Desire> dependencies) {
54 this(type, new NullProvider(), dependencies);
55 }
56
57 public MockSatisfaction(Class<?> type, Object instance, List<Desire> dependencies) {
58 this(type, new InstanceProvider(instance), dependencies);
59 }
60
61 public MockSatisfaction(Class<?> type, Provider<?> provider, List<Desire> dependencies) {
62 this.type = type;
63 this.provider = provider;
64 this.dependencies = dependencies;
65 }
66
67 @Override
68 public CachePolicy getDefaultCachePolicy() {
69 return CachePolicy.NO_PREFERENCE;
70 }
71
72 @Override
73 public List<Desire> getDependencies() {
74 return dependencies;
75 }
76
77 @Override
78 public Type getType() {
79 return type;
80 }
81
82 @Override
83 public Class<?> getErasedType() {
84 return type;
85 }
86
87 @Override
88 public boolean hasInstance() {
89 return true;
90 }
91
92 @Override
93 public <T> T visit(SatisfactionVisitor<T> visitor) {
94 throw new UnsupportedOperationException("cannot visit the mock satisfaction");
95 }
96
97 @Override
98 public Instantiator makeInstantiator(Map<Desire,Instantiator> dependencies,
99 LifecycleManager lm) {
100 return Instantiators.ofProvider(provider);
101 }
102
103 @Override
104 public String toString() {
105 StringBuilder sb = new StringBuilder();
106 return sb.append("mock satisfaction of ")
107 .append(type)
108 .append(" with provider ")
109 .append(provider)
110 .append(" (")
111 .append(dependencies.size())
112 .append(" dependencies)")
113 .toString();
114 }
115
116 @SuppressWarnings("rawtypes")
117 private static class NullProvider implements Provider {
118 @Override
119 public Object get() {
120 return null;
121 }
122
123 @Override
124 public String toString() {
125 return "return null";
126 }
127 }
128
129 @SuppressWarnings("rawtypes")
130 private static class InstanceProvider implements Provider {
131 private final Object instance;
132
133 public InstanceProvider(Object instance) {
134 this.instance = instance;
135 }
136
137 @Override
138 public Object get() {
139 return instance;
140 }
141
142 @Override
143 public String toString() {
144 return "return " + instance;
145 }
146 }
147 }