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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2008 Sun Microsystems, Inc.
026 */
027
028 package 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 */
044 public 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 // The optional default managed object associated with this
064 // optional relation.
065 private DefaultManagedObject<? extends C, ? extends S>
066 defaultManagedObject = null;
067
068
069
070 /**
071 * Creates a new builder which can be used to incrementally build
072 * an optional relation definition.
073 *
074 * @param pd
075 * The parent managed object definition.
076 * @param name
077 * The name of the relation.
078 * @param cd
079 * The child managed object definition.
080 */
081 public Builder(AbstractManagedObjectDefinition<?, ?> pd, String name,
082 AbstractManagedObjectDefinition<C, S> cd) {
083 super(pd, name, cd);
084 }
085
086
087
088 /**
089 * Sets the optional default managed object associated with this
090 * optional relation definition.
091 *
092 * @param defaultManagedObject
093 * The default managed object or <code>null</code> if
094 * there is no default managed object defined for this
095 * relation definition.
096 */
097 public void setDefaultManagedObject(
098 DefaultManagedObject<? extends C, ? extends S> defaultManagedObject) {
099 this.defaultManagedObject = defaultManagedObject;
100 }
101
102
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 protected OptionalRelationDefinition<C, S> buildInstance(
109 Common<C, S> common) {
110 return new OptionalRelationDefinition<C, S>(common, defaultManagedObject);
111 }
112
113 }
114
115
116
117 // The optional default managed object associated with this
118 // optional relation.
119 private final DefaultManagedObject<? extends C, ? extends S>
120 defaultManagedObject;
121
122
123
124 // Private constructor.
125 private OptionalRelationDefinition(Common<C, S> common,
126 DefaultManagedObject<? extends C, ? extends S> defaultManagedObject) {
127 super(common);
128 this.defaultManagedObject = defaultManagedObject;
129 }
130
131
132
133 /**
134 * {@inheritDoc}
135 */
136 @Override
137 public <R, P> R accept(RelationDefinitionVisitor<R, P> v, P p) {
138 return v.visitOptional(this, p);
139 }
140
141
142
143 /**
144 * Gets the optional default managed object associated with this
145 * optional relation definition.
146 *
147 * @return Returns the default managed object or <code>null</code>
148 * if there is no default managed object defined for this
149 * relation definition.
150 */
151 public DefaultManagedObject<? extends C, ? extends S>
152 getDefaultManagedObject() {
153 return defaultManagedObject;
154 }
155
156
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 public void toString(StringBuilder builder) {
163 builder.append("name=");
164 builder.append(getName());
165 builder.append(" type=composition parent=");
166 builder.append(getParentDefinition().getName());
167 builder.append(" child=");
168 builder.append(getChildDefinition().getName());
169 builder.append(" minOccurs=0 maxOccurs=1");
170 }
171
172
173
174 /**
175 * {@inheritDoc}
176 */
177 @Override
178 protected void initialize() throws Exception {
179 if (defaultManagedObject != null) {
180 defaultManagedObject.initialize();
181 }
182 }
183
184 }