Skip to content

Commit

Permalink
Added support for method chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 26, 2020
1 parent 2ea2ea6 commit 9339d0e
Show file tree
Hide file tree
Showing 23 changed files with 239 additions and 172 deletions.
48 changes: 42 additions & 6 deletions src/main/java/net/fortuna/ical4j/model/Calendar.java
Expand Up @@ -212,16 +212,34 @@ protected void setComponents(ComponentList<CalendarComponent> components) {
this.components = components;
}

public void add(CalendarComponent component) {
/**
* Add a component to the calendar's component list.
* @param component the component to add
* @return a reference to the calendar to support method chaining
*/
public Calendar add(CalendarComponent component) {
setComponents((ComponentList<CalendarComponent>) components.add(component));
return this;
}

public void remove(CalendarComponent component) {
/**
* Remove a component from the calendar's component list.
* @param component the component to remove
* @return a reference to the calendar to support method chaining
*/
public Calendar remove(CalendarComponent component) {
setComponents((ComponentList<CalendarComponent>) components.remove(component));
return this;
}

public void replace(CalendarComponent component) {
/**
* Add a component to the calendar's component list whilst removing all other components with the same component name.
* @param component the component to add
* @return a reference to the calendar to support method chaining
*/
public Calendar replace(CalendarComponent component) {
setComponents((ComponentList<CalendarComponent>) components.replace(component));
return this;
}

/**
Expand Down Expand Up @@ -260,16 +278,34 @@ protected void setProperties(PropertyList properties) {
this.properties = properties;
}

public void add(Property property) {
/**
* Add a property to the calendar's property list.
* @param property the property to add
* @return a reference to the calendar to support method chaining
*/
public Calendar add(Property property) {
setProperties((PropertyList) properties.add(property));
return this;
}

public void remove(Property property) {
/**
* Remove a property from the calendar's property list.
* @param property the property to remove
* @return a reference to the calendar to support method chaining
*/
public Calendar remove(Property property) {
setProperties((PropertyList) properties.remove(property));
return this;
}

public void replace(Property property) {
/**
* Add a property to the calendar's property list whilst removing all other properties with the same property name.
* @param property the property to add
* @return a reference to the calendar to support method chaining
*/
public Calendar replace(Property property) {
setProperties((PropertyList) properties.replace(property));
return this;
}

/**
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/net/fortuna/ical4j/model/Component.java
Expand Up @@ -177,35 +177,47 @@ public final PropertyList getProperties() {
}

/**
* Add a property to the component.
* Add a property to the component's property list.
* @param property the property to add
* @return a reference to the component to support method chaining
*/
public void add(Property property) {
@SuppressWarnings("unchecked")
public <T extends Component> T add(Property property) {
setProperties((PropertyList) properties.add(property));
return (T) this;
}

/**
* Remove a property from the component.
* Remove a property from the component's property list.
* @param property the property to remove
* @return a reference to the component to support method chaining
*/
public void remove(Property property) {
@SuppressWarnings("unchecked")
public <T extends Component> T remove(Property property) {
setProperties((PropertyList) properties.remove(property));
return (T) this;
}

/**
* Remove multiple properties from the component.
* @param propertyName the name of properties to remove
* Remove all properties from the component's property list with the matching property name.
* @param propertyName name of the properties to remove
* @return a reference to the component to support method chaining
*/
public void removeAll(String... propertyName) {
@SuppressWarnings("unchecked")
public <T extends Component> T removeAll(String... propertyName) {
setProperties((PropertyList) properties.removeAll(propertyName));
return (T) this;
}

/**
* Replace existing properties with a new property.
* @param property the new property
* Add a property to the component's property list whilst removing all other properties with the same property name.
* @param property the property to add
* @return a reference to the component to support method chaining
*/
public void replace(Property property) {
@SuppressWarnings("unchecked")
public <T extends Component> T replace(Property property) {
setProperties((PropertyList) properties.replace(property));
return (T) this;
}

/**
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/Property.java
Expand Up @@ -443,20 +443,48 @@ protected void setParameters(ParameterList parameters) {
this.parameters = parameters;
}

public void add(Parameter parameter) {
/**
* Add a parameter to the property's parameter list.
* @param parameter the parameter to add
* @return a reference to the property to support method chaining
*/
@SuppressWarnings("unchecked")
public <T extends Property> T add(Parameter parameter) {
setParameters((ParameterList) parameters.add(parameter));
return (T) this;
}

public void remove(Parameter parameter) {
/**
* Remove a parameter from the property's parameter list.
* @param parameter the parameter to remove
* @return a reference to the property to support method chaining
*/
@SuppressWarnings("unchecked")
public <T extends Property> T remove(Parameter parameter) {
setParameters((ParameterList) parameters.remove(parameter));
return (T) this;
}

public void removeAll(String parameterName) {
/**
* Remove all parameters with the specified name from the property's parameter list.
* @param parameterName the name of parameters to remove
* @return a reference to the property to support method chaining
*/
@SuppressWarnings("unchecked")
public <T extends Property> T removeAll(String parameterName) {
setParameters((ParameterList) parameters.removeAll(parameterName));
return (T) this;
}

public void replace(Parameter parameter) {
/**
* Add a parameter to the property's parameter list whilst removing all other parameters with the same name.
* @param parameter the parameter to add
* @return a reference to the property to support method chaining
*/
@SuppressWarnings("unchecked")
public <T extends Property> T replace(Parameter parameter) {
setParameters((ParameterList) parameters.replace(parameter));
return (T) this;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/net/fortuna/ical4j/model/property/Action.java
Expand Up @@ -89,13 +89,15 @@ private ImmutableAction(final String value) {
}

@Override
public void add(Parameter parameter) {
public ImmutableAction add(Parameter parameter) {
throwException();
return null;
}

@Override
public void remove(Parameter parameter) {
public ImmutableAction remove(Parameter parameter) {
throwException();
return null;
}

/**
Expand All @@ -107,15 +109,16 @@ public void setValue(final String aValue) {
}

@Override
public void removeAll(String parameterName) {
public ImmutableAction removeAll(String parameterName) {
throwException();
return null;
}

@Override
public void replace(Parameter parameter) {
public ImmutableAction replace(Parameter parameter) {
throwException();
return null;
}

}

private String value;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/property/BusyType.java
Expand Up @@ -98,23 +98,27 @@ public void setValue(final String aValue) {
}

@Override
public void add(Parameter parameter) {
public ImmutableBusyType add(Parameter parameter) {
throwException();
return null;
}

@Override
public void remove(Parameter parameter) {
public ImmutableBusyType remove(Parameter parameter) {
throwException();
return null;
}

@Override
public void removeAll(String parameterName) {
public ImmutableBusyType removeAll(String parameterName) {
throwException();
return null;
}

@Override
public void replace(Parameter parameter) {
public ImmutableBusyType replace(Parameter parameter) {
throwException();
return null;
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/property/CalScale.java
Expand Up @@ -68,13 +68,15 @@ private ImmutableCalScale(final String value) {
}

@Override
public void add(Parameter parameter) {
public ImmutableCalScale add(Parameter parameter) {
throwException();
return null;
}

@Override
public void remove(Parameter parameter) {
public ImmutableCalScale remove(Parameter parameter) {
throwException();
return null;
}

/**
Expand All @@ -86,13 +88,15 @@ public void setValue(final String aValue) {
}

@Override
public void removeAll(String parameterName) {
public ImmutableCalScale removeAll(String parameterName) {
throwException();
return null;
}

@Override
public void replace(Parameter parameter) {
public ImmutableCalScale replace(Parameter parameter) {
throwException();
return null;
}

}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/property/Clazz.java
Expand Up @@ -123,13 +123,15 @@ private ImmutableClazz(final String value) {
}

@Override
public void add(Parameter parameter) {
public ImmutableClazz add(Parameter parameter) {
throwException();
return null;
}

@Override
public void remove(Parameter parameter) {
public ImmutableClazz remove(Parameter parameter) {
throwException();
return null;
}

/**
Expand All @@ -141,13 +143,15 @@ public void setValue(final String aValue) {
}

@Override
public void removeAll(String parameterName) {
public ImmutableClazz removeAll(String parameterName) {
throwException();
return null;
}

@Override
public void replace(Parameter parameter) {
public ImmutableClazz replace(Parameter parameter) {
throwException();
return null;
}
}

Expand Down
Expand Up @@ -56,7 +56,7 @@ public class Comment extends Property implements Escapable {

private String value;

private Validator<Comment> validator = new PropertyValidator<>(
private final Validator<Comment> validator = new PropertyValidator<>(
new ValidationRule<>(OneOrLess, ALTREP, LANGUAGE));

/**
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/property/Method.java
Expand Up @@ -111,23 +111,27 @@ public void setValue(final String aValue) {
}

@Override
public void add(Parameter parameter) {
public ImmutableMethod add(Parameter parameter) {
throwException();
return null;
}

@Override
public void remove(Parameter parameter) {
public ImmutableMethod remove(Parameter parameter) {
throwException();
return null;
}

@Override
public void removeAll(String parameterName) {
public ImmutableMethod removeAll(String parameterName) {
throwException();
return null;
}

@Override
public void replace(Parameter parameter) {
public ImmutableMethod replace(Parameter parameter) {
throwException();
return null;
}
}

Expand Down

0 comments on commit 9339d0e

Please sign in to comment.