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