src/net/fortuna/ical4j/util/PropertyValidator.java

changeset 0
fb9019fb1bf7
equal deleted inserted replaced
-1:000000000000 0:55a23654c448
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.util;
33
34 import net.fortuna.ical4j.model.PropertyList;
35 import net.fortuna.ical4j.model.ValidationException;
36
37 /**
38 * $Id$ [15-May-2004]
39 *
40 * Defines methods for validating properties and property lists.
41 *
42 * @author Ben Fortuna
43 */
44 public final class PropertyValidator {
45
46 private static final String ASSERT_NONE_MESSAGE = "Property [{0}] is not applicable";
47
48 private static final String ASSERT_ONE_OR_LESS_MESSAGE = "Property [{0}] must only be specified once";
49
50 private static final String ASSERT_ONE_MESSAGE = "Property [{0}] must be specified once";
51
52 private static final String ASSERT_ONE_OR_MORE_MESSAGE = "Property [{0}] must be specified at least once";
53
54 private static PropertyValidator instance = new PropertyValidator();
55
56 /**
57 * Constructor made private to enforce singleton.
58 */
59 private PropertyValidator() {
60 }
61
62 /**
63 * Ensure a property occurs no more than once.
64 *
65 * @param propertyName
66 * the property name
67 * @param properties
68 * a list of properties to query
69 * @throws ValidationException
70 * when the specified property occurs more than once
71 */
72 public void assertOneOrLess(final String propertyName,
73 final PropertyList properties) throws ValidationException {
74
75 if (properties.getProperties(propertyName).size() > 1) {
76 throw new ValidationException(ASSERT_ONE_OR_LESS_MESSAGE, new Object[] {propertyName});
77 }
78 }
79
80 /**
81 * Ensure a property occurs at least once.
82 *
83 * @param propertyName
84 * the property name
85 * @param properties
86 * a list of properties to query
87 * @throws ValidationException
88 * when the specified property occurs more than once
89 */
90 public void assertOneOrMore(final String propertyName,
91 final PropertyList properties) throws ValidationException {
92
93 if (properties.getProperties(propertyName).size() < 1) {
94 throw new ValidationException(ASSERT_ONE_OR_MORE_MESSAGE, new Object[] {propertyName});
95 }
96 }
97
98 /**
99 * Ensure a property occurs once.
100 *
101 * @param propertyName
102 * the property name
103 * @param properties
104 * a list of properties to query
105 * @throws ValidationException
106 * when the specified property does not occur once
107 */
108 public void assertOne(final String propertyName,
109 final PropertyList properties) throws ValidationException {
110
111 if (properties.getProperties(propertyName).size() != 1) {
112 throw new ValidationException(ASSERT_ONE_MESSAGE, new Object[] {propertyName});
113 }
114 }
115
116 /**
117 * Ensure a property doesn't occur in the specified list.
118 * @param propertyName the name of a property
119 * @param properties a list of properties
120 * @throws ValidationException thrown when the specified property
121 * is found in the list of properties
122 */
123 public void assertNone(final String propertyName, final PropertyList properties) throws ValidationException {
124 if (properties.getProperty(propertyName) != null) {
125 throw new ValidationException(ASSERT_NONE_MESSAGE, new Object[] {propertyName});
126 }
127 }
128
129 /**
130 * @return Returns the instance.
131 */
132 public static PropertyValidator getInstance() {
133 return instance;
134 }
135 }

mercurial