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   *    https://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  import org.apache.directory.api.i18n.I18n;
27  
28  
29  /**
30   * Represents an LDAP MatchingRuleUseDescription defined in RFC 2252.
31   * <p>
32   * According to ldapbis [MODELS]:
33   * </p>
34   * 
35   * <pre>
36   *  Values of the matchingRuleUse list the attributes which are suitable
37   *  for use with an extensible matching rule.
38   * 
39   *    Matching rule use descriptions are written according to the following
40   *    ABNF:
41   * 
42   *      MatchingRuleUseDescription = LPAREN WSP
43   *          numericoid                ; object identifier
44   *          [ SP &quot;NAME&quot; SP qdescrs ]  ; short names (descriptors)
45   *          [ SP &quot;DESC&quot; SP qdstring ] ; description
46   *          [ SP &quot;OBSOLETE&quot; ]         ; not active
47   *          SP &quot;APPLIES&quot; SP oids      ; attribute types
48   *          extensions WSP RPAREN     ; extensions
49   * 
50   *    where:
51   *      [numericoid] is the object identifier of the matching rule
52   *          associated with this matching rule use description;
53   *      NAME [qdescrs] are short names (descriptors) identifying this
54   *          matching rule use;
55   *      DESC [qdstring] is a short descriptive string;
56   *      OBSOLETE indicates this matching rule use is not active;
57   *      APPLIES provides a list of attribute types the matching rule applies
58   *          to; and
59   *      [extensions] describe extensions.
60   * 
61   *  The matchingRule within the MatchingRuleUse definition can be used by an
62   *  extensible match assertion if the assertion is based on the attributes
63   *  listed within the MatchingRuleUse definition.  If an extensible match
64   *  assertion is based on attributes other than those listed within the
65   *  MatchingRuleUse definition then the assertion is deemed undefined.
66   * 
67   *  Also according to 3.3.20 of [SYNTAXES] (ldapbis working group):
68   * 
69   *  A value of the Matching Rule Use Description syntax indicates the
70   *  attribute types to which a matching rule may be applied in an
71   *  extensibleMatch search filter [PROT].  The LDAP-specific encoding of
72   *  a value of this syntax is defined by the &lt;MatchingRuleUseDescription&gt;
73   *  rule in [MODELS] above.
74   * </pre>
75   * 
76   * @see <a
77   *      href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-models-11.txt">ldapbis
78   *      [MODELS]</a>
79   * @see <a
80   *      href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-syntaxes-09.txt">ldapbis
81   *      [SYNTAXES]</a>
82   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
83   */
84  public class MatchingRuleUse extends AbstractSchemaObject
85  {
86      /** The mandatory serialVersionUID */
87      public static final long serialVersionUID = 1L;
88  
89      /** The list of attributes types OID the matching rule applies to */
90      private List<String> applicableAttributeOids;
91  
92      /** The list of attributes types the matching rule applies to */
93      private List<AttributeType> applicableAttributes;
94  
95  
96      /**
97       * Creates a new instance of MatchingRuleUseDescription
98       * 
99       * @param oid the MatchingRuleUse's OID
100      */
101     public MatchingRuleUse( String oid )
102     {
103         super( SchemaObjectType.MATCHING_RULE_USE, oid );
104 
105         applicableAttributeOids = new ArrayList<>();
106         applicableAttributes = new ArrayList<>();
107     }
108 
109 
110     /**
111      * @return The matchingRule's list of AttributeType OIDs the MRU applies to
112      */
113     public List<String> getApplicableAttributeOids()
114     {
115         return applicableAttributeOids;
116     }
117 
118 
119     /**
120      * @return The matchingRule's list of AttributeType OIDs the MRU applies to
121      */
122     public List<AttributeType> getApplicableAttributes()
123     {
124         return applicableAttributes;
125     }
126 
127 
128     /**
129      * Set the matchingRule's AttributeType OIDs the MRU applies to.
130      *
131      * @param applicableAttributeOids The AttributeType OIDs list
132      */
133     public void setApplicableAttributeOids( List<String> applicableAttributeOids )
134     {
135         if ( locked )
136         {
137             throw new UnsupportedOperationException( I18n.err( I18n.ERR_13700_CANNOT_MODIFY_LOCKED_SCHEMA_OBJECT, getName() ) );
138         }
139 
140         this.applicableAttributeOids = applicableAttributeOids;
141     }
142 
143 
144     /**
145      * Set the matchingRule's AttributeType the MRU applies to.
146      *
147      * @param applicableAttributes The AttributeType list
148      */
149     public void setApplicableAttributes( List<AttributeType> applicableAttributes )
150     {
151         if ( locked )
152         {
153             throw new UnsupportedOperationException( I18n.err( I18n.ERR_13700_CANNOT_MODIFY_LOCKED_SCHEMA_OBJECT, getName() ) );
154         }
155 
156         this.applicableAttributes = applicableAttributes;
157 
158         // update the OIDS now
159         applicableAttributeOids.clear();
160 
161         for ( AttributeType at : applicableAttributes )
162         {
163             applicableAttributeOids.add( at.getOid() );
164         }
165     }
166 
167 
168     /**
169      * Add a matchingRule's AttributeType OIDs the MRU applies to.
170      *
171      * @param oid A matchingRule's AttributeType OIDs the MRU applies to
172      */
173     public void addApplicableAttributeOids( String oid )
174     {
175         if ( locked )
176         {
177             throw new UnsupportedOperationException( I18n.err( I18n.ERR_13700_CANNOT_MODIFY_LOCKED_SCHEMA_OBJECT, getName() ) );
178         }
179 
180         if ( !applicableAttributeOids.contains( oid ) )
181         {
182             applicableAttributeOids.add( oid );
183         }
184     }
185 
186 
187     /**
188      * Add a matchingRule's AttributeType the MRU applies to.
189      *
190      * @param attributeType A matchingRule's AttributeType the MRU applies to
191      */
192     public void addApplicableAttribute( AttributeType attributeType )
193     {
194         if ( locked )
195         {
196             throw new UnsupportedOperationException( I18n.err( I18n.ERR_13700_CANNOT_MODIFY_LOCKED_SCHEMA_OBJECT, getName() ) );
197         }
198 
199         if ( !applicableAttributeOids.contains( attributeType.getOid() ) )
200         {
201             applicableAttributes.add( attributeType );
202             applicableAttributeOids.add( attributeType.getOid() );
203         }
204     }
205 
206 
207     /**
208      * @see Object#toString()
209      */
210     @Override
211     public String toString()
212     {
213         return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( this );
214     }
215 
216 
217     /**
218      * Copy an MatchingRuleUse
219      */
220     @Override
221     public MatchingRuleUse copy()
222     {
223         MatchingRuleUse copy = new MatchingRuleUse( oid );
224 
225         // Copy the SchemaObject common data
226         copy.copy( this );
227 
228         // Clone the APPLY AttributeTypes
229         copy.applicableAttributeOids = new ArrayList<>();
230 
231         // Copy the APPLIES oid list
232         for ( String oid : applicableAttributeOids )
233         {
234             copy.applicableAttributeOids.add( oid );
235         }
236 
237         // Copy the APPLIES list (will be empty)
238         copy.applicableAttributes = new ArrayList<>();
239 
240         return copy;
241     }
242 
243     
244     /**
245      * @see Object#equals(Object)
246      */
247     @Override
248     public int hashCode()
249     {
250         int hash = h;
251      
252         if ( applicableAttributeOids != null )
253         {
254             int tempHash = 0;
255             
256             for ( String oid : applicableAttributeOids )
257             {
258                 tempHash += oid.hashCode();
259             }
260             
261             hash = hash * 17 + tempHash;
262         }
263         
264         if ( applicableAttributes != null )
265         {
266             int tempHash = 0;
267 
268             for ( AttributeType attributeType : applicableAttributes )
269             {
270                 tempHash += attributeType.hashCode();
271             }
272 
273             hash = hash * 17 + tempHash;
274         }
275 
276         return hash;
277     }
278 
279     /**
280      * @see Object#equals(Object)
281      */
282     @Override
283     public boolean equals( Object o )
284     {
285         if ( !super.equals( o ) )
286         {
287             return false;
288         }
289 
290         if ( !( o instanceof MatchingRuleUse ) )
291         {
292             return false;
293         }
294 
295         @SuppressWarnings("unused")
296         MatchingRuleUse that = ( MatchingRuleUse ) o;
297 
298         // TODO : complete the checks
299         return true;
300     }
301 
302 
303     /**
304      * {@inheritDoc}
305      */
306     @Override
307     public void clear()
308     {
309         // Clear the common elements
310         super.clear();
311 
312         // Clear the references
313         applicableAttributes.clear();
314         applicableAttributeOids.clear();
315     }
316 }