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