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-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS
026 */
027
028package org.opends.server.admin;
029
030
031
032/**
033 * A managed object composite relationship definition which represents
034 * a composition of an optional single managed object (i.e. the
035 * referenced managed object may or may not be present).
036 *
037 * @param <C>
038 *          The type of client managed object configuration that this
039 *          relation definition refers to.
040 * @param <S>
041 *          The type of server managed object configuration that this
042 *          relation definition refers to.
043 */
044public final class OptionalRelationDefinition
045    <C extends ConfigurationClient, S extends Configuration>
046    extends RelationDefinition<C, S> {
047
048  /**
049   * An interface for incrementally constructing optional relation
050   * definitions.
051   *
052   * @param <C>
053   *          The type of client managed object configuration that
054   *          this relation definition refers to.
055   * @param <S>
056   *          The type of server managed object configuration that
057   *          this relation definition refers to.
058   */
059  public static final class Builder
060      <C extends ConfigurationClient, S extends Configuration>
061      extends AbstractBuilder<C, S, OptionalRelationDefinition<C, S>> {
062
063    /**
064     * The optional default managed object associated with this
065     * optional relation.
066     */
067    private DefaultManagedObject<? extends C, ? extends S> defaultManagedObject;
068
069
070
071    /**
072     * Creates a new builder which can be used to incrementally build
073     * an optional relation definition.
074     *
075     * @param pd
076     *          The parent managed object definition.
077     * @param name
078     *          The name of the relation.
079     * @param cd
080     *          The child managed object definition.
081     */
082    public Builder(AbstractManagedObjectDefinition<?, ?> pd, String name,
083        AbstractManagedObjectDefinition<C, S> cd) {
084      super(pd, name, cd);
085    }
086
087
088
089    /**
090     * Sets the optional default managed object associated with this
091     * optional relation definition.
092     *
093     * @param defaultManagedObject
094     *          The default managed object or <code>null</code> if
095     *          there is no default managed object defined for this
096     *          relation definition.
097     */
098    public void setDefaultManagedObject(
099        DefaultManagedObject<? extends C, ? extends S> defaultManagedObject) {
100      this.defaultManagedObject = defaultManagedObject;
101    }
102
103
104
105    /** {@inheritDoc} */
106    @Override
107    protected OptionalRelationDefinition<C, S> buildInstance(
108        Common<C, S> common) {
109      return new OptionalRelationDefinition<>(common, defaultManagedObject);
110    }
111  }
112
113
114
115  /** The optional default managed object associated with this optional relation. */
116  private final DefaultManagedObject<? extends C, ? extends S> defaultManagedObject;
117
118
119
120  /** Private constructor. */
121  private OptionalRelationDefinition(Common<C, S> common,
122      DefaultManagedObject<? extends C, ? extends S> defaultManagedObject) {
123    super(common);
124    this.defaultManagedObject = defaultManagedObject;
125  }
126
127
128
129  /** {@inheritDoc} */
130  @Override
131  public <R, P> R accept(RelationDefinitionVisitor<R, P> v, P p) {
132    return v.visitOptional(this, p);
133  }
134
135
136
137  /**
138   * Gets the optional default managed object associated with this
139   * optional relation definition.
140   *
141   * @return Returns the default managed object or <code>null</code>
142   *         if there is no default managed object defined for this
143   *         relation definition.
144   */
145  public DefaultManagedObject<? extends C, ? extends S>
146      getDefaultManagedObject() {
147    return defaultManagedObject;
148  }
149
150
151
152  /** {@inheritDoc} */
153  @Override
154  public void toString(StringBuilder builder) {
155    builder.append("name=");
156    builder.append(getName());
157    builder.append(" type=optional parent=");
158    builder.append(getParentDefinition().getName());
159    builder.append(" child=");
160    builder.append(getChildDefinition().getName());
161  }
162
163
164
165  /** {@inheritDoc} */
166  @Override
167  protected void initialize() throws Exception {
168    if (defaultManagedObject != null) {
169      defaultManagedObject.initialize();
170    }
171  }
172
173}