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