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 java.io.Serializable;
24  import java.util.Comparator;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.schema.LdapComparator;
29  import org.apache.directory.api.ldap.model.schema.SchemaManager;
30  
31  
32  /**
33   * A serializable wrapper around a Comparator which uses delayed initialization
34   * of the underlying wrapped comparator which is JIT resolved from a static
35   * global registry.
36   * 
37   * @param <E> The type of object to compare
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class SerializableComparator<E> extends LdapComparator<E> implements Serializable
42  {
43      /** The serial version UID */
44      private static final long serialVersionUID = 2L;
45  
46      /** the OID of the matchingRule for this comparator */
47      private String matchingRuleOid;
48  
49      /** the transient wrapped comparator */
50      private transient Comparator<E> wrapped;
51  
52      /** A reference to the schema manager */
53      private transient SchemaManager schemaManager;
54  
55  
56      /**
57       * Creates a new instance of SerializableComparator.
58       *
59       * @param matchingRuleOid The MatchingRule OID
60       */
61      public SerializableComparator( String matchingRuleOid )
62      {
63          super( matchingRuleOid );
64          this.matchingRuleOid = matchingRuleOid;
65      }
66  
67  
68      // ------------------------------------------------------------------------
69      // 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
70      // ------------------------------------------------------------------------
71  
72      /**
73       * {@inheritDoc}
74       */
75      @SuppressWarnings("unchecked")
76      @Override
77      public int compare( E o1, E o2 )
78      {
79          if ( wrapped == null )
80          {
81              try
82              {
83                  wrapped = ( Comparator<E> ) schemaManager.lookupComparatorRegistry( matchingRuleOid );
84              }
85              catch ( LdapException le )
86              {
87                  throw new RuntimeException( I18n.err( I18n.ERR_04221, matchingRuleOid ), le );
88              }
89          }
90  
91          return wrapped.compare( o1, o2 );
92      }
93  
94  
95      /**
96       * @param schemaManager the schemaManager to set
97       */
98      @SuppressWarnings("unchecked")
99      @Override
100     public void setSchemaManager( SchemaManager schemaManager )
101     {
102         if ( wrapped == null )
103         {
104             try
105             {
106                 wrapped = ( Comparator<E> )
107                     schemaManager.lookupComparatorRegistry( matchingRuleOid );
108             }
109             catch ( LdapException ne )
110             {
111                 // Not found : get the default comparator
112                 wrapped = ( Comparator<E> )
113                     new ComparableComparator<>( matchingRuleOid );
114             }
115         }
116 
117         ( ( LdapComparator<E> ) wrapped ).setSchemaManager( schemaManager );
118         this.schemaManager = schemaManager;
119     }
120 }