View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.model.schema;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  
27  /**
28   * An objectClass definition.
29   * <p>
30   * According to ldapbis [MODELS]:
31   * </p>
32   *
33   * <pre>
34   *  Object Class definitions are written according to the ABNF:
35   *
36   *    ObjectClassDescription = LPAREN WSP
37   *        numericoid                ; object identifier
38   *        [ SP &quot;NAME&quot; SP qdescrs ]  ; short names (descriptors)
39   *        [ SP &quot;DESC&quot; SP qdstring ] ; description
40   *        [ SP &quot;OBSOLETE&quot; ]         ; not active
41   *        [ SP &quot;SUP&quot; SP oids ]      ; superior object classes
42   *        [ SP kind ]               ; kind of class
43   *        [ SP &quot;MUST&quot; SP oids ]     ; attribute types
44   *        [ SP &quot;MAY&quot; SP oids ]      ; attribute types
45   *        extensions WSP RPAREN
46   *
47   *     kind = &quot;ABSTRACT&quot; / &quot;STRUCTURAL&quot; / &quot;AUXILIARY&quot;
48   *
49   *   where:
50   *     [numericoid] is object identifier assigned to this object class;
51   *     NAME [qdescrs] are short names (descriptors) identifying this object
52   *         class;
53   *     DESC [qdstring] is a short descriptive string;
54   *     OBSOLETE indicates this object class is not active;
55   *     SUP [oids] specifies the direct superclasses of this object class;
56   *     the kind of object class is indicated by one of ABSTRACT,
57   *         STRUCTURAL, or AUXILIARY, default is STRUCTURAL;
58   *     MUST and MAY specify the sets of required and allowed attribute
59   *         types, respectively; and
60   *    [extensions] describe extensions.
61   * </pre>
62   *
63   * @see <a href="http://www.faqs.org/rfcs/rfc2252.html">RFC2252 Section 4.4</a>
64   * @see <a
65   *      href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-models-11.txt">ldapbis
66   *      [MODELS]</a>
67   * @see DescriptionUtils#getDescription(ObjectClass)
68   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
69   */
70  public class ObjectClass extends AbstractSchemaObject
71  {
72      /** The mandatory serialVersionUID */
73      public static final long serialVersionUID = 1L;
74  
75      /** The ObjectClass type : ABSTRACT, AUXILIARY or STRUCTURAL */
76      protected ObjectClassTypeEnum objectClassType = ObjectClassTypeEnum.STRUCTURAL;
77  
78      /** The ObjectClass superior OIDs */
79      protected List<String> superiorOids;
80  
81      /** The ObjectClass superiors */
82      protected List<ObjectClass> superiors;
83  
84      /** The list of allowed AttributeType OIDs */
85      protected List<String> mayAttributeTypeOids;
86  
87      /** The list of allowed AttributeTypes */
88      protected List<AttributeType> mayAttributeTypes;
89  
90      /** The list of required AttributeType OIDs */
91      protected List<String> mustAttributeTypeOids;
92  
93      /** The list of required AttributeTypes */
94      protected List<AttributeType> mustAttributeTypes;
95  
96  
97      /**
98       * Creates a new instance of MatchingRuleUseDescription
99       * @param oid the OID for this objectClass
100      */
101     public ObjectClass( String oid )
102     {
103         super( SchemaObjectType.OBJECT_CLASS, oid );
104 
105         mayAttributeTypeOids = new ArrayList<String>();
106         mustAttributeTypeOids = new ArrayList<String>();
107         superiorOids = new ArrayList<String>();
108 
109         mayAttributeTypes = new ArrayList<AttributeType>();
110         mustAttributeTypes = new ArrayList<AttributeType>();
111         superiors = new ArrayList<ObjectClass>();
112         objectClassType = ObjectClassTypeEnum.STRUCTURAL;
113     }
114 
115 
116     /**
117      * @return the mayAttributeTypeOids
118      */
119     public List<String> getMayAttributeTypeOids()
120     {
121         return mayAttributeTypeOids;
122     }
123 
124 
125     /**
126      * @return the mayAttributeTypes
127      */
128     public List<AttributeType> getMayAttributeTypes()
129     {
130         return mayAttributeTypes;
131     }
132 
133 
134     /**
135      * @return the mustAttributeTypeOids
136      */
137     public List<String> getMustAttributeTypeOids()
138     {
139         return mustAttributeTypeOids;
140     }
141 
142 
143     /**
144      * @return the mustAttributeTypes
145      */
146     public List<AttributeType> getMustAttributeTypes()
147     {
148         return mustAttributeTypes;
149     }
150 
151 
152     /**
153      * Gets the superclasses of this ObjectClass.
154      *
155      * @return the superclasses
156      */
157     public List<ObjectClass> getSuperiors()
158     {
159         return superiors;
160     }
161 
162 
163     /**
164      * Gets the superclasses OIDsof this ObjectClass.
165      *
166      * @return the superclasses OIDs
167      */
168     public List<String> getSuperiorOids()
169     {
170         return superiorOids;
171     }
172 
173 
174     /**
175      * Gets the type of this ObjectClass as a type safe enum.
176      *
177      * @return the ObjectClass type as an enum
178      */
179     public ObjectClassTypeEnum getType()
180     {
181         return objectClassType;
182     }
183 
184 
185     /**
186      * Tells if the current ObjectClass is STRUCTURAL
187      *
188      * @return <code>true</code> if the ObjectClass is STRUCTURAL
189      */
190     public boolean isStructural()
191     {
192         return objectClassType == ObjectClassTypeEnum.STRUCTURAL;
193     }
194 
195 
196     /**
197      * Tells if the current ObjectClass is ABSTRACT
198      *
199      * @return <code>true</code> if the ObjectClass is ABSTRACT
200      */
201     public boolean isAbstract()
202     {
203         return objectClassType == ObjectClassTypeEnum.ABSTRACT;
204     }
205 
206 
207     /**
208      * Tells if the current ObjectClass is AUXILIARY
209      *
210      * @return <code>true</code> if the ObjectClass is AUXILIARY
211      */
212     public boolean isAuxiliary()
213     {
214         return objectClassType == ObjectClassTypeEnum.AUXILIARY;
215     }
216 
217 
218     /**
219      * {@inheritDoc}
220      */
221     public String toString()
222     {
223         return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( this );
224     }
225 
226 
227     /**
228      * Copy an ObjectClass
229      */
230     public ObjectClass copy()
231     {
232         ObjectClass copy = new ObjectClass( oid );
233 
234         // Copy the SchemaObject common data
235         copy.copy( this );
236 
237         // Copy the ObjectClass type
238         copy.objectClassType = objectClassType;
239 
240         // Copy the Superiors ObjectClasses OIDs
241         copy.superiorOids = new ArrayList<String>();
242 
243         for ( String oid : superiorOids )
244         {
245             copy.superiorOids.add( oid );
246         }
247 
248         // Copy the Superiors ObjectClasses ( will be empty )
249         copy.superiors = new ArrayList<ObjectClass>();
250 
251         // Copy the MAY AttributeTypes OIDs
252         copy.mayAttributeTypeOids = new ArrayList<String>();
253 
254         for ( String oid : mayAttributeTypeOids )
255         {
256             copy.mayAttributeTypeOids.add( oid );
257         }
258 
259         // Copy the MAY AttributeTypes ( will be empty )
260         copy.mayAttributeTypes = new ArrayList<AttributeType>();
261 
262         // Copy the MUST AttributeTypes OIDs
263         copy.mustAttributeTypeOids = new ArrayList<String>();
264 
265         for ( String oid : mustAttributeTypeOids )
266         {
267             copy.mustAttributeTypeOids.add( oid );
268         }
269 
270         // Copy the MUST AttributeTypes ( will be empty )
271         copy.mustAttributeTypes = new ArrayList<AttributeType>();
272 
273         return copy;
274     }
275 
276 
277     /**
278      * @see Object#equals(Object)
279      */
280     @Override
281     public boolean equals( Object o )
282     {
283         if ( !super.equals( o ) )
284         {
285             return false;
286         }
287 
288         if ( !( o instanceof ObjectClass ) )
289         {
290             return false;
291         }
292 
293         ObjectClass that = ( ObjectClass ) o;
294 
295         // The ObjectClassType
296         if ( objectClassType != that.objectClassType )
297         {
298             return false;
299         }
300 
301         // The Superiors OIDs
302         if ( superiorOids.size() != that.superiorOids.size() )
303         {
304             return false;
305         }
306 
307         // One way
308         for ( String oid : superiorOids )
309         {
310             if ( !that.superiorOids.contains( oid ) )
311             {
312                 return false;
313             }
314         }
315 
316         // The other way
317         for ( String oid : that.superiorOids )
318         {
319             if ( !superiorOids.contains( oid ) )
320             {
321                 return false;
322             }
323         }
324 
325         // The Superiors
326         if ( superiors.size() != that.superiors.size() )
327         {
328             return false;
329         }
330 
331         // One way
332         for ( ObjectClass oid : superiors )
333         {
334             if ( !that.superiors.contains( oid ) )
335             {
336                 return false;
337             }
338         }
339 
340         // The other way
341         for ( ObjectClass oid : that.superiors )
342         {
343             if ( !superiors.contains( oid ) )
344             {
345                 return false;
346             }
347         }
348 
349         // The MAY OIDs
350         if ( mayAttributeTypeOids.size() != that.mayAttributeTypeOids.size() )
351         {
352             return false;
353         }
354 
355         // One way
356         for ( String oid : mayAttributeTypeOids )
357         {
358             if ( !that.mayAttributeTypeOids.contains( oid ) )
359             {
360                 return false;
361             }
362         }
363 
364         // The other way
365         for ( String oid : that.mayAttributeTypeOids )
366         {
367             if ( !mayAttributeTypeOids.contains( oid ) )
368             {
369                 return false;
370             }
371         }
372 
373         // The MAY
374         if ( mayAttributeTypes.size() != that.mayAttributeTypes.size() )
375         {
376             return false;
377         }
378 
379         // One way
380         for ( AttributeType oid : mayAttributeTypes )
381         {
382             if ( !that.mayAttributeTypes.contains( oid ) )
383             {
384                 return false;
385             }
386         }
387 
388         // The other way
389         for ( AttributeType oid : that.mayAttributeTypes )
390         {
391             if ( !mayAttributeTypes.contains( oid ) )
392             {
393                 return false;
394             }
395         }
396 
397         // The MUST OIDs
398         if ( mustAttributeTypeOids.size() != that.mustAttributeTypeOids.size() )
399         {
400             return false;
401         }
402 
403         // One way
404         for ( String oid : mustAttributeTypeOids )
405         {
406             if ( !that.mustAttributeTypeOids.contains( oid ) )
407             {
408                 return false;
409             }
410         }
411 
412         // The other way
413         for ( String oid : that.mustAttributeTypeOids )
414         {
415             if ( !mustAttributeTypeOids.contains( oid ) )
416             {
417                 return false;
418             }
419         }
420 
421         // The MUST
422         if ( mustAttributeTypes.size() != that.mustAttributeTypes.size() )
423         {
424             return false;
425         }
426 
427         // One way
428         for ( AttributeType oid : mustAttributeTypes )
429         {
430             if ( !that.mustAttributeTypes.contains( oid ) )
431             {
432                 return false;
433             }
434         }
435 
436         // The other way
437         for ( AttributeType oid : that.mustAttributeTypes )
438         {
439             if ( !mustAttributeTypes.contains( oid ) )
440             {
441                 return false;
442             }
443         }
444 
445         return true;
446     }
447 }