|
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; |
|
33 |
|
34 import java.util.Collections; |
|
35 import java.util.HashMap; |
|
36 import java.util.Iterator; |
|
37 import java.util.Map; |
|
38 |
|
39 /** |
|
40 * $Id$ |
|
41 * |
|
42 * Created on 4/02/2006 |
|
43 * |
|
44 * Provides indexing of properties on a specific parameter. |
|
45 * @author Ben Fortuna |
|
46 */ |
|
47 public class IndexedPropertyList { |
|
48 |
|
49 private static final PropertyList EMPTY_LIST = new PropertyList(); |
|
50 |
|
51 private Map index; |
|
52 |
|
53 /** |
|
54 * Creates a new instance indexed on the parameters with the specified name. |
|
55 * @param list a list of properties |
|
56 * @param parameterName the name of parameters on which to index |
|
57 */ |
|
58 public IndexedPropertyList(final PropertyList list, final String parameterName) { |
|
59 final Map indexedProperties = new HashMap(); |
|
60 for (final Iterator i = list.iterator(); i.hasNext();) { |
|
61 final Property property = (Property) i.next(); |
|
62 for (final Iterator j = property.getParameters(parameterName).iterator(); j.hasNext();) { |
|
63 final Parameter parameter = (Parameter) j.next(); |
|
64 PropertyList properties = (PropertyList) indexedProperties.get(parameter.getValue()); |
|
65 if (properties == null) { |
|
66 properties = new PropertyList(); |
|
67 indexedProperties.put(parameter.getValue(), properties); |
|
68 } |
|
69 properties.add(property); |
|
70 } |
|
71 } |
|
72 this.index = Collections.unmodifiableMap(indexedProperties); |
|
73 } |
|
74 |
|
75 /** |
|
76 * Returns a list of properties containing a parameter with the |
|
77 * specified value. |
|
78 * @param paramValue the value of the parameter contained in the |
|
79 * returned properties |
|
80 * @return a property list |
|
81 */ |
|
82 public PropertyList getProperties(final String paramValue) { |
|
83 PropertyList properties = (PropertyList) index.get(paramValue); |
|
84 if (properties == null) { |
|
85 properties = EMPTY_LIST; |
|
86 } |
|
87 return properties; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Returns the first property containing a parameter with the specified |
|
92 * value. |
|
93 * @param paramValue the value of the parameter identified in the returned |
|
94 * property |
|
95 * @return a property or null if no property is found containing a parameter |
|
96 * with the specified value |
|
97 */ |
|
98 public Property getProperty(final String paramValue) { |
|
99 final PropertyList properties = getProperties(paramValue); |
|
100 if (!properties.isEmpty()) { |
|
101 return (Property) properties.iterator().next(); |
|
102 } |
|
103 return null; |
|
104 } |
|
105 } |