1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
63
64
65
66 public SerializableComparator( String matchingRuleOid )
67 {
68 this.matchingRuleOid = matchingRuleOid;
69 }
70
71
72
73
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 }