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   *    https://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       * {@inheritDoc}
70       */
71      @SuppressWarnings("unchecked")
72      @Override
73      public int compare( E o1, E o2 )
74      {
75          if ( wrapped == null )
76          {
77              try
78              {
79                  wrapped = ( Comparator<E> ) schemaManager.lookupComparatorRegistry( matchingRuleOid );
80              }
81              catch ( LdapException le )
82              {
83                  throw new RuntimeException( I18n.err( I18n.ERR_13723_MATCHING_RULE_NOT_FOUND, matchingRuleOid ), le );
84              }
85          }
86  
87          return wrapped.compare( o1, o2 );
88      }
89  
90  
91      /**
92       * @param schemaManager the schemaManager to set
93       */
94      @SuppressWarnings("unchecked")
95      @Override
96      public void setSchemaManager( SchemaManager schemaManager )
97      {
98          if ( wrapped == null )
99          {
100             try
101             {
102                 wrapped = ( Comparator<E> )
103                     schemaManager.lookupComparatorRegistry( matchingRuleOid );
104             }
105             catch ( LdapException ne )
106             {
107                 // Not found : get the default comparator
108                 wrapped = ( Comparator<E> )
109                     new ComparableComparator<>( matchingRuleOid );
110             }
111         }
112 
113         ( ( LdapComparator<E> ) wrapped ).setSchemaManager( schemaManager );
114         this.schemaManager = schemaManager;
115     }
116 }