001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2016 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.config;
019
020import java.net.InetAddress;
021
022import org.forgerock.opendj.ldap.AddressMask;
023import org.forgerock.opendj.ldap.DN;
024import org.forgerock.opendj.ldap.schema.AttributeType;
025
026/**
027 * A visitor of property values, in the style of the visitor design pattern.
028 * Classes implementing this interface can query a property a value and its
029 * associated property definition in a type-safe manner when the kind of
030 * property value is unknown at compile time. When a visitor is passed to a
031 * property definition's accept method, the corresponding visit method most
032 * applicable to that property definition is invoked.
033 * <p>
034 * Each <code>visitXXX</code> method is provided with a default implementation
035 * which calls {@link #visitUnknown(PropertyDefinition, Object, Object)}.
036 * Sub-classes can override any or all of the methods to provide their own
037 * type-specific behavior.
038 *
039 * @param <R>
040 *            The return type of this visitor's methods. Use
041 *            {@link java.lang.Void} for visitors that do not need to return
042 *            results.
043 * @param <P>
044 *            The type of the additional parameter to this visitor's methods.
045 *            Use {@link java.lang.Void} for visitors that do not need an
046 *            additional parameter.
047 */
048public abstract class PropertyValueVisitor<R, P> {
049
050    /** Default constructor. */
051    protected PropertyValueVisitor() {
052        // No implementation required.
053    }
054
055    /**
056     * Visit a dseecompat ACI.
057     *
058     * @param pd
059     *            The dseecompat ACI property definition.
060     * @param v
061     *            The property value to visit.
062     * @param p
063     *            A visitor specified parameter.
064     * @return Returns a visitor specified result.
065     */
066    public R visitACI(ACIPropertyDefinition pd, String v, P p) {
067        return visitUnknown(pd, v, p);
068    }
069
070    /**
071     * Visit an aggregation property value.
072     *
073     * @param <C>
074     *            The type of client managed object configuration that this
075     *            aggregation property definition refers to.
076     * @param <S>
077     *            The type of server managed object configuration that this
078     *            aggregation property definition refers to.
079     * @param pd
080     *            The aggregation property definition to visit.
081     * @param v
082     *            The property value to visit.
083     * @param p
084     *            A visitor specified parameter.
085     * @return Returns a visitor specified result.
086     */
087    public <C extends ConfigurationClient, S extends Configuration> R visitAggregation(
088        AggregationPropertyDefinition<C, S> pd, String v, P p) {
089        return visitUnknown(pd, v, p);
090    }
091
092    /**
093     * Visit an attribute type.
094     *
095     * @param pd
096     *            The attribute type property definition.
097     * @param v
098     *            The property value to visit.
099     * @param p
100     *            A visitor specified parameter.
101     * @return Returns a visitor specified result.
102     */
103    public R visitAttributeType(AttributeTypePropertyDefinition pd, AttributeType v, P p) {
104        return visitUnknown(pd, v, p);
105    }
106
107    /**
108     * Visit a boolean.
109     *
110     * @param pd
111     *            The boolean property definition.
112     * @param v
113     *            The property value to visit.
114     * @param p
115     *            A visitor specified parameter.
116     * @return Returns a visitor specified result.
117     */
118    public R visitBoolean(BooleanPropertyDefinition pd, Boolean v, P p) {
119        return visitUnknown(pd, v, p);
120    }
121
122    /**
123     * Visit a class.
124     *
125     * @param pd
126     *            The class property definition.
127     * @param v
128     *            The property value to visit.
129     * @param p
130     *            A visitor specified parameter.
131     * @return Returns a visitor specified result.
132     */
133    public R visitClass(ClassPropertyDefinition pd, String v, P p) {
134        return visitUnknown(pd, v, p);
135    }
136
137    /**
138     * Visit a DN.
139     *
140     * @param pd
141     *            The DN property definition.
142     * @param v
143     *            The property value to visit.
144     * @param p
145     *            A visitor specified parameter.
146     * @return Returns a visitor specified result.
147     */
148    public R visitDN(DNPropertyDefinition pd, DN v, P p) {
149        return visitUnknown(pd, v, p);
150    }
151
152    /**
153     * Visit a duration.
154     *
155     * @param pd
156     *            The duration property definition.
157     * @param v
158     *            The property value to visit.
159     * @param p
160     *            A visitor specified parameter.
161     * @return Returns a visitor specified result.
162     */
163    public R visitDuration(DurationPropertyDefinition pd, Long v, P p) {
164        return visitUnknown(pd, v, p);
165    }
166
167    /**
168     * Visit an enumeration.
169     *
170     * @param <E>
171     *            The enumeration that should be used for values of the property
172     *            definition.
173     * @param pd
174     *            The enumeration property definition.
175     * @param v
176     *            The property value to visit.
177     * @param p
178     *            A visitor specified parameter.
179     * @return Returns a visitor specified result.
180     */
181    public <E extends Enum<E>> R visitEnum(EnumPropertyDefinition<E> pd, E v, P p) {
182        return visitUnknown(pd, v, p);
183    }
184
185    /**
186     * Visit an integer.
187     *
188     * @param pd
189     *            The integer property definition.
190     * @param v
191     *            The property value to visit.
192     * @param p
193     *            A visitor specified parameter.
194     * @return Returns a visitor specified result.
195     */
196    public R visitInteger(IntegerPropertyDefinition pd, Integer v, P p) {
197        return visitUnknown(pd, v, p);
198    }
199
200    /**
201     * Visit a IP address.
202     *
203     * @param pd
204     *            The IP address property definition.
205     * @param v
206     *            The property value to visit.
207     * @param p
208     *            A visitor specified parameter.
209     * @return Returns a visitor specified result.
210     */
211    public R visitIPAddress(IPAddressPropertyDefinition pd, InetAddress v, P p) {
212        return visitUnknown(pd, v, p);
213    }
214
215    /**
216     * Visit a IP address mask.
217     *
218     * @param pd
219     *            The IP address mask property definition.
220     * @param v
221     *            The property value to visit.
222     * @param p
223     *            A visitor specified parameter.
224     * @return Returns a visitor specified result.
225     */
226    public R visitIPAddressMask(IPAddressMaskPropertyDefinition pd, AddressMask v, P p) {
227        return visitUnknown(pd, v, p);
228    }
229
230    /**
231     * Visit a size.
232     *
233     * @param pd
234     *            The size property definition.
235     * @param v
236     *            The property value to visit.
237     * @param p
238     *            A visitor specified parameter.
239     * @return Returns a visitor specified result.
240     */
241    public R visitSize(SizePropertyDefinition pd, Long v, P p) {
242        return visitUnknown(pd, v, p);
243    }
244
245    /**
246     * Visit a string.
247     *
248     * @param pd
249     *            The string property definition.
250     * @param v
251     *            The property value to visit.
252     * @param p
253     *            A visitor specified parameter.
254     * @return Returns a visitor specified result.
255     */
256    public R visitString(StringPropertyDefinition pd, String v, P p) {
257        return visitUnknown(pd, v, p);
258    }
259
260    /**
261     * Visit an unknown type of property value. Implementations of this method
262     * can provide default behavior for unknown types of property.
263     * <p>
264     * The default implementation of this method throws an
265     * {@link PropertyException}. Sub-classes can override this
266     * method with their own default behavior.
267     *
268     * @param <T>
269     *            The type of property value to visit.
270     * @param pd
271     *            The property definition.
272     * @param v
273     *            The property value.
274     * @param p
275     *            A visitor specified parameter.
276     * @return Returns a visitor specified result.
277     * @throws PropertyException
278     *             Visitor implementations may optionally throw this exception.
279     */
280    public <T> R visitUnknown(PropertyDefinition<T> pd, T v, P p) {
281        throw PropertyException.unknownPropertyDefinitionException(pd);
282    }
283
284}