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 java.io.Serializable;
21  import java.util.Comparator;
22  
23  import javax.naming.NamingException;
24  
25  
26  /***
27   * A serializable wrapper around a Comparator which uses delayed initialization
28   * of the underlying wrapped comparator which is JIT resolved from a static
29   * global registry.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 264732 $
33   */
34  public class SerializableComparator implements Comparator, Serializable
35  {
36      private static final long serialVersionUID = 3257566226288162870L;
37  
38      /*** the system global Comparator registry */
39      private static ComparatorRegistry registry = null;
40      /*** the OID of the matchingRule for this comparator */
41      private String matchingRuleOid;
42      /*** the transient wrapped comparator */
43      private transient Comparator wrapped;
44  
45  
46      // ------------------------------------------------------------------------
47      // S T A T I C   M E T H O D S
48      // ------------------------------------------------------------------------
49  
50  
51      /***
52       * Sets the global Comparator registry for comparator lookups.
53       *
54       * @param registry the comparator registry to use for Comparator lookups
55       */
56      public static void setRegistry( ComparatorRegistry registry )
57      {
58          SerializableComparator.registry = registry;
59      }
60  
61  
62      // ------------------------------------------------------------------------
63      // C O N T R U C T O R S
64      // ------------------------------------------------------------------------
65  
66  
67      public SerializableComparator( String matchingRuleOid )
68      {
69          this.matchingRuleOid = matchingRuleOid;
70      }
71  
72  
73      // ------------------------------------------------------------------------
74      // 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
75      // ------------------------------------------------------------------------
76  
77  
78      /***
79       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
80       */
81      public int compare( Object o1, Object o2 )
82      {
83          if ( wrapped == null )
84          {
85              try
86              {
87                  wrapped = registry.lookup( matchingRuleOid );
88              }
89              catch ( NamingException e )
90              {
91                  e.printStackTrace();
92              }
93          }
94  
95          return wrapped.compare( o1, o2 );
96      }
97  }