Skip to content

Commit

Permalink
Improved backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Aug 22, 2021
1 parent 5cd2e73 commit 3a41831
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
Expand Up @@ -9,6 +9,8 @@

import java.util.*;

import static net.fortuna.ical4j.validate.ContentValidator.*;

/**
* Created by fortuna on 13/09/15.
*/
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/net/fortuna/ical4j/validate/ComponentValidator.java
Expand Up @@ -32,6 +32,7 @@
package net.fortuna.ical4j.validate;

import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.util.CompatibilityHints;

Expand Down Expand Up @@ -60,19 +61,41 @@ public void validate(T target) throws ValidationException {
if (rule.getPredicate().test(target)) {
switch (rule.getType()) {
case None:
rule.getInstances().forEach(s -> assertNone(s, target.getProperties().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertNone(s, target.getProperties().getAll(), warnOnly));
break;
case One:
rule.getInstances().forEach(s -> assertOne(s, target.getProperties().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertOne(s, target.getProperties().getAll(), warnOnly));
break;
case OneOrLess:
rule.getInstances().forEach(s -> assertOneOrLess(s, target.getProperties().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertOneOrLess(s, target.getProperties().getAll(), warnOnly));
break;
case OneOrMore:
rule.getInstances().forEach(s -> assertOneOrMore(s, target.getProperties().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertOneOrMore(s, target.getProperties().getAll(), warnOnly));
break;
}
}
}
}

/**
* @param componentName a component name used in the assertion
* @param components a list of components
* @throws ValidationException where the assertion fails
* @deprecated see {@link ContentValidator#assertNone(String, List, boolean)}
*/
@Deprecated
public static void assertNone(String componentName, ComponentList<?> components) throws ValidationException {
ContentValidator.assertNone(componentName, components.getAll(), false);
}

/**
* @param componentName a component name used in the assertion
* @param components a list of components
* @throws ValidationException where the assertion fails
* @deprecated see {@link ContentValidator#assertOneOrLess(String, List, boolean)}
*/
@Deprecated
public static void assertOneOrLess(String componentName, ComponentList<?> components) throws ValidationException {
ContentValidator.assertOneOrLess(componentName, components.getAll(), false);
}
}
Expand Up @@ -14,22 +14,22 @@ public interface ContentValidator<T extends Content> {

String ASSERT_ONE_OR_MORE_MESSAGE = "Content [{0}] must be specified at least once";

default void assertNone(final String name, final List<T> content, boolean warn) throws ValidationException {
static <T extends Content> void assertNone(final String name, final List<T> content, boolean warn) throws ValidationException {
Validator.assertFalse(input -> input.parallelStream().anyMatch(c -> c.getName().equals(name)),
ASSERT_NONE_MESSAGE, warn, content, name);
}

default void assertOne(final String name, final List<T> content, boolean warn) throws ValidationException {
static <T extends Content> void assertOne(final String name, final List<T> content, boolean warn) throws ValidationException {
Validator.assertFalse(input -> input.stream().filter(c -> c.getName().equals(name)).count() != 1,
ASSERT_ONE_MESSAGE, warn, content, name);
}

default void assertOneOrLess(final String name, final List<T> content, boolean warn) throws ValidationException {
static <T extends Content> void assertOneOrLess(final String name, final List<T> content, boolean warn) throws ValidationException {
Validator.assertFalse(input -> input.stream().filter(c -> c.getName().equals(name)).count() > 1,
ASSERT_ONE_OR_LESS_MESSAGE, warn, content, name);
}

default void assertOneOrMore(final String name, final List<T> content, boolean warn) throws ValidationException {
static <T extends Content> void assertOneOrMore(final String name, final List<T> content, boolean warn) throws ValidationException {
Validator.assertFalse(input -> input.stream().filter(c -> c.getName().equals(name)).count() < 1,
ASSERT_ONE_OR_MORE_MESSAGE, warn, content, name);
}
Expand Down
68 changes: 65 additions & 3 deletions src/main/java/net/fortuna/ical4j/validate/PropertyValidator.java
Expand Up @@ -33,6 +33,7 @@

import net.fortuna.ical4j.model.Parameter;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyList;
import net.fortuna.ical4j.util.CompatibilityHints;

import java.util.Arrays;
Expand Down Expand Up @@ -63,16 +64,77 @@ public void validate(T target) throws ValidationException {
if (rule.getPredicate().test(target)) {
switch (rule.getType()) {
case None:
rule.getInstances().forEach(s -> assertNone(s, target.getParameters().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertNone(s, target.getParameters().getAll(), warnOnly));
break;
case One:
rule.getInstances().forEach(s -> assertOne(s, target.getParameters().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertOne(s, target.getParameters().getAll(), warnOnly));
break;
case OneOrLess:
rule.getInstances().forEach(s -> assertOneOrLess(s, target.getParameters().getAll(), warnOnly));
rule.getInstances().forEach(s -> ContentValidator.assertOneOrLess(s, target.getParameters().getAll(), warnOnly));
break;
}
}
}
}

/**
* Ensure a property occurs no more than once.
*
* @param propertyName
* the property name
* @param properties
* a list of properties to query
* @throws ValidationException
* when the specified property occurs more than once
* @deprecated see {@link ContentValidator#assertOneOrLess(String, List, boolean)}
*/
@Deprecated
public static void assertOneOrLess(final String propertyName, final PropertyList properties) throws ValidationException {
ContentValidator.assertOneOrLess(propertyName, properties.getAll(), false);
}

/**
* Ensure a property occurs at least once.
*
* @param propertyName
* the property name
* @param properties
* a list of properties to query
* @throws ValidationException
* when the specified property occurs more than once
* @deprecated see {@link ContentValidator#assertOneOrMore(String, List, boolean)}
*/
@Deprecated
public static void assertOneOrMore(final String propertyName, final PropertyList properties) throws ValidationException {
ContentValidator.assertOneOrMore(propertyName, properties.getAll(), false);
}

/**
* Ensure a property occurs once.
*
* @param propertyName
* the property name
* @param properties
* a list of properties to query
* @throws ValidationException
* when the specified property does not occur once
* @deprecated see {@link ContentValidator#assertOne(String, List, boolean)}
*/
@Deprecated
public static void assertOne(final String propertyName, final PropertyList properties) throws ValidationException {
ContentValidator.assertOne(propertyName, properties.getAll(), false);
}

/**
* Ensure a property doesn't occur in the specified list.
* @param propertyName the name of a property
* @param properties a list of properties
* @throws ValidationException thrown when the specified property
* is found in the list of properties
* @deprecated see {@link ContentValidator#assertNone(String, List, boolean)}
*/
@Deprecated
public static void assertNone(final String propertyName, final PropertyList properties) throws ValidationException {
ContentValidator.assertNone(propertyName, properties.getAll(), false);
}
}

0 comments on commit 3a41831

Please sign in to comment.