001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 */
026package org.forgerock.opendj.config;
027
028/**
029 * A default behavior provider which retrieves default values from a parent
030 * managed object. It should be used by properties which inherit their default
031 * value(s) from properties held in an other managed object.
032 *
033 * @param <T>
034 *            The type of values represented by this provider.
035 */
036public final class RelativeInheritedDefaultBehaviorProvider<T> extends DefaultBehaviorProvider<T> {
037
038    /** The type of managed object expected at the relative offset. */
039    private final AbstractManagedObjectDefinition<?, ?> d;
040
041    /**
042     * The relative offset (where 1 = parent, 2 = grandparent) of the
043     * managed object containing the property.
044     */
045    private final int offset;
046
047    /** The name of the property containing the inherited default values. */
048    private final String propertyName;
049
050    /**
051     * Create a relative inherited default behavior provider associated with a
052     * parent managed object.
053     *
054     * @param d
055     *            The type of parent managed object expected at the relative
056     *            location.
057     * @param propertyName
058     *            The name of the property containing the inherited default
059     *            values.
060     * @param offset
061     *            The relative location of the parent managed object (where 0 is
062     *            the managed object itself, 1 is the parent, and 2 is the
063     *            grand-parent).
064     * @throws IllegalArgumentException
065     *             If the offset is less than 0.
066     */
067    public RelativeInheritedDefaultBehaviorProvider(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
068        int offset) {
069        // We do not decode the property name now because the property
070        // might not have been constructed at this point (e.g. when the
071        // offset is 0).
072        if (offset < 0) {
073            throw new IllegalArgumentException("Negative offset");
074        }
075        this.d = d;
076        this.propertyName = propertyName;
077        this.offset = offset;
078    }
079
080    /** {@inheritDoc} */
081    public <R, P> R accept(DefaultBehaviorProviderVisitor<T, R, P> v, P p) {
082        return v.visitRelativeInherited(this, p);
083    }
084
085    /**
086     * Get the definition of the parent managed object containing the inherited
087     * default values.
088     *
089     * @return Returns the definition of the parent managed object containing
090     *         the inherited default values.
091     */
092    public AbstractManagedObjectDefinition<?, ?> getManagedObjectDefinition() {
093        return d;
094    }
095
096    /**
097     * Get the absolute path of the managed object containing the property which
098     * has the default values.
099     *
100     * @param path
101     *            The path of the current managed object from which the relative
102     *            path should be determined.
103     * @return Returns the absolute path of the managed object containing the
104     *         property which has the default values.
105     */
106    public ManagedObjectPath<?, ?> getManagedObjectPath(ManagedObjectPath<?, ?> path) {
107        return path.parent(offset);
108    }
109
110    /**
111     * Gets the name of the property containing the inherited default values.
112     *
113     * @return Returns the name of the property containing the inherited default
114     *         values.
115     */
116    public String getPropertyName() {
117        return propertyName;
118    }
119
120    /**
121     * Get the relative location of the parent managed object.
122     *
123     * @return Returns the relative location of the parent managed object (where
124     *         0 is the managed object itself, 1 is the parent, and 2 is the
125     *         grand-parent).
126     */
127    public int getRelativeOffset() {
128        return offset;
129    }
130}