001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 * 
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 * 
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 * 
019 */
020package org.apache.directory.api.ldap.model.schema;
021
022
023
024
025/**
026 * A matchingRule definition. MatchingRules associate a comparator and a
027 * normalizer, forming the basic tools necessary to assert actions against
028 * attribute values. MatchingRules are associated with a specific Syntax for the
029 * purpose of resolving a normalized form and for comparisons.
030 * <p>
031 * According to ldapbis [MODELS]:
032 * </p>
033 * 
034 * <pre>
035 *  4.1.3. Matching Rules
036 * 
037 *    Matching rules are used by servers to compare attribute values against
038 *    assertion values when performing Search and Compare operations.  They
039 *    are also used to identify the value to be added or deleted when
040 *    modifying entries, and are used when comparing a purported
041 *    distinguished name with the name of an entry.
042 * 
043 *    A matching rule specifies the syntax of the assertion value.
044 * 
045 *    Each matching rule is identified by an object identifier (OID) and,
046 *    optionally, one or more short names (descriptors).
047 * 
048 *    Matching rule definitions are written according to the ABNF:
049 * 
050 *      MatchingRuleDescription = LPAREN WSP
051 *          numericoid                ; object identifier
052 *          [ SP &quot;NAME&quot; SP qdescrs ]  ; short names (descriptors)
053 *          [ SP &quot;DESC&quot; SP qdstring ] ; description
054 *          [ SP &quot;OBSOLETE&quot; ]         ; not active
055 *          SP &quot;SYNTAX&quot; SP numericoid ; assertion syntax
056 *          extensions WSP RPAREN     ; extensions
057 * 
058 *    where:
059 *      [numericoid] is object identifier assigned to this matching rule;
060 *      NAME [qdescrs] are short names (descriptors) identifying this
061 *          matching rule;
062 *      DESC [qdstring] is a short descriptive string;
063 *      OBSOLETE indicates this matching rule is not active;
064 *      SYNTAX identifies the assertion syntax by object identifier; and
065 *      [extensions] describe extensions.
066 * </pre>
067 * 
068 * @see <a href="http://www.faqs.org/rfcs/rfc2252.html">RFC 2252 Section 4.5</a>
069 * @see <a
070 *      href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-models-11.txt">ldapbis
071 *      [MODELS]</a>
072 * @see DescriptionUtils#getDescription(MatchingRule)
073 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
074 */
075public class MatchingRule extends AbstractSchemaObject
076{
077    /** The mandatory serialVersionUID */
078    public static final long serialVersionUID = 1L;
079
080    /** The associated Comparator */
081    protected LdapComparator<? super Object> ldapComparator;
082
083    /** The associated Normalizer */
084    protected Normalizer normalizer;
085
086    /** The associated LdapSyntax */
087    protected LdapSyntax ldapSyntax;
088
089    /** The associated LdapSyntax OID */
090    protected String ldapSyntaxOid;
091
092
093    /**
094     * Creates a new instance of MatchingRule.
095     *
096     * @param oid The MatchingRule OID
097     */
098    public MatchingRule( String oid )
099    {
100        super( SchemaObjectType.MATCHING_RULE, oid );
101    }
102
103
104    /**
105     * Gets the LdapSyntax used by this MatchingRule.
106     * 
107     * @return the LdapSyntax of this MatchingRule
108     */
109    public LdapSyntax getSyntax()
110    {
111        return ldapSyntax;
112    }
113
114
115    /**
116     * Gets the LdapSyntax OID used by this MatchingRule.
117     * 
118     * @return the LdapSyntax of this MatchingRule
119     */
120    public String getSyntaxOid()
121    {
122        return ldapSyntaxOid;
123    }
124
125
126    /**
127     * Gets the LdapComparator enabling the use of this MatchingRule for ORDERING
128     * and sorted indexing.
129     * 
130     * @return the ordering LdapComparator
131     */
132    public LdapComparator<? super Object> getLdapComparator()
133    {
134        return ldapComparator;
135    }
136
137
138    /**
139     * Gets the Normalizer enabling the use of this MatchingRule for EQUALITY
140     * matching and indexing.
141     * 
142     * @return the associated normalizer
143     */
144    public Normalizer getNormalizer()
145    {
146        return normalizer;
147    }
148
149
150    /**
151     * @see Object#toString()
152     */
153    public String toString()
154    {
155        return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( this );
156    }
157
158
159    /**
160     * Copy an MatchingRule
161     */
162    public MatchingRule copy()
163    {
164        MatchingRule copy = new MutableMatchingRule( oid );
165
166        // Copy the SchemaObject common data
167        copy.copy( this );
168
169        // All the references to other Registries object are set to null.
170        copy.ldapComparator = null;
171        copy.ldapSyntax = null;
172        copy.normalizer = null;
173
174        // Copy the syntax OID
175        copy.ldapSyntaxOid = ldapSyntaxOid;
176
177        return copy;
178    }
179
180
181    /**
182     * @see Object#equals()
183     */
184    @Override
185    public boolean equals( Object o )
186    {
187        if ( !super.equals( o ) )
188        {
189            return false;
190        }
191
192        if ( !( o instanceof MatchingRule ) )
193        {
194            return false;
195        }
196
197        MatchingRule that = ( MatchingRule ) o;
198
199        // Check the Comparator
200        if ( ldapComparator != null )
201        {
202            if ( !ldapComparator.equals( that.ldapComparator ) )
203            {
204                return false;
205            }
206        }
207        else
208        {
209            if ( that.ldapComparator != null )
210            {
211                return false;
212            }
213        }
214
215        // Check the Normalizer
216        if ( normalizer != null )
217        {
218            if ( !normalizer.equals( that.normalizer ) )
219            {
220                return false;
221            }
222        }
223        else
224        {
225            if ( that.normalizer != null )
226            {
227                return false;
228            }
229        }
230
231        // Check the Syntax OID
232        if ( !compareOid( ldapSyntaxOid, that.ldapSyntaxOid ) )
233        {
234            return false;
235        }
236
237        // Check the Syntax
238        if ( ldapSyntax != null )
239        {
240            if ( !ldapSyntax.equals( that.ldapSyntax ) )
241            {
242                return false;
243            }
244        }
245        else
246        {
247            if ( that.ldapSyntax != null )
248            {
249                return false;
250            }
251        }
252
253        return true;
254    }
255}