forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputFieldsCache.java
More file actions
150 lines (137 loc) · 4.98 KB
/
OutputFieldsCache.java
File metadata and controls
150 lines (137 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package act.data;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.App;
import act.data.util.StringOrPattern;
import act.util.ActContext;
import act.util.PropertySpec;
import org.osgl.$;
import org.osgl.util.C;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
class OutputFieldsCache {
// Key to index final output fields. Key includes:
// 1. excluded - developer declared excluded field list
// 2. outputs - developer declared output field list
// 3. component type - the type of the entity where field data get extracted
private class K {
Set<String> excluded;
List<String> outputs;
Class<?> componentType;
K(Set<String> ss, List<String> ls, Class<?> componentType) {
excluded = ss;
outputs = ls;
this.componentType = componentType;
}
@Override
public int hashCode() {
return $.hc(excluded, outputs, componentType);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof K) {
K that = (K) obj;
return $.eq(that.excluded, this.excluded)
&& $.eq(that.outputs, this.outputs)
&& $.eq(that.componentType, this.componentType);
}
return false;
}
}
private Map<K, List<String>> cache = C.newMap();
public List<String> getOutputFields(PropertySpec.MetaInfo spec, Class<?> componentClass, ActContext context) {
K k = new K(spec.excludedFields(context), spec.outputFields(context), componentClass);
List<String> outputs = cache.get(k);
if (null == outputs) {
outputs = calculateOutputs(k);
cache.put(k, outputs);
}
return outputs;
}
List<String> calculateOutputs(K k) {
Class<?> type = k.componentType;
if ($.isSimpleType(type) && k.excluded.isEmpty() && k.outputs.isEmpty()) {
return C.list();
}
C.List<StringOrPattern> outputs = C.newList();
boolean hasPattern = hasPattern(k.outputs, outputs);
Set<String> excluded = k.excluded;
if (hasPattern || outputs.isEmpty()) {
DataPropertyRepository repo = App.instance().service(DataPropertyRepository.class);
List<String> allFields = repo.propertyListOf(k.componentType);
if (!excluded.isEmpty()) {
List<String> finalOutputs;
List<StringOrPattern> lsp = C.newList();
boolean excludeHasPattern = hasPattern(excluded, lsp);
if (!excludeHasPattern) {
return C.list(allFields).without(excluded);
} else {
finalOutputs = C.newList(allFields);
outer:
for (String s : allFields) {
for (StringOrPattern sp: lsp) {
if (sp.matches(s)) {
finalOutputs.remove(s);
continue outer;
}
}
}
return finalOutputs;
}
} else {
if (outputs.isEmpty()) {
return allFields;
}
// excluded is empty and output fields has pattern
List<String> finalOutputs = C.newList();
for (StringOrPattern sp: outputs) {
if (sp.isPattern()) {
Pattern p = sp.p();
for (String s: allFields) {
if (p.matcher(s).matches()) {
finalOutputs.add(s);
}
}
} else {
finalOutputs.add(sp.s());
}
}
return finalOutputs;
}
} else {
return k.outputs;
}
}
private boolean hasPattern(Collection<String> ls, List<StringOrPattern> lsp) {
boolean b = false;
for (String s: ls) {
StringOrPattern sp = new StringOrPattern(s);
b = b || sp.isPattern();
lsp.add(sp);
}
return b;
}
}