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