Skip to content

Commit

Permalink
Use varargs for improved backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Nov 15, 2019
1 parent 1e717ea commit 04c419a
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 111 deletions.
16 changes: 6 additions & 10 deletions src/main/java/net/fortuna/ical4j/model/component/VAlarm.java
Expand Up @@ -36,8 +36,6 @@
import net.fortuna.ical4j.validate.*;

import java.time.temporal.TemporalAmount;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -199,14 +197,12 @@ public class VAlarm extends CalendarComponent {

private final Map<Action, Validator> actionValidators = new HashMap<Action, Validator>();
{
actionValidators.put(Action.AUDIO, new ComponentValidator<VAlarm>(
Collections.singletonList(new ValidationRule(OneOrLess, ATTACH))));
actionValidators.put(Action.DISPLAY, new ComponentValidator<VAlarm>(
Collections.singletonList(new ValidationRule(One, DESCRIPTION))));
actionValidators.put(Action.EMAIL, new ComponentValidator<VAlarm>(
Arrays.asList(new ValidationRule(One, DESCRIPTION, SUMMARY), new ValidationRule(OneOrMore, ATTENDEE))));
actionValidators.put(Action.PROCEDURE, new ComponentValidator<VAlarm>(
Arrays.asList(new ValidationRule(One, ATTACH), new ValidationRule(OneOrLess, DESCRIPTION))));
actionValidators.put(Action.AUDIO, new ComponentValidator<VAlarm>(new ValidationRule(OneOrLess, ATTACH)));
actionValidators.put(Action.DISPLAY, new ComponentValidator<VAlarm>(new ValidationRule(One, DESCRIPTION)));
actionValidators.put(Action.EMAIL, new ComponentValidator<VAlarm>(new ValidationRule(One, DESCRIPTION, SUMMARY),
new ValidationRule(OneOrMore, ATTENDEE)));
actionValidators.put(Action.PROCEDURE, new ComponentValidator<VAlarm>(new ValidationRule(One, ATTACH),
new ValidationRule(OneOrLess, DESCRIPTION)));
}

/**
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/net/fortuna/ical4j/model/component/VEvent.java
Expand Up @@ -201,50 +201,43 @@ public class VEvent extends CalendarComponent {

private final Map<Method, Validator> methodValidators = new HashMap<Method, Validator>();
{
methodValidators.put(Method.ADD, new VEventValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, DTSTART, ORGANIZER, SEQUENCE, SUMMARY, UID),
methodValidators.put(Method.ADD, new VEventValidator(new ValidationRule(One, DTSTAMP, DTSTART, ORGANIZER, SEQUENCE, SUMMARY, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTEND, DURATION, GEO,
LAST_MODIFIED, LOCATION, PRIORITY, RESOURCES, STATUS, TRANSP, URL),
new ValidationRule(None, RECURRENCE_ID, REQUEST_STATUS))));
methodValidators.put(Method.CANCEL, new VEventValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, DTSTART, ORGANIZER, SEQUENCE, UID),
new ValidationRule(None, RECURRENCE_ID, REQUEST_STATUS)));
methodValidators.put(Method.CANCEL, new VEventValidator(false, new ValidationRule(One, DTSTAMP, DTSTART, ORGANIZER, SEQUENCE, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTEND, DTSTART, DURATION, GEO,
LAST_MODIFIED, LOCATION, PRIORITY, RECURRENCE_ID, RESOURCES, STATUS, SUMMARY, TRANSP, URL),
new ValidationRule(None, REQUEST_STATUS)), false));
methodValidators.put(Method.COUNTER, new VEventValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, DTSTART, SEQUENCE, SUMMARY, UID),
new ValidationRule(None, REQUEST_STATUS)));
methodValidators.put(Method.COUNTER, new VEventValidator(new ValidationRule(One, DTSTAMP, DTSTART, SEQUENCE, SUMMARY, UID),
new ValidationRule(One, true, ORGANIZER),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTEND, DURATION, GEO,
LAST_MODIFIED, LOCATION, PRIORITY, RECURRENCE_ID, RESOURCES, STATUS, TRANSP, URL))));
methodValidators.put(Method.DECLINE_COUNTER, new VEventValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, ORGANIZER, UID),
LAST_MODIFIED, LOCATION, PRIORITY, RECURRENCE_ID, RESOURCES, STATUS, TRANSP, URL)));
methodValidators.put(Method.DECLINE_COUNTER, new VEventValidator(false, new ValidationRule(One, DTSTAMP, ORGANIZER, UID),
new ValidationRule(OneOrLess, RECURRENCE_ID, SEQUENCE),
new ValidationRule(None, ATTACH, ATTENDEE, CATEGORIES, CLASS, CONTACT, CREATED, DESCRIPTION, DTEND,
DTSTART, DURATION, EXDATE, EXRULE, GEO, LAST_MODIFIED, LOCATION, PRIORITY, RDATE, RELATED_TO,
RESOURCES, RRULE, STATUS, SUMMARY, TRANSP, URL)), false));
methodValidators.put(Method.PUBLISH, new VEventValidator(Arrays.asList(
new ValidationRule(One, DTSTART, UID),
RESOURCES, RRULE, STATUS, SUMMARY, TRANSP, URL)));
methodValidators.put(Method.PUBLISH, new VEventValidator(new ValidationRule(One, DTSTART, UID),
new ValidationRule(One, true, DTSTAMP, ORGANIZER, SUMMARY),
new ValidationRule(OneOrLess, RECURRENCE_ID, SEQUENCE, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTEND,
DURATION, GEO, LAST_MODIFIED, LOCATION, PRIORITY, RESOURCES, STATUS, TRANSP, URL),
new ValidationRule(None, true, ATTENDEE),
new ValidationRule(None, REQUEST_STATUS))));
methodValidators.put(Method.REFRESH, new VEventValidator(Arrays.asList(
new ValidationRule(One, ATTENDEE, DTSTAMP, ORGANIZER, UID),
new ValidationRule(None, REQUEST_STATUS)));
methodValidators.put(Method.REFRESH, new VEventValidator(false, new ValidationRule(One, ATTENDEE, DTSTAMP, ORGANIZER, UID),
new ValidationRule(OneOrLess, RECURRENCE_ID),
new ValidationRule(None, ATTACH, CATEGORIES, CLASS, CONTACT, CREATED, DESCRIPTION, DTEND, DTSTART,
DURATION, EXDATE, EXRULE, GEO, LAST_MODIFIED, LOCATION, PRIORITY, RDATE, RELATED_TO,
REQUEST_STATUS, RESOURCES, RRULE, SEQUENCE, STATUS, SUMMARY, TRANSP, URL)), false));
methodValidators.put(Method.REPLY, new VEventValidator(Arrays.asList(
REQUEST_STATUS, RESOURCES, RRULE, SEQUENCE, STATUS, SUMMARY, TRANSP, URL)));
methodValidators.put(Method.REPLY, new VEventValidator(CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION),
new ValidationRule(One, ATTENDEE, DTSTAMP, ORGANIZER, UID),
new ValidationRule(OneOrLess, RECURRENCE_ID, SEQUENCE, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTEND,
DTSTART, DURATION, GEO, LAST_MODIFIED, LOCATION, PRIORITY, RESOURCES, STATUS, SUMMARY, TRANSP,
URL)), CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)));
methodValidators.put(Method.REQUEST, new VEventValidator(Arrays.asList(
new ValidationRule(OneOrMore, true, ATTENDEE),
URL)));
methodValidators.put(Method.REQUEST, new VEventValidator(new ValidationRule(OneOrMore, true, ATTENDEE),
new ValidationRule(One, DTSTAMP, DTSTART, ORGANIZER, SUMMARY, UID),
new ValidationRule(OneOrLess, SEQUENCE, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTEND, DURATION, GEO,
LAST_MODIFIED, LOCATION, PRIORITY, RECURRENCE_ID, RESOURCES, STATUS, TRANSP, URL))));
LAST_MODIFIED, LOCATION, PRIORITY, RECURRENCE_ID, RESOURCES, STATUS, TRANSP, URL)));
}

private ComponentList<VAlarm> alarms;
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/net/fortuna/ical4j/model/component/VFreeBusy.java
Expand Up @@ -207,19 +207,16 @@ public class VFreeBusy extends CalendarComponent {

private final Map<Method, Validator> methodValidators = new HashMap<Method, Validator>();
{
methodValidators.put(Method.PUBLISH, new ComponentValidator<VFreeBusy>(Arrays.asList(
new ValidationRule(OneOrMore, FREEBUSY),
methodValidators.put(Method.PUBLISH, new ComponentValidator<VFreeBusy>(new ValidationRule(OneOrMore, FREEBUSY),
new ValidationRule(One, DTSTAMP, DTSTART, DTEND, ORGANIZER, UID),
new ValidationRule(OneOrLess, URL),
new ValidationRule(None, ATTENDEE, DURATION, REQUEST_STATUS))));
methodValidators.put(Method.REPLY, new ComponentValidator(Arrays.asList(
new ValidationRule(One, ATTENDEE, DTSTAMP, DTEND, DTSTART, ORGANIZER, UID),
new ValidationRule(None, ATTENDEE, DURATION, REQUEST_STATUS)));
methodValidators.put(Method.REPLY, new ComponentValidator(new ValidationRule(One, ATTENDEE, DTSTAMP, DTEND, DTSTART, ORGANIZER, UID),
new ValidationRule(OneOrLess, URL),
new ValidationRule(None, DURATION, SEQUENCE))));
methodValidators.put(Method.REQUEST, new ComponentValidator(Arrays.asList(
new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(None, DURATION, SEQUENCE)));
methodValidators.put(Method.REQUEST, new ComponentValidator(new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(One, DTEND, DTSTAMP, DTSTART, ORGANIZER, UID),
new ValidationRule(None, FREEBUSY, DURATION, REQUEST_STATUS, URL))));
new ValidationRule(None, FREEBUSY, DURATION, REQUEST_STATUS, URL)));
}

/**
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/net/fortuna/ical4j/model/component/VJournal.java
Expand Up @@ -108,20 +108,17 @@ public class VJournal extends CalendarComponent {

private final Map<Method, Validator> methodValidators = new HashMap<Method, Validator>();
{
methodValidators.put(Method.ADD, new ComponentValidator<VJournal>(Arrays.asList(
new ValidationRule(One, DESCRIPTION, DTSTAMP, DTSTART, ORGANIZER, SEQUENCE, UID),
methodValidators.put(Method.ADD, new ComponentValidator<VJournal>(new ValidationRule(One, DESCRIPTION, DTSTAMP, DTSTART, ORGANIZER, SEQUENCE, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, LAST_MODIFIED, STATUS, SUMMARY, URL),
new ValidationRule(None, ATTENDEE, RECURRENCE_ID))));
methodValidators.put(Method.CANCEL, new ComponentValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, ORGANIZER, SEQUENCE, UID),
new ValidationRule(None, ATTENDEE, RECURRENCE_ID)));
methodValidators.put(Method.CANCEL, new ComponentValidator(new ValidationRule(One, DTSTAMP, ORGANIZER, SEQUENCE, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTSTART, LAST_MODIFIED,
RECURRENCE_ID, STATUS, SUMMARY, URL),
new ValidationRule(None, REQUEST_STATUS))));
methodValidators.put(Method.PUBLISH, new ComponentValidator(Arrays.asList(
new ValidationRule(One, DESCRIPTION, DTSTAMP, DTSTART, ORGANIZER, UID),
new ValidationRule(None, REQUEST_STATUS)));
methodValidators.put(Method.PUBLISH, new ComponentValidator(new ValidationRule(One, DESCRIPTION, DTSTAMP, DTSTART, ORGANIZER, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, LAST_MODIFIED, RECURRENCE_ID, SEQUENCE, STATUS,
SUMMARY, URL),
new ValidationRule(None, ATTENDEE))));
new ValidationRule(None, ATTENDEE)));
}

/**
Expand Down
Expand Up @@ -46,7 +46,6 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Collections;
import java.util.Objects;

/**
Expand Down Expand Up @@ -126,7 +125,7 @@ public class VTimeZone extends CalendarComponent {

private static final long serialVersionUID = 5629679741050917815L;

private final Validator itipValidator = new VTimeZoneValidator(Collections.emptyList());
private final Validator itipValidator = new VTimeZoneValidator();

private ComponentList<Observance> observances;

Expand Down
40 changes: 16 additions & 24 deletions src/main/java/net/fortuna/ical4j/model/component/VToDo.java
Expand Up @@ -127,52 +127,44 @@ public class VToDo extends CalendarComponent {

private final Map<Method, Validator> methodValidators = new HashMap<Method, Validator>();
{
methodValidators.put(Method.ADD, new VToDoValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, ORGANIZER, PRIORITY, SEQUENCE, SUMMARY, UID),
methodValidators.put(Method.ADD, new VToDoValidator(new ValidationRule(One, DTSTAMP, ORGANIZER, PRIORITY, SEQUENCE, SUMMARY, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTSTART, DUE, DURATION, GEO,
LAST_MODIFIED, LOCATION, PERCENT_COMPLETE, RESOURCES, STATUS, URL),
new ValidationRule(None, RECURRENCE_ID, REQUEST_STATUS))));
methodValidators.put(Method.CANCEL, new VToDoValidator(Arrays.asList(
new ValidationRule(One, UID, DTSTAMP, ORGANIZER, SEQUENCE),
new ValidationRule(None, RECURRENCE_ID, REQUEST_STATUS)));
methodValidators.put(Method.CANCEL, new VToDoValidator(false, new ValidationRule(One, UID, DTSTAMP, ORGANIZER, SEQUENCE),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTSTART, DUE, DURATION, GEO,
LAST_MODIFIED, LOCATION, PERCENT_COMPLETE, RECURRENCE_ID, RESOURCES, PRIORITY, STATUS, URL),
new ValidationRule(None, REQUEST_STATUS)), false));
methodValidators.put(Method.COUNTER, new VToDoValidator(Arrays.asList(
new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(None, REQUEST_STATUS)));
methodValidators.put(Method.COUNTER, new VToDoValidator(new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(One, DTSTAMP, ORGANIZER, PRIORITY, SUMMARY, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTSTART, DUE, DURATION, GEO,
LAST_MODIFIED, LOCATION, PERCENT_COMPLETE, RECURRENCE_ID, RESOURCES, RRULE, SEQUENCE, STATUS,
URL))));
methodValidators.put(Method.DECLINE_COUNTER, new VToDoValidator(Arrays.asList(
new ValidationRule(OneOrMore, ATTENDEE),
URL)));
methodValidators.put(Method.DECLINE_COUNTER, new VToDoValidator(false, new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(One, DTSTAMP, ORGANIZER, SEQUENCE, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTSTART, DUE, DURATION, GEO,
LAST_MODIFIED, LOCATION, LOCATION, PERCENT_COMPLETE, PRIORITY, RECURRENCE_ID, RESOURCES, STATUS,
URL)), false));
methodValidators.put(Method.PUBLISH, new VToDoValidator(Arrays.asList(
new ValidationRule(One, DTSTAMP, SUMMARY, UID),
URL)));
methodValidators.put(Method.PUBLISH, new VToDoValidator(new ValidationRule(One, DTSTAMP, SUMMARY, UID),
new ValidationRule(One, true, ORGANIZER, PRIORITY),
new ValidationRule(OneOrLess, DTSTART, SEQUENCE, CATEGORIES, CLASS, CREATED, DESCRIPTION, DUE, DURATION,
GEO, LAST_MODIFIED, LOCATION, PERCENT_COMPLETE, RECURRENCE_ID, RESOURCES, STATUS, URL),
new ValidationRule(None, ATTENDEE, REQUEST_STATUS))));
methodValidators.put(Method.REFRESH, new VToDoValidator(Arrays.asList(
new ValidationRule(One, ATTENDEE, DTSTAMP, UID),
new ValidationRule(None, ATTENDEE, REQUEST_STATUS)));
methodValidators.put(Method.REFRESH, new VToDoValidator(false, new ValidationRule(One, ATTENDEE, DTSTAMP, UID),
new ValidationRule(OneOrLess, RECURRENCE_ID),
new ValidationRule(None, ATTACH, CATEGORIES, CLASS, CONTACT, CREATED, DESCRIPTION, DTSTART, DUE,
DURATION, EXDATE, EXRULE, GEO, LAST_MODIFIED, LOCATION, ORGANIZER, PERCENT_COMPLETE, PRIORITY,
RDATE, RELATED_TO, REQUEST_STATUS, RESOURCES, RRULE, SEQUENCE, STATUS, URL)), false));
methodValidators.put(Method.REPLY, new VToDoValidator(Arrays.asList(
new ValidationRule(OneOrMore, ATTENDEE),
RDATE, RELATED_TO, REQUEST_STATUS, RESOURCES, RRULE, SEQUENCE, STATUS, URL)));
methodValidators.put(Method.REPLY, new VToDoValidator(false, new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(One, DTSTAMP, ORGANIZER, UID),
new ValidationRule(OneOrLess, CATEGORIES, CLASS, CREATED, DESCRIPTION, DTSTART, DUE, DURATION, GEO,
LAST_MODIFIED, LOCATION, PERCENT_COMPLETE, PRIORITY, RESOURCES, RECURRENCE_ID, SEQUENCE, STATUS,
SUMMARY, URL)), false));
methodValidators.put(Method.REQUEST, new VToDoValidator(Arrays.asList(
new ValidationRule(OneOrMore, ATTENDEE),
SUMMARY, URL)));
methodValidators.put(Method.REQUEST, new VToDoValidator(new ValidationRule(OneOrMore, ATTENDEE),
new ValidationRule(One, DTSTAMP, DTSTART, ORGANIZER, PRIORITY, SUMMARY, UID),
new ValidationRule(OneOrLess, SEQUENCE, CATEGORIES, CLASS, CREATED, DESCRIPTION, DUE, DURATION, GEO,
LAST_MODIFIED, LOCATION, PERCENT_COMPLETE, RECURRENCE_ID, RESOURCES, STATUS, URL),
new ValidationRule(None, REQUEST_STATUS))));
new ValidationRule(None, REQUEST_STATUS)));
}

private ComponentList<VAlarm> alarms = new ComponentList<>();
Expand Down
Expand Up @@ -8,6 +8,7 @@
import net.fortuna.ical4j.util.CompatibilityHints;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -20,8 +21,8 @@ public class CalendarValidatorImpl implements Validator<Calendar> {

private final List<ValidationRule> rules;

public CalendarValidatorImpl(List<ValidationRule> rules) {
this.rules = rules;
public CalendarValidatorImpl(ValidationRule... rules) {
this.rules = Arrays.asList(rules);

Collections.addAll(calendarProperties, CalScale.class, Method.class, ProdId.class, Version.class,
Uid.class, LastModified.class, Url.class, RefreshInterval.class, Source.class, Color.class,
Expand Down
Expand Up @@ -35,6 +35,7 @@
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.util.CompatibilityHints;

import java.util.Arrays;
import java.util.List;

import static net.fortuna.ical4j.validate.Validator.assertFalse;
Expand All @@ -51,8 +52,8 @@ public class ComponentValidator<T extends Component> implements Validator<T> {

private final List<ValidationRule> rules;

public ComponentValidator(List<ValidationRule> rules) {
this.rules = rules;
public ComponentValidator(ValidationRule... rules) {
this.rules = Arrays.asList(rules);
}

@Override
Expand Down
Expand Up @@ -2,8 +2,6 @@

import net.fortuna.ical4j.model.Calendar;

import java.util.Arrays;

import static net.fortuna.ical4j.model.Property.*;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.One;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.OneOrLess;
Expand All @@ -14,8 +12,7 @@
public class DefaultCalendarValidatorFactory implements CalendarValidatorFactory {
@Override
public Validator<Calendar> newInstance() {
return new CalendarValidatorImpl(Arrays.asList(
new ValidationRule(One, PRODID, VERSION),
new ValidationRule(OneOrLess, CALSCALE, METHOD)));
return new CalendarValidatorImpl(new ValidationRule(One, PRODID, VERSION),
new ValidationRule(OneOrLess, CALSCALE, METHOD));
}
}

0 comments on commit 04c419a

Please sign in to comment.