Skip to content

Commit

Permalink
Use optional as return type from ComponentList.getComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Mar 27, 2020
1 parent e0dd7b3 commit 6beda1b
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 81 deletions.
7 changes: 4 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/Calendar.java
Expand Up @@ -47,6 +47,7 @@
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

/**
* $Id$ [Apr 5, 2004]
Expand Down Expand Up @@ -144,7 +145,7 @@ public class Calendar implements Serializable {
* Default constructor.
*/
public Calendar() {
this(new PropertyList(), new ComponentList<CalendarComponent>());
this(new PropertyList(), new ComponentList<>());
}

/**
Expand Down Expand Up @@ -225,8 +226,8 @@ public final <C extends CalendarComponent> ComponentList<C> getComponents(final
* @param name name of the component to retrieve
* @return the first matching component in the component list with the specified name
*/
public final CalendarComponent getComponent(final String name) {
return getComponents().getComponent(name);
public final <T extends CalendarComponent> Optional<T> getComponent(final String name) {
return (Optional<T>) getComponents().getComponent(name);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/ComponentList.java
Expand Up @@ -36,6 +36,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -96,13 +97,13 @@ public final String toString() {
* @param aName name of component to return
* @return a component or null if no matching component found
*/
public final T getComponent(final String aName) {
public final Optional<T> getComponent(final String aName) {
for (final T c : this) {
if (c.getName().equals(aName)) {
return c;
return Optional.of(c);
}
}
return null;
return Optional.empty();
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/net/fortuna/ical4j/model/TimeZoneLoader.java
Expand Up @@ -108,13 +108,14 @@ public VTimeZone loadVTimeZone(String id) throws IOException, ParserException, P
try (InputStream in = resource.openStream()) {
final CalendarBuilder builder = new CalendarBuilder();
final Calendar calendar = builder.build(in);
final VTimeZone vTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE);
final Optional<VTimeZone> vTimeZone = calendar.getComponent(Component.VTIMEZONE);
// load any available updates for the timezone.. can be explicility enabled via configuration
if ("true".equals(Configurator.getProperty(UPDATE_ENABLED).orElse("false"))) {
return updateDefinition(vTimeZone);
if (vTimeZone.isPresent()
&& "true".equals(Configurator.getProperty(UPDATE_ENABLED).orElse("false"))) {
return updateDefinition(vTimeZone.get());
}
if (vTimeZone != null) {
cache.putIfAbsent(id, vTimeZone);
if (vTimeZone.isPresent()) {
cache.putIfAbsent(id, vTimeZone.get());
}
}
} else {
Expand Down Expand Up @@ -149,9 +150,9 @@ private VTimeZone updateDefinition(VTimeZone vTimeZone) throws IOException, Pars
final CalendarBuilder builder = new CalendarBuilder();

final Calendar calendar = builder.build(connection.getInputStream());
final VTimeZone updatedVTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE);
if (updatedVTimeZone != null) {
return updatedVTimeZone;
final Optional<VTimeZone> updatedVTimeZone = calendar.getComponent(Component.VTIMEZONE);
if (updatedVTimeZone.isPresent()) {
return updatedVTimeZone.get();
}
}
return vTimeZone;
Expand Down
Expand Up @@ -204,8 +204,8 @@ public final void validate(final boolean recurse)
/*
* ; one of 'standardc' or 'daylightc' MUST occur ..; and each MAY occur more than once. standardc / daylightc /
*/
if (getObservances().getComponent(Observance.STANDARD) == null
&& getObservances().getComponent(Observance.DAYLIGHT) == null) {
if (!getObservances().getComponent(Observance.STANDARD).isPresent()
&& !getObservances().getComponent(Observance.DAYLIGHT).isPresent()) {
throw new ValidationException("Sub-components ["
+ Observance.STANDARD + "," + Observance.DAYLIGHT
+ "] must be specified at least once");
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void validate(Calendar target) throws ValidationException {

// validate properties..
for (final Property property : target.getProperties()) {
boolean isCalendarProperty = calendarProperties.stream().filter(calProp -> calProp.isInstance(property)) != null;
boolean isCalendarProperty = calendarProperties.stream().anyMatch(calProp -> calProp.isInstance(property));

if (!(property instanceof XProperty) && !isCalendarProperty) {
throw new ValidationException("Invalid property: " + property.getName());
Expand Down Expand Up @@ -120,21 +120,21 @@ public static class PublishValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());

if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
}
else if (target.getComponent(Component.VFREEBUSY) != null) {
else if (target.getComponent(Component.VFREEBUSY).isPresent()) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
// ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
Expand All @@ -151,18 +151,18 @@ public static class RequestValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VFREEBUSY) != null) {
else if (target.getComponent(Component.VFREEBUSY).isPresent()) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
// ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
Expand All @@ -174,21 +174,21 @@ public static class ReplyValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VFREEBUSY) != null) {
else if (target.getComponent(Component.VFREEBUSY).isPresent()) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VALARM, target.getComponents());
Expand All @@ -203,17 +203,17 @@ public static class AddValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
else if (target.getComponent(Component.VJOURNAL) != null) {
else if (target.getComponent(Component.VJOURNAL).isPresent()) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
Expand All @@ -227,21 +227,21 @@ public static class CancelValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
else if (target.getComponent(Component.VJOURNAL) != null) {
else if (target.getComponent(Component.VJOURNAL).isPresent()) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
Expand All @@ -254,13 +254,13 @@ public static class RefreshValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
Expand All @@ -274,12 +274,12 @@ public static class CounterValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
Expand All @@ -293,14 +293,14 @@ public static class DeclineCounterValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
if (target.getComponent(Component.VEVENT).isPresent()) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
else if (target.getComponent(Component.VTODO).isPresent()) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
Expand Down
Expand Up @@ -89,7 +89,7 @@ public void validate(T target) throws ValidationException {
* @throws ValidationException where the assertion fails
*/
public static void assertNone(String componentName, ComponentList<?> components) throws ValidationException {
assertFalse(input -> input.getComponent(componentName) != null, ASSERT_NONE_MESSAGE, false,
assertFalse(input -> input.getComponent(componentName).isPresent(), ASSERT_NONE_MESSAGE, false,
components, componentName);
}

Expand Down
Expand Up @@ -39,6 +39,7 @@

import java.io.StringReader;
import java.net.URISyntaxException;
import java.util.Optional;

/**
* $Id: CalendarBuilderCustomRegistryTest.java [Nov 16, 2009]
Expand Down Expand Up @@ -118,8 +119,8 @@ public Parameter createParameter(final String value) throws URISyntaxException {

Calendar cal = builder.build(new StringReader(VEVENT_WITH_SCHEDULE_STATUS));

VEvent event = (VEvent)cal.getComponent(Component.VEVENT);
VEvent eventBis = (VEvent)event.copy();
Optional<VEvent> event = cal.getComponent(Component.VEVENT);
VEvent eventBis = event.get().copy();
assertEquals(eventBis, event);
}
}
4 changes: 2 additions & 2 deletions src/test/java/net/fortuna/ical4j/model/TimeZoneTest.java
Expand Up @@ -179,8 +179,8 @@ public TimeZoneTest(String testMethod, String vtimezoneDef, String zuluDateTimeS
String expectedLocalDateTimeStr) throws Exception {
super(testMethod);
net.fortuna.ical4j.model.Calendar cal = new CalendarBuilder().build(new StringReader(vtimezoneDef));
VTimeZone vtz = (VTimeZone) cal.getComponent(VTimeZone.VTIMEZONE);
this.timezone = new TimeZone(vtz);
Optional<VTimeZone> vtz = cal.getComponent(VTimeZone.VTIMEZONE);
this.timezone = new TimeZone(vtz.get());
this.zuluDateTimeStr = zuluDateTimeStr;
this.expectedLocalDateTimeStr = expectedLocalDateTimeStr;
}
Expand Down
Expand Up @@ -41,6 +41,7 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Optional;


/**
Expand All @@ -54,9 +55,9 @@ public void testVAvailability() throws ParserException, IOException
String availability = getVAvailabilityICal();
Reader reader = new StringReader(availability);
Calendar calendar = calendarBuilder.build(reader);
Component availabilityComponent = calendar.getComponent(Component.VAVAILABILITY);
Optional<VAvailability> availabilityComponent = calendar.getComponent(Component.VAVAILABILITY);
Assert.assertNotNull(availabilityComponent);
Assert.assertFalse(((VAvailability) availabilityComponent).getAvailable().isEmpty());
Assert.assertFalse(availabilityComponent.get().getAvailable().isEmpty());
String iCalString = calendar.toString();
Assert.assertTrue(iCalString.contains("BEGIN:AVAILABLE"));
Assert.assertEquals(iCalString.trim(), availability);
Expand Down

0 comments on commit 6beda1b

Please sign in to comment.