Skip to content

Commit 725599e

Browse files
authored
Merge pull request #706 from metafacture/moveFixCommandEnumsToIndividualClasses
Move Fix command enums to individual classes.
2 parents aadbe15 + 45a833d commit 725599e

File tree

100 files changed

+4891
-664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+4891
-664
lines changed

README.md

Lines changed: 97 additions & 93 deletions
Large diffs are not rendered by default.

metafix/src/main/java/org/metafacture/metafix/FixBind.java

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,93 +17,39 @@
1717
package org.metafacture.metafix;
1818

1919
import org.metafacture.metafix.api.FixContext;
20+
import org.metafacture.metafix.bind.*; // checkstyle-disable-line AvoidStarImport
2021

21-
import java.util.HashMap;
22-
import java.util.HashSet;
2322
import java.util.List;
2423
import java.util.Map;
25-
import java.util.Set;
2624

25+
@Deprecated(since = "7.1.0", forRemoval = true)
2726
public enum FixBind implements FixContext {
2827

2928
list {
3029
@Override
3130
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
32-
final String scopeVariable = options.get("var");
33-
Value.asList(record.get(options.get("path")), a -> {
34-
for (int i = 0; i < a.size(); ++i) {
35-
final Value value = a.get(i);
36-
37-
// with var -> keep full record in scope, add the var:
38-
if (scopeVariable != null) {
39-
record.put(scopeVariable, value, false);
40-
recordTransformer.transform(record);
41-
record.remove(scopeVariable);
42-
}
43-
// w/o var -> use the currently bound value as the record:
44-
else {
45-
final int index = i;
46-
47-
value.matchType()
48-
.ifHash(h -> {
49-
final Record scopeRecord = new Record();
50-
scopeRecord.addAll(h);
51-
52-
recordTransformer.transform(scopeRecord);
53-
a.set(index, new Value(scopeRecord));
54-
})
55-
// TODO: bind to arrays (if that makes sense) and strings (access with '.')
56-
.orElseThrow();
57-
}
58-
}
59-
});
31+
new org.metafacture.metafix.bind.List().execute(metafix, record, params, options, recordTransformer);
6032
}
6133
},
6234

6335
list_as {
6436
@Override
6537
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
66-
final Map<String, Value.Array> lists = new HashMap<>();
67-
options.forEach((k, v) -> Value.asList(record.get(v), a -> lists.put(k, a)));
68-
69-
final int size = lists.values().stream().mapToInt(a -> a.size()).max().orElse(0);
70-
for (int i = 0; i < size; ++i) {
71-
final int index = i;
72-
73-
lists.forEach((k, v) -> {
74-
final Value value = index < v.size() ? v.get(index) : null;
75-
76-
if (value != null) {
77-
record.put(k, value);
78-
}
79-
else {
80-
record.remove(k);
81-
}
82-
});
83-
84-
recordTransformer.transform(record);
85-
}
86-
87-
lists.keySet().forEach(record::remove);
38+
new ListAs().execute(metafix, record, params, options, recordTransformer);
8839
}
8940
},
9041

9142
once {
92-
private final Map<Metafix, Set<String>> executed = new HashMap<>();
93-
9443
@Override
9544
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
96-
if (executed.computeIfAbsent(metafix, k -> new HashSet<>()).add(params.isEmpty() ? null : params.get(0))) {
97-
recordTransformer.transform(record);
98-
}
45+
new Once().execute(metafix, record, params, options, recordTransformer);
9946
}
10047
},
10148

10249
put_macro {
10350
@Override
10451
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
105-
recordTransformer.setVars(options);
106-
metafix.putMacro(params.get(0), recordTransformer);
52+
new PutMacro().execute(metafix, record, params, options, recordTransformer);
10753
}
10854
}
10955

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025 hbz NRW
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metafix;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
@Documented
26+
@Retention(RetentionPolicy.RUNTIME)
27+
@Target(ElementType.TYPE)
28+
public @interface FixCommand {
29+
30+
/**
31+
* Returns the Fix command name.
32+
*
33+
* @return Fix command name
34+
*/
35+
String value();
36+
37+
}

metafix/src/main/java/org/metafacture/metafix/FixConditional.java

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,183 +17,168 @@
1717
package org.metafacture.metafix;
1818

1919
import org.metafacture.metafix.api.FixPredicate;
20+
import org.metafacture.metafix.conditional.*; // checkstyle-disable-line AvoidStarImport
2021

2122
import java.util.List;
2223
import java.util.Map;
2324

25+
@Deprecated(since = "7.1.0", forRemoval = true) // checkstyle-disable-line ClassDataAbstractionCoupling|ClassFanOutComplexity
2426
public enum FixConditional implements FixPredicate {
2527

2628
all_contain {
2729
@Override
2830
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
29-
return testConditional(record, params, ALL, CONTAINS);
31+
return new AllContain().test(metafix, record, params, options);
3032
}
3133
},
3234
any_contain {
3335
@Override
3436
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
35-
return testConditional(record, params, ANY, CONTAINS);
37+
return new AnyContain().test(metafix, record, params, options);
3638
}
3739
},
3840
none_contain {
3941
@Override
4042
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
41-
return !any_contain.test(metafix, record, params, options);
43+
return new NoneContain().test(metafix, record, params, options);
4244
}
4345
},
4446
str_contain {
4547
@Override
4648
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
47-
return testConditional(params, CONTAINS);
49+
return new StrContain().test(metafix, record, params, options);
4850
}
4951
},
5052

5153
all_equal {
5254
@Override
5355
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
54-
return testConditional(record, params, ALL, EQUALS);
56+
return new AllEqual().test(metafix, record, params, options);
5557
}
5658
},
5759
any_equal {
5860
@Override
5961
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
60-
return testConditional(record, params, ANY, EQUALS);
62+
return new AnyEqual().test(metafix, record, params, options);
6163
}
6264
},
6365
none_equal {
6466
@Override
6567
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
66-
return !any_equal.test(metafix, record, params, options);
68+
return new NoneEqual().test(metafix, record, params, options);
6769
}
6870
},
6971
str_equal {
7072
@Override
7173
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
72-
return testConditional(params, EQUALS);
74+
return new StrEqual().test(metafix, record, params, options);
7375
}
7476
},
7577

7678
exists {
7779
@Override
7880
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
79-
return record.containsPath(params.get(0));
81+
return new Exists().test(metafix, record, params, options);
8082
}
8183
},
8284

8385
in {
8486
@Override
8587
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
86-
final Value value1 = record.get(params.get(0));
87-
final Value value2 = record.get(params.get(1));
88-
89-
return value1 != null && value2 != null && value1.<Boolean>extractType((m, c) -> m
90-
.ifArray(a1 -> value2.matchType()
91-
.ifArray(a2 -> c.accept(a1.equals(a2)))
92-
.orElse(v -> c.accept(false))
93-
)
94-
.ifHash(h1 -> value2.matchType()
95-
.ifHash(h2 -> c.accept(h1.equals(h2)))
96-
.orElse(v -> c.accept(false))
97-
)
98-
.ifString(s1 -> value2.matchType()
99-
.ifArray(a2 -> c.accept(a2.stream().anyMatch(value1::equals)))
100-
.ifHash(h2 -> c.accept(h2.containsField(s1)))
101-
.ifString(s2 -> c.accept(s1.equals(s2)))
102-
)
103-
);
88+
return new In().test(metafix, record, params, options);
10489
}
10590
},
10691
is_contained_in {
10792
@Override
10893
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
109-
return in.test(metafix, record, params, options);
94+
return new IsContainedIn().test(metafix, record, params, options);
11095
}
11196
},
11297

11398
is_array {
11499
@Override
115100
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
116-
return testConditional(record, params, Value::isArray);
101+
return new IsArray().test(metafix, record, params, options);
117102
}
118103
},
119104
is_empty {
120105
@Override
121106
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
122-
return testConditional(record, params, IS_EMPTY);
107+
return new IsEmpty().test(metafix, record, params, options);
123108
}
124109
},
125110
is_false {
126111
@Override
127112
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
128-
return testStringConditional(record, params, IS_FALSE); // TODO: strict=false
113+
return new IsFalse().test(metafix, record, params, options);
129114
}
130115
},
131116
is_hash {
132117
@Override
133118
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
134-
return is_object.test(metafix, record, params, options);
119+
return new IsHash().test(metafix, record, params, options);
135120
}
136121
},
137122
is_number {
138123
@Override
139124
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
140-
return testStringConditional(record, params, IS_NUMBER);
125+
return new IsNumber().test(metafix, record, params, options);
141126
}
142127
},
143128
is_object {
144129
@Override
145130
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
146-
return testConditional(record, params, Value::isHash);
131+
return new IsObject().test(metafix, record, params, options);
147132
}
148133
},
149134
is_string {
150135
@Override
151136
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
152-
return testConditional(record, params, Value::isString) && !is_number.test(metafix, record, params, options);
137+
return new IsString().test(metafix, record, params, options);
153138
}
154139
},
155140
is_true {
156141
@Override
157142
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
158-
return testStringConditional(record, params, IS_TRUE); // TODO: strict=false
143+
return new IsTrue().test(metafix, record, params, options);
159144
}
160145
},
161146

162147
all_match {
163148
@Override
164149
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
165-
return testConditional(record, params, ALL, MATCHES);
150+
return new AllMatch().test(metafix, record, params, options);
166151
}
167152
},
168153
any_match {
169154
@Override
170155
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
171-
return testConditional(record, params, ANY, MATCHES);
156+
return new AnyMatch().test(metafix, record, params, options);
172157
}
173158
},
174159
none_match {
175160
@Override
176161
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
177-
return !any_match.test(metafix, record, params, options);
162+
return new NoneMatch().test(metafix, record, params, options);
178163
}
179164
},
180165
str_match {
181166
@Override
182167
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
183-
return testConditional(params, MATCHES);
168+
return new StrMatch().test(metafix, record, params, options);
184169
}
185170
},
186171

187172
greater_than {
188173
@Override
189174
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
190-
return testConditional(record, params, ALL, GREATER_THAN);
175+
return new GreaterThan().test(metafix, record, params, options);
191176
}
192177
},
193178
less_than {
194179
@Override
195180
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
196-
return testConditional(record, params, ALL, LESS_THAN);
181+
return new LessThan().test(metafix, record, params, options);
197182
}
198183
}
199184

0 commit comments

Comments
 (0)