Skip to content

Commit

Permalink
Return immutable lists when filtering components
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Sep 21, 2021
1 parent ad2a332 commit 3fa847b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
@@ -1,10 +1,12 @@
package net.fortuna.ical4j.model;

import java.util.List;

public interface ComponentContainer<T extends Component> {

ComponentList<T> getComponents();

default <C extends T> ComponentList<C> getComponents(final String name) {
default <C extends T> List<C> getComponents(final String name) {
return getComponents().getComponents(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.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -116,13 +117,13 @@ public final T getComponent(final String aName) {
* @return a list of components with the matching name
*/
@SuppressWarnings("unchecked")
public final <C extends T> ComponentList<C> getComponents(final String name) {
final ComponentList<C> components = new ComponentList<>();
public final <C extends T> List<C> getComponents(final String name) {
final List<C> components = new ArrayList<>();
for (final T c : this) {
if (c.getName().equals(name)) {
components.add((C) c);
}
}
return components;
return Collections.unmodifiableList(components);
}
}
25 changes: 11 additions & 14 deletions src/main/java/net/fortuna/ical4j/model/IndexedComponentList.java
Expand Up @@ -31,9 +31,7 @@
*/
package net.fortuna.ical4j.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* $Id$
Expand All @@ -45,27 +43,26 @@
*/
public class IndexedComponentList<T extends Component> {

private final ComponentList<T> EMPTY_LIST = new ComponentList<T>();

private Map<String, ComponentList<T>> index;
private Map<String, List<T>> index;

/**
* Creates a new instance indexed on properties with the specified name.
* @param list a list of components
* @param propertyName the name of the properties to index on
*/
public IndexedComponentList(final ComponentList<T> list, final String propertyName) {
final Map<String, ComponentList<T>> indexedComponents = new HashMap<String, ComponentList<T>>();
public IndexedComponentList(final List<T> list, final String propertyName) {
final Map<String, List<T>> indexedComponents = new HashMap<>();
for (final T component : list) {
for (final Property property : component.getProperties(propertyName)) {
ComponentList<T> components = indexedComponents.get(property.getValue());
List<T> components = indexedComponents.get(property.getValue());
if (components == null) {
components = new ComponentList<T>();
components = new ArrayList<>();
indexedComponents.put(property.getValue(), components);
}
components.add(component);
}
}
indexedComponents.keySet().forEach(p -> indexedComponents.put(p, Collections.unmodifiableList(indexedComponents.get(p))));
this.index = Collections.unmodifiableMap(indexedComponents);
}

Expand All @@ -76,10 +73,10 @@ public IndexedComponentList(final ComponentList<T> list, final String propertyNa
* returned components
* @return a component list
*/
public ComponentList<T> getComponents(final String propertyValue) {
ComponentList<T> components = index.get(propertyValue);
public List<T> getComponents(final String propertyValue) {
List<T> components = index.get(propertyValue);
if (components == null) {
components = EMPTY_LIST;
components = Collections.EMPTY_LIST;
}
return components;
}
Expand All @@ -93,7 +90,7 @@ public ComponentList<T> getComponents(final String propertyValue) {
* with the specified value
*/
public T getComponent(final String propertyValue) {
final ComponentList<T> components = getComponents(propertyValue);
final List<T> components = getComponents(propertyValue);
if (!components.isEmpty()) {
return components.get(0);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/fortuna/ical4j/model/TimeZone.java
Expand Up @@ -41,6 +41,7 @@

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* $Id$
Expand Down Expand Up @@ -157,7 +158,7 @@ public final void setRawOffset(final int offsetMillis) {
*/
@Override
public final boolean useDaylightTime() {
final ComponentList<Observance> daylights = vTimeZone.getObservances().getComponents(Observance.DAYLIGHT);
final List<Observance> daylights = vTimeZone.getObservances().getComponents(Observance.DAYLIGHT);
return (!daylights.isEmpty());
}

Expand All @@ -170,7 +171,7 @@ public final VTimeZone getVTimeZone() {

private static int getRawOffset(VTimeZone vt) {

ComponentList<Observance> seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD);
List<Observance> seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD);
// if no standard time use daylight time..
if (seasonalTimes.isEmpty()) {
seasonalTimes = vt.getObservances().getComponents(Observance.DAYLIGHT);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/fortuna/ical4j/util/Calendars.java
Expand Up @@ -47,6 +47,7 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -137,9 +138,8 @@ public static Calendar[] split(final Calendar calendar) {
return new Calendar[] {calendar};
}

final ComponentList<VTimeZone> timezoneList = calendar.getComponents(Component.VTIMEZONE);
final IndexedComponentList<VTimeZone> timezones = new IndexedComponentList<VTimeZone>(
timezoneList, Property.TZID);
final List<VTimeZone> timezoneList = calendar.getComponents(Component.VTIMEZONE);
final IndexedComponentList<VTimeZone> timezones = new IndexedComponentList<>(timezoneList, Property.TZID);

final Map<Uid, Calendar> calendars = new HashMap<Uid, Calendar>();
for (final CalendarComponent c : calendar.getComponents()) {
Expand Down
Expand Up @@ -34,7 +34,6 @@
import junit.framework.TestCase;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.component.VEvent;
Expand All @@ -45,6 +44,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;

/**
* $Id: CalendarBuilderTimezoneTest.java [Jul 1, 2008]
Expand Down Expand Up @@ -98,7 +98,7 @@ public void testVTimeZoneAfterVEvent() throws Exception {

calendar = builder.build(in);
assertNotNull("Calendar is null", calendar);
ComponentList<CalendarComponent> comps = calendar.getComponents(Component.VEVENT);
List<CalendarComponent> comps = calendar.getComponents(Component.VEVENT);
assertTrue("VEVENT not found", comps.size() == 1);
VEvent vevent = (VEvent) comps.get(0);

Expand Down Expand Up @@ -176,7 +176,7 @@ public void testTwoDaylights() throws IOException, ParserException {

calendar = builder.build(in);
assertNotNull("Calendar is null", calendar);
ComponentList<CalendarComponent> comps = calendar.getComponents(Component.VEVENT);
List<CalendarComponent> comps = calendar.getComponents(Component.VEVENT);
assertEquals("2 VEVENTs not found", 2, comps.size());
VEvent vevent0 = (VEvent) comps.get(0);

Expand Down

0 comments on commit 3fa847b

Please sign in to comment.