Skip to content

Commit

Permalink
Added accessors for sub-components
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jan 6, 2022
1 parent 667785f commit 8d4f514
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 24 deletions.
Expand Up @@ -38,6 +38,7 @@
import net.fortuna.ical4j.validate.ValidationRule;
import net.fortuna.ical4j.validate.Validator;

import java.util.List;
import java.util.Optional;

import static net.fortuna.ical4j.model.Property.*;
Expand Down Expand Up @@ -164,6 +165,10 @@ public final void validate(final boolean recurse) throws ValidationException {
}
}

public <C extends Component> List<C> getComponents() {
return ComponentContainer.super.getComponents();
}

/**
*
* @return Returns the underlying component list.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/component/VEvent.java
Expand Up @@ -326,6 +326,18 @@ public final List<VAlarm> getAlarms() {
return getComponents();
}

public final List<Participant> getParticipants() {
return getComponents(Component.PARTICIPANT);
}

public final List<VLocation> getLocations() {
return getComponents(Component.VLOCATION);
}

public final List<VResource> getResources() {
return getComponents(Component.VRESOURCE);
}

/**
*
* @return Returns the underlying component list.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/component/VFreeBusy.java
Expand Up @@ -514,6 +514,18 @@ public void validate(Method method) throws ValidationException {
}
}

public final List<Participant> getParticipants() {
return getComponents(Component.PARTICIPANT);
}

public final List<VLocation> getLocations() {
return getComponents(Component.VLOCATION);
}

public final List<VResource> getResources() {
return getComponents(Component.VRESOURCE);
}

/**
*
* @return Returns the underlying component list.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/component/VJournal.java
Expand Up @@ -41,6 +41,7 @@

import java.time.temporal.Temporal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -191,6 +192,18 @@ public void validate(Method method) throws ValidationException {
}
}

public final List<Participant> getParticipants() {
return getComponents(Component.PARTICIPANT);
}

public final List<VLocation> getLocations() {
return getComponents(Component.VLOCATION);
}

public final List<VResource> getResources() {
return getComponents(Component.VRESOURCE);
}

/**
*
* @return Returns the underlying component list.
Expand Down
Expand Up @@ -189,8 +189,7 @@ public void validate(Method method) throws ValidationException {
* @return Returns the types.
*/
public final List<Observance> getObservances() {
return (List<Observance>) components.get(Observance.STANDARD,
Observance.DAYLIGHT);
return getComponents();
}

/**
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/net/fortuna/ical4j/model/component/VToDo.java
Expand Up @@ -42,10 +42,7 @@

import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

import static net.fortuna.ical4j.model.Property.*;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.*;
Expand Down Expand Up @@ -240,11 +237,21 @@ public VToDo(final Temporal start, final TemporalAmount duration, final String s
/**
* Returns the list of alarms for this todo.
* @return a component list
* @deprecated use {@link VToDo#getComponents()} to avoid potential {@link ClassCastException}
*/
@Deprecated
public final ComponentList<VAlarm> getAlarms() {
return (ComponentList<VAlarm>) components;
public final List<VAlarm> getAlarms() {
return getComponents(Component.VALARM);
}

public final List<Participant> getParticipants() {
return getComponents(Component.PARTICIPANT);
}

public final List<VLocation> getLocations() {
return getComponents(Component.VLOCATION);
}

public final List<VResource> getResources() {
return getComponents(Component.VRESOURCE);
}

/**
Expand Down
@@ -1,7 +1,6 @@
package net.fortuna.ical4j.validate.component;

import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.component.VAlarm;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.validate.ComponentValidator;
import net.fortuna.ical4j.validate.ContentValidator;
Expand All @@ -28,7 +27,7 @@ public void validate(VEvent target) throws ValidationException {
super.validate(target);

if (alarmsAllowed) {
target.getComponents(Component.VALARM).forEach(a -> ComponentValidator.VALARM_ITIP.validate((VAlarm) a));
target.getAlarms().forEach(a -> ComponentValidator.VALARM_ITIP.validate(a));
} else {
ContentValidator.assertNone(Component.VALARM, target.getAlarms(), false);
}
Expand Down
Expand Up @@ -2,10 +2,10 @@

import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.component.VAlarm;
import net.fortuna.ical4j.model.component.VToDo;
import net.fortuna.ical4j.model.property.Status;
import net.fortuna.ical4j.validate.ComponentValidator;
import net.fortuna.ical4j.validate.ContentValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationRule;

Expand Down Expand Up @@ -39,9 +39,9 @@ public void validate(VToDo target) throws ValidationException {
}

if (alarmsAllowed) {
target.getComponents(Component.VALARM).forEach(a -> ComponentValidator.VALARM_ITIP.validate((VAlarm) a));
target.getAlarms().forEach(a -> ComponentValidator.VALARM_ITIP.validate(a));
} else {
ComponentValidator.assertNone(Component.VALARM, target.getAlarms());
ContentValidator.assertNone(Component.VALARM, target.getAlarms(), false);
}
}
}
Expand Up @@ -21,9 +21,9 @@ class ParticipantFactoryTest extends Specification {
}

then: 'result is as expected'
participant.properties.uid[0].value == '1'
participant.getComponents().size() == 2
participant.getComponents()[0].properties.uid[0].value == '11'
participant.getComponents()[1].properties.uid[0].value == '12'
participant.getProperties('uid')[0].value == '1'
participant.components.size() == 2
participant.components[0].propertyList.uid[0].value == '11'
participant.components[1].propertyList.uid[0].value == '12'
}
}
Expand Up @@ -18,8 +18,8 @@ class VAvailabilityFactoryTest extends Specification {
}

then: 'result is as expected'
availability.properties.uid[0].value == '1'
availability.propertyList.uid[0].value == '1'
availability.getComponents().size() == 1
availability.getComponents()[0].properties.uid[0].value == '11'
availability.getComponents()[0].propertyList.uid[0].value == '11'
}
}
Expand Up @@ -15,6 +15,6 @@ class VLocationFactoryTest extends Specification {
}

then: 'result is as expected'
location.properties.uid[0].value == '1'
location.propertyList.uid[0].value == '1'
}
}
Expand Up @@ -15,6 +15,6 @@ class VResourceFactoryTest extends Specification {
}

then: 'result is as expected'
resource.properties.uid[0].value == '1'
resource.propertyList.uid[0].value == '1'
}
}
Expand Up @@ -97,7 +97,7 @@ class ByDayRuleTest extends Specification {
TemporalAdapter<Instant> to = TemporalAdapter.parse("20210107T070000Z")

Period period = new Period(from.temporal, to.temporal);
for (Component c : calendar.getComponents("VEVENT")) {
for (Component c : calendar.getComponents(Component.VEVENT)) {
PeriodList list = c.calculateRecurrenceSet(period);
for (Object po : list) {
System.out.println((Period) po);
Expand Down

0 comments on commit 8d4f514

Please sign in to comment.