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