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