View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.schema;
18  
19  
20  import javax.naming.NamingException;
21  import java.io.Serializable;
22  import java.util.Comparator;
23  
24  
25  /***
26   * A serializable wrapper around a Comparator which uses delayed initialization
27   * of the underlying wrapped comparator which is JIT resolved from a static
28   * global registry.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev: 159259 $
32   */
33  public class SerializableComparator implements Comparator, Serializable
34  {
35      private static final long serialVersionUID = 3257566226288162870L;
36  
37      /*** the system global Comparator registry */
38      private static ComparatorRegistry registry = null;
39      /*** the OID of the matchingRule for this comparator */
40      private String matchingRuleOid;
41      /*** the transient wrapped comparator */
42      private transient Comparator wrapped;
43  
44  
45      // ------------------------------------------------------------------------
46      // S T A T I C   M E T H O D S
47      // ------------------------------------------------------------------------
48  
49  
50      /***
51       * Sets the global Comparator registry for comparator lookups.
52       *
53       * @param registry the comparator registry to use for Comparator lookups
54       */
55      public static void setRegistry( ComparatorRegistry registry )
56      {
57          SerializableComparator.registry = registry;
58      }
59  
60  
61      // ------------------------------------------------------------------------
62      // C O N T R U C T O R S
63      // ------------------------------------------------------------------------
64  
65  
66      public SerializableComparator( String matchingRuleOid )
67      {
68          this.matchingRuleOid = matchingRuleOid;
69      }
70  
71  
72      // ------------------------------------------------------------------------
73      // C O M P A R A T O R   I M P L E M E N T A T I O N S
74      // ------------------------------------------------------------------------
75  
76  
77      /***
78       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
79       */
80      public int compare( Object o1, Object o2 )
81      {
82          if ( wrapped == null )
83          {
84              try
85              {
86                  wrapped = registry.lookup( matchingRuleOid );
87              }
88              catch ( NamingException e )
89              {
90                  e.printStackTrace();
91              }
92          }
93  
94          return wrapped.compare( o1, o2 );
95      }
96  }