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