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 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
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
64
65
66
67 public SerializableComparator( String matchingRuleOid )
68 {
69 this.matchingRuleOid = matchingRuleOid;
70 }
71
72
73
74
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 }