This repository has been archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom Java predicates to be called from Fix. (#14)
- Loading branch information
1 parent
ee0e578
commit 01cac68
Showing
6 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
metafix/src/main/java/org/metafacture/metafix/api/FixPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022 hbz NRW | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.metafacture.metafix.api; | ||
|
||
import org.metafacture.metafix.Metafix; | ||
import org.metafacture.metafix.Record; | ||
import org.metafacture.metafix.Value; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
@FunctionalInterface | ||
public interface FixPredicate { | ||
|
||
BiPredicate<Stream<Value>, Predicate<Value>> ALL = Stream::allMatch; | ||
BiPredicate<Stream<Value>, Predicate<Value>> ANY = Stream::anyMatch; | ||
|
||
BiPredicate<String, String> CONTAINS = String::contains; | ||
BiPredicate<String, String> EQUALS = String::equals; | ||
BiPredicate<String, String> MATCHES = String::matches; | ||
|
||
boolean test(Metafix metafix, Record record, List<String> params, Map<String, String> options); | ||
|
||
default boolean testConditional(final Record record, final List<String> params, final BiPredicate<Stream<Value>, Predicate<Value>> qualifier, final BiPredicate<String, String> conditional) { | ||
final String field = params.get(0); | ||
final String string = params.get(1); | ||
|
||
final Value value = record.find(field); | ||
return value != null && qualifier.test(value.asList(null).asArray().stream(), v -> conditional.test(v.toString(), string)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
metafix/src/test/java/org/metafacture/metafix/util/TestPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2022 hbz NRW | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.metafacture.metafix.util; | ||
|
||
import org.metafacture.metafix.FixConditional; | ||
import org.metafacture.metafix.Metafix; | ||
import org.metafacture.metafix.Record; | ||
import org.metafacture.metafix.api.FixPredicate; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class TestPredicate implements FixPredicate { | ||
|
||
public TestPredicate() { | ||
} | ||
|
||
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) { | ||
return !FixConditional.exists.test(metafix, record, params, options) || | ||
FixConditional.any_equal.test(metafix, record, params, options); | ||
} | ||
|
||
} |