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.comparators;
21  
22  
23  import org.apache.directory.api.ldap.model.exception.LdapException;
24  import org.apache.directory.api.ldap.model.schema.LdapComparator;
25  import org.apache.directory.api.ldap.model.schema.Normalizer;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * A comparator which normalizes a value first before using a subordinate
32   * comparator to compare them.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class NormalizingComparator extends LdapComparator<String>
37  {
38      /** The serial version UID */
39      private static final long serialVersionUID = 2L;
40  
41      /** A logger for this class */
42      private static final Logger LOG = LoggerFactory.getLogger( NormalizingComparator.class );
43  
44      /** the Normalizer to normalize values with before comparing */
45      private Normalizer normalizer;
46  
47      /** the underlying comparator to use for comparisons */
48      private LdapComparator<String> comparator;
49  
50      private boolean onServer = false;
51  
52  
53      /**
54       * A comparator which normalizes a value first before comparing them.
55       * 
56       * @param oid The Comparator's OID
57       * @param normalizer the Normalizer to normalize values with before comparing
58       * @param comparator the underlying comparator to use for comparisons
59       */
60      public NormalizingComparator( String oid, Normalizer normalizer, LdapComparator<String> comparator )
61      {
62          super( oid );
63          this.normalizer = normalizer;
64          this.comparator = comparator;
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      public int compare( String o1, String o2 )
72      {
73          if ( onServer )
74          {
75              return comparator.compare( o1, o2 );
76          }
77  
78          String n1;
79          String n2;
80  
81          try
82          {
83              n1 = normalizer.normalize( o1 );
84          }
85          catch ( LdapException e )
86          {
87              LOG.warn( "Failed to normalize: " + o1, e );
88              n1 = o1;
89          }
90  
91          try
92          {
93              n2 = normalizer.normalize( o2 );
94          }
95          catch ( LdapException e )
96          {
97              LOG.warn( "Failed to normalize: " + o2, e );
98              n2 = o2;
99          }
100 
101         return comparator.compare( n1, n2 );
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      * 
108      * This implementation makes sure we update the oid property of the contained normalizer and 
109      * comparator.
110      */
111     @Override
112     public void setOid( String oid )
113     {
114         super.setOid( oid );
115         normalizer.setOid( oid );
116         comparator.setOid( oid );
117     }
118 
119 
120     /**
121      * tells that the normalizingComparator should not normalize values which are
122      * already normalized on the server 
123      */
124     public void setOnServer()
125     {
126         this.onServer = true;
127     }
128 }