Tue, 10 Feb 2015 18:12:00 +0100
Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.
1 /**
2 * Copyright (c) 2012, Ben Fortuna
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * o Neither the name of Ben Fortuna nor the names of any other contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.fortuna.ical4j.model;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
39 /**
40 * $Id$
41 *
42 * Created on 4/02/2006
43 *
44 * Provides indexing of components on a specific property.
45 * @author Ben Fortuna
46 */
47 public class IndexedComponentList {
49 private static final ComponentList EMPTY_LIST = new ComponentList();
51 private Map index;
53 /**
54 * Creates a new instance indexed on properties with the specified name.
55 * @param list a list of components
56 * @param propertyName the name of the properties to index on
57 */
58 public IndexedComponentList(final ComponentList list, final String propertyName) {
59 final Map indexedComponents = new HashMap();
60 for (final Iterator i = list.iterator(); i.hasNext();) {
61 final Component component = (Component) i.next();
62 for (final Iterator j = component.getProperties(propertyName).iterator(); j.hasNext();) {
63 final Property property = (Property) j.next();
64 ComponentList components = (ComponentList) indexedComponents.get(property.getValue());
65 if (components == null) {
66 components = new ComponentList();
67 indexedComponents.put(property.getValue(), components);
68 }
69 components.add(component);
70 }
71 }
72 this.index = Collections.unmodifiableMap(indexedComponents);
73 }
75 /**
76 * Returns a list of components containing a property with the
77 * specified value.
78 * @param propertyValue the value of the property contained in the
79 * returned components
80 * @return a component list
81 */
82 public ComponentList getComponents(final String propertyValue) {
83 ComponentList components = (ComponentList) index.get(propertyValue);
84 if (components == null) {
85 components = EMPTY_LIST;
86 }
87 return components;
88 }
90 /**
91 * Returns the first component containing a property with the specified
92 * value.
93 * @param propertyValue the value of the property identified in the returned
94 * component
95 * @return a component or null if no component is found containing a property
96 * with the specified value
97 */
98 public Component getComponent(final String propertyValue) {
99 final ComponentList components = getComponents(propertyValue);
100 if (!components.isEmpty()) {
101 return (Component) components.iterator().next();
102 }
103 return null;
104 }
105 }