1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.api.engine;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.mo.ModelObjectComplete;
20
21 import java.io.Serializable;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26
27
28
29
30
31
32
33
34
35
36 public final class Facts implements ModelObjectComplete, Serializable {
37
38 private static final long serialVersionUID = -1448089944850300846L;
39
40
41
42
43 public static final Facts EMPTY_FACTS = new Facts(Builder.create());
44
45 private Map<Term, Object> factMap;
46
47 private Facts() {
48
49 }
50
51 private Facts(Builder b) {
52
53 this.factMap = new HashMap<Term, Object>(b.factMap);
54 }
55
56
57
58
59 public Map<Term, Object> getFactMap() {
60 return Collections.unmodifiableMap(factMap);
61 }
62
63 @Override
64 public boolean equals(Object o) {
65 if (this == o) {
66 return true;
67 }
68 if (o == null || getClass() != o.getClass()) {
69 return false;
70 }
71
72 final Facts facts = (Facts) o;
73
74 if (factMap != null ? !factMap.equals(facts.factMap) : facts.factMap != null) {
75 return false;
76 }
77
78 return true;
79 }
80
81 @Override
82 public int hashCode() {
83 return factMap != null ? factMap.hashCode() : 0;
84 }
85
86 @Override
87 public String toString() {
88 return "Facts{" + "factMap=" + factMap + '}';
89 }
90
91
92
93
94 public static class Builder {
95 private Map<Term, Object> factMap = new HashMap<Term, Object>();
96
97 private Builder() {
98
99 }
100
101
102
103
104
105 public static Builder create() {
106 return new Builder();
107 }
108
109
110
111
112
113
114
115 public Builder addFact(String termName, Map<String, String> termParameters, Object factValue) {
116 if (StringUtils.isEmpty(termName)) {
117 throw new IllegalArgumentException("termName must not be null or empty");
118 }
119 factMap.put(new Term(termName, termParameters), factValue);
120 return this;
121 }
122
123
124
125
126
127
128 public Builder addFact(String termName, Object factValue) {
129 addFact(termName, null, factValue);
130 return this;
131 }
132
133
134
135
136
137
138 public Builder addFact(Term term, Object factValue) {
139 if (term == null) {
140 throw new IllegalArgumentException("term must not be null");
141 }
142 factMap.put(term, factValue);
143 return this;
144 }
145
146
147
148
149
150 public Builder addFactsByTerm(Map<Term, Object> facts) {
151 if (facts != null) {
152 factMap.putAll(facts);
153 }
154 return this;
155 }
156
157
158
159
160
161 public Builder addFactsByName(Map<String, Object> facts) {
162 if (facts != null) {
163 for (Map.Entry<String, Object> entry : facts.entrySet()) {
164 factMap.put(new Term(entry.getKey()), entry.getValue());
165 }
166 }
167 return this;
168 }
169
170
171
172
173 public Facts build() {
174 if (factMap.isEmpty()) {
175 return EMPTY_FACTS;
176 } else {
177 return new Facts(this);
178 }
179 }
180
181 }
182
183
184 }