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 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
073 */
074public class MatchingRule extends AbstractSchemaObject
075{
076    /** The mandatory serialVersionUID */
077    public static final long serialVersionUID = 1L;
078
079    /** The associated Comparator */
080    protected LdapComparator<? super Object> ldapComparator;
081
082    /** The associated Normalizer */
083    protected Normalizer normalizer;
084
085    /** The associated LdapSyntax */
086    protected LdapSyntax ldapSyntax;
087
088    /** The associated LdapSyntax OID */
089    protected String ldapSyntaxOid;
090
091
092    /**
093     * Creates a new instance of MatchingRule.
094     *
095     * @param oid The MatchingRule OID
096     */
097    public MatchingRule( String oid )
098    {
099        super( SchemaObjectType.MATCHING_RULE, oid );
100    }
101
102
103    /**
104     * Gets the LdapSyntax used by this MatchingRule.
105     * 
106     * @return the LdapSyntax of this MatchingRule
107     */
108    public LdapSyntax getSyntax()
109    {
110        return ldapSyntax;
111    }
112
113
114    /**
115     * Gets the LdapSyntax OID used by this MatchingRule.
116     * 
117     * @return the LdapSyntax of this MatchingRule
118     */
119    public String getSyntaxOid()
120    {
121        return ldapSyntaxOid;
122    }
123
124
125    /**
126     * Gets the LdapComparator enabling the use of this MatchingRule for ORDERING
127     * and sorted indexing.
128     * 
129     * @return the ordering LdapComparator
130     */
131    public LdapComparator<? super Object> getLdapComparator()
132    {
133        return ldapComparator;
134    }
135
136
137    /**
138     * Gets the Normalizer enabling the use of this MatchingRule for EQUALITY
139     * matching and indexing.
140     * 
141     * @return the associated normalizer
142     */
143    public Normalizer getNormalizer()
144    {
145        return normalizer;
146    }
147
148
149    /**
150     * @see Object#toString()
151     */
152    @Override
153    public String toString()
154    {
155        return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( this );
156    }
157
158
159    /**
160     * Copy an MatchingRule
161     */
162    @Override
163    public MatchingRule copy()
164    {
165        MatchingRule copy = new MutableMatchingRule( oid );
166
167        // Copy the SchemaObject common data
168        copy.copy( this );
169
170        // All the references to other Registries object are set to null.
171        copy.ldapComparator = null;
172        copy.ldapSyntax = null;
173        copy.normalizer = null;
174
175        // Copy the syntax OID
176        copy.ldapSyntaxOid = ldapSyntaxOid;
177
178        return copy;
179    }
180
181
182    /**
183     * @see Object#equals(Object)
184     */
185    @Override
186    public boolean equals( Object o )
187    {
188        if ( !super.equals( o ) )
189        {
190            return false;
191        }
192
193        if ( !( o instanceof MatchingRule ) )
194        {
195            return false;
196        }
197
198        MatchingRule that = ( MatchingRule ) o;
199
200        // Check the Comparator
201        if ( ldapComparator != null )
202        {
203            if ( !ldapComparator.equals( that.ldapComparator ) )
204            {
205                return false;
206            }
207        }
208        else
209        {
210            if ( that.ldapComparator != null )
211            {
212                return false;
213            }
214        }
215
216        // Check the Normalizer
217        if ( normalizer != null )
218        {
219            if ( !normalizer.equals( that.normalizer ) )
220            {
221                return false;
222            }
223        }
224        else
225        {
226            if ( that.normalizer != null )
227            {
228                return false;
229            }
230        }
231
232        // Check the Syntax OID
233        if ( !compareOid( ldapSyntaxOid, that.ldapSyntaxOid ) )
234        {
235            return false;
236        }
237
238        // Check the Syntax
239        if ( ldapSyntax != null )
240        {
241            if ( !ldapSyntax.equals( that.ldapSyntax ) )
242            {
243                return false;
244            }
245        }
246        else
247        {
248            if ( that.ldapSyntax != null )
249            {
250                return false;
251            }
252        }
253
254        return true;
255    }
256}