Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e8676d
commit b665172
Showing
10 changed files
with
463 additions
and
29 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/net/fortuna/ical4j/validate/AbstractValidationRuleSet.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,65 @@ | ||
/* | ||
* Copyright (c) 2022, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.validate; | ||
|
||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Encapsulates a set of validation rules and context for the application of these rules. | ||
* | ||
* @param <T> target type for rule application | ||
*/ | ||
public abstract class AbstractValidationRuleSet<T> implements Serializable { | ||
|
||
protected final Set<ValidationRule> rules = new HashSet<>(); | ||
|
||
protected final String context; | ||
|
||
public AbstractValidationRuleSet(String context, ValidationRule...rules) { | ||
this.context = context; | ||
this.rules.addAll(Arrays.asList(rules)); | ||
} | ||
|
||
public abstract List<ValidationEntry> apply(T target); | ||
|
||
protected List<String> matches(List<String> instances, Predicate<String> matchPredicate) { | ||
return instances.stream().filter(matchPredicate).collect(Collectors.toList()); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/net/fortuna/ical4j/validate/ComponentContainerRuleSet.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,86 @@ | ||
/* | ||
* Copyright (c) 2022, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.validate; | ||
|
||
import net.fortuna.ical4j.model.ComponentContainer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ComponentContainerRuleSet extends AbstractValidationRuleSet<ComponentContainer<?>> { | ||
|
||
public ComponentContainerRuleSet(String context, ValidationRule... rules) { | ||
super(context, rules); | ||
} | ||
|
||
@Override | ||
public List<ValidationEntry> apply(ComponentContainer<?> target) { | ||
List<ValidationEntry> results = new ArrayList<>(); | ||
for (ValidationRule rule: rules) { | ||
List<String> matches = Collections.emptyList(); | ||
// only consider the specified instances in the total count.. | ||
int total = rule.getInstances().stream().mapToInt(s -> target.getComponents(s).size()).sum(); | ||
switch (rule.getType()) { | ||
case None: | ||
matches = matches(rule.getInstances(), s -> target.getComponents(s) != null); | ||
break; | ||
case One: | ||
matches = matches(rule.getInstances(), s -> target.getComponents(s).size() != 1); | ||
break; | ||
case OneOrLess: | ||
matches = matches(rule.getInstances(), s -> target.getComponents(s).size() > 1); | ||
break; | ||
case OneOrMore: | ||
matches = matches(rule.getInstances(), s -> target.getComponents(s).size() < 1); | ||
break; | ||
case OneExclusive: | ||
if (rule.getInstances().stream().anyMatch(s -> target.getComponents(s).size() > 0 | ||
&& target.getComponents(s).size() != total)) { | ||
matches = rule.getInstances(); | ||
} | ||
break; | ||
case AllOrNone: | ||
if (total > 0 && total != rule.getInstances().size()) { | ||
results.add(new ValidationEntry(rule, context)); | ||
} | ||
break; | ||
} | ||
if (!matches.isEmpty()) { | ||
results.add(new ValidationEntry(rule, context, matches.toArray(new String[0]))); | ||
} | ||
} | ||
return results; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/net/fortuna/ical4j/validate/PredicateRule.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,58 @@ | ||
/* | ||
* Copyright (c) 2022, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.validate; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class PredicateRule<T> extends ValidationRule { | ||
|
||
private final Predicate<T> predicate; | ||
|
||
private final String message; | ||
|
||
public PredicateRule(Predicate<T> predicate, String message, String... instances) { | ||
super(null, instances); | ||
this.predicate = predicate; | ||
this.message = message; | ||
} | ||
|
||
public Predicate<T> getPredicate() { | ||
return predicate; | ||
} | ||
|
||
@Override | ||
public String getMessage(String... instances) { | ||
return String.format(message, (Object[]) instances); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/net/fortuna/ical4j/validate/PropertyContainerRuleSet.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,93 @@ | ||
/* | ||
* Copyright (c) 2022, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.validate; | ||
|
||
import net.fortuna.ical4j.model.Property; | ||
import net.fortuna.ical4j.model.PropertyContainer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
public class PropertyContainerRuleSet extends AbstractValidationRuleSet<PropertyContainer> { | ||
|
||
public PropertyContainerRuleSet(String context, ValidationRule... rules) { | ||
super(context, rules); | ||
} | ||
|
||
@Override | ||
public List<ValidationEntry> apply(PropertyContainer target) { | ||
List<ValidationEntry> results = new ArrayList<>(); | ||
for (ValidationRule rule: rules) { | ||
List<String> matches = Collections.emptyList(); | ||
if (rule instanceof PredicateRule<?>) { | ||
Predicate<Property> predicate = ((PredicateRule<Property>) rule).getPredicate(); | ||
matches = matches(rule.getInstances(), s -> predicate.test(target.getProperty(s))); | ||
} else { | ||
int total = rule.getInstances().stream().mapToInt(s -> target.getProperties(s).size()).sum(); | ||
switch (rule.getType()) { | ||
case None: | ||
matches = matches(rule.getInstances(), s -> target.getProperty(s) != null); | ||
break; | ||
case One: | ||
matches = matches(rule.getInstances(), s -> target.getProperties(s).size() != 1); | ||
break; | ||
case OneOrLess: | ||
matches = matches(rule.getInstances(), s -> target.getProperties(s).size() > 1); | ||
break; | ||
case OneOrMore: | ||
matches = matches(rule.getInstances(), s -> target.getProperties(s).size() < 1); | ||
break; | ||
case OneExclusive: | ||
if (rule.getInstances().stream().anyMatch(s -> target.getProperties(s).size() > 0 | ||
&& target.getProperties(s).size() != total)) { | ||
matches = rule.getInstances(); | ||
} | ||
break; | ||
case AllOrNone: | ||
if (total > 0 && total != rule.getInstances().size()) { | ||
matches = rule.getInstances(); | ||
} | ||
break; | ||
} | ||
} | ||
if (!matches.isEmpty()) { | ||
results.add(new ValidationEntry(rule, context, matches.toArray(new String[0]))); | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/net/fortuna/ical4j/validate/PropertyRuleSet.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,85 @@ | ||
/* | ||
* Copyright (c) 2022, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.validate; | ||
|
||
import net.fortuna.ical4j.model.Property; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class PropertyRuleSet extends AbstractValidationRuleSet<Property> { | ||
|
||
public PropertyRuleSet(String context, ValidationRule... rules) { | ||
super(context, rules); | ||
} | ||
|
||
@Override | ||
public List<ValidationEntry> apply(Property target) { | ||
List<ValidationEntry> results = new ArrayList<>(); | ||
for (ValidationRule rule: rules) { | ||
List<String> matches = Collections.emptyList(); | ||
int total = rule.getInstances().stream().mapToInt(s -> target.getParameters(s).size()).sum(); | ||
switch (rule.getType()) { | ||
case None: | ||
matches = matches(rule.getInstances(), s -> target.getParameter(s) != null); | ||
break; | ||
case One: | ||
matches = matches(rule.getInstances(), s -> target.getParameters(s).size() != 1); | ||
break; | ||
case OneOrLess: | ||
matches = matches(rule.getInstances(), s -> target.getParameters(s).size() > 1); | ||
break; | ||
case OneOrMore: | ||
matches = matches(rule.getInstances(), s -> target.getParameters(s).size() < 1); | ||
break; | ||
case OneExclusive: | ||
if (rule.getInstances().stream().anyMatch(s -> target.getParameters(s).size() > 0 | ||
&& target.getParameters(s).size() != total)) { | ||
matches = rule.getInstances(); | ||
} | ||
break; | ||
case AllOrNone: | ||
if (total > 0 && total != rule.getInstances().size()) { | ||
results.add(new ValidationEntry(rule, context)); | ||
} | ||
break; | ||
} | ||
if (!matches.isEmpty()) { | ||
results.add(new ValidationEntry(rule, context, matches.toArray(new String[0]))); | ||
} | ||
} | ||
return results; | ||
} | ||
} |
Oops, something went wrong.