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 Sun Microsystems, Inc.
015 */
016package org.forgerock.opendj.config;
017
018/**
019 * A default behavior provider which retrieves default values from a parent
020 * managed object. It should be used by properties which inherit their default
021 * value(s) from properties held in an other managed object.
022 *
023 * @param <T>
024 *            The type of values represented by this provider.
025 */
026public final class RelativeInheritedDefaultBehaviorProvider<T> extends DefaultBehaviorProvider<T> {
027
028    /** The type of managed object expected at the relative offset. */
029    private final AbstractManagedObjectDefinition<?, ?> d;
030
031    /**
032     * The relative offset (where 1 = parent, 2 = grandparent) of the
033     * managed object containing the property.
034     */
035    private final int offset;
036
037    /** The name of the property containing the inherited default values. */
038    private final String propertyName;
039
040    /**
041     * Create a relative inherited default behavior provider associated with a
042     * parent managed object.
043     *
044     * @param d
045     *            The type of parent managed object expected at the relative
046     *            location.
047     * @param propertyName
048     *            The name of the property containing the inherited default
049     *            values.
050     * @param offset
051     *            The relative location of the parent managed object (where 0 is
052     *            the managed object itself, 1 is the parent, and 2 is the
053     *            grand-parent).
054     * @throws IllegalArgumentException
055     *             If the offset is less than 0.
056     */
057    public RelativeInheritedDefaultBehaviorProvider(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
058        int offset) {
059        // We do not decode the property name now because the property
060        // might not have been constructed at this point (e.g. when the
061        // offset is 0).
062        if (offset < 0) {
063            throw new IllegalArgumentException("Negative offset");
064        }
065        this.d = d;
066        this.propertyName = propertyName;
067        this.offset = offset;
068    }
069
070    /** {@inheritDoc} */
071    public <R, P> R accept(DefaultBehaviorProviderVisitor<T, R, P> v, P p) {
072        return v.visitRelativeInherited(this, p);
073    }
074
075    /**
076     * Get the definition of the parent managed object containing the inherited
077     * default values.
078     *
079     * @return Returns the definition of the parent managed object containing
080     *         the inherited default values.
081     */
082    public AbstractManagedObjectDefinition<?, ?> getManagedObjectDefinition() {
083        return d;
084    }
085
086    /**
087     * Get the absolute path of the managed object containing the property which
088     * has the default values.
089     *
090     * @param path
091     *            The path of the current managed object from which the relative
092     *            path should be determined.
093     * @return Returns the absolute path of the managed object containing the
094     *         property which has the default values.
095     */
096    public ManagedObjectPath<?, ?> getManagedObjectPath(ManagedObjectPath<?, ?> path) {
097        return path.parent(offset);
098    }
099
100    /**
101     * Gets the name of the property containing the inherited default values.
102     *
103     * @return Returns the name of the property containing the inherited default
104     *         values.
105     */
106    public String getPropertyName() {
107        return propertyName;
108    }
109
110    /**
111     * Get the relative location of the parent managed object.
112     *
113     * @return Returns the relative location of the parent managed object (where
114     *         0 is the managed object itself, 1 is the parent, and 2 is the
115     *         grand-parent).
116     */
117    public int getRelativeOffset() {
118        return offset;
119    }
120}