1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.solver;
21
22 import com.google.common.base.Preconditions;
23 import org.grouplens.grapht.CachePolicy;
24 import org.grouplens.grapht.reflect.QualifierMatcher;
25 import org.grouplens.grapht.reflect.Qualifiers;
26 import org.grouplens.grapht.reflect.Satisfaction;
27
28 import java.util.EnumSet;
29
30
31
32
33
34
35
36 public class BindRuleBuilder {
37 private Class<?> dependencyType;
38 private QualifierMatcher qualifierMatcher = Qualifiers.matchAny();
39
40 private Satisfaction satisfaction;
41 private Class<?> implementation;
42
43 private CachePolicy cachePolicy = CachePolicy.NO_PREFERENCE;
44 private EnumSet<BindingFlag> flags = BindingFlag.emptySet();
45
46 public static BindRuleBuilder create() {
47 return new BindRuleBuilder();
48 }
49
50
51
52
53
54 public Class<?> getDependencyType() {
55 return dependencyType;
56 }
57
58
59
60
61
62 public BindRuleBuilder setDependencyType(Class<?> type) {
63 dependencyType = type;
64 return this;
65 }
66
67
68
69
70
71 public QualifierMatcher getQualifierMatcher() {
72 return qualifierMatcher;
73 }
74
75
76
77
78
79 public BindRuleBuilder setQualifierMatcher(QualifierMatcher qm) {
80 qualifierMatcher = qm;
81 return this;
82 }
83
84
85
86
87
88 public Satisfaction getSatisfaction() {
89 return satisfaction;
90 }
91
92
93
94
95
96
97
98 public BindRuleBuilder setSatisfaction(Satisfaction sat) {
99 satisfaction = sat;
100 return this;
101 }
102
103
104
105
106
107 public Class<?> getImplementation() {
108 return implementation;
109 }
110
111
112
113
114
115
116
117 public BindRuleBuilder setImplementation(Class<?> type) {
118 implementation = type;
119 return this;
120 }
121
122
123
124
125
126 public boolean isTerminal() {
127 return flags.contains(BindingFlag.TERMINAL);
128 }
129
130
131
132
133
134
135
136 public BindRuleBuilder setTerminal(boolean term) {
137 if (term) {
138 flags.add(BindingFlag.TERMINAL);
139 } else {
140 flags.remove(BindingFlag.TERMINAL);
141 }
142 return this;
143 }
144
145
146
147
148
149
150 public BindRuleBuilder setFlags(EnumSet<BindingFlag> fs) {
151 flags = fs.clone();
152 return this;
153 }
154
155
156
157
158
159
160 public BindRuleBuilder addFlag(BindingFlag flag) {
161 flags.add(flag);
162 return this;
163 }
164
165
166
167
168
169 public CachePolicy getCachePolicy() {
170 return cachePolicy;
171 }
172
173
174
175
176
177 public BindRuleBuilder setCachePolicy(CachePolicy policy) {
178 cachePolicy = policy;
179 return this;
180 }
181
182 public BindRule build() {
183 Preconditions.checkState(dependencyType != null, "no dependency type specified");
184 if (implementation != null) {
185 assert satisfaction == null;
186 return new BindRuleImpl(dependencyType, implementation, cachePolicy, qualifierMatcher, flags);
187 } else if (satisfaction != null) {
188 return new BindRuleImpl(dependencyType, satisfaction, cachePolicy, qualifierMatcher, flags);
189 } else {
190 throw new IllegalStateException("no binding target specified");
191 }
192 }
193 }