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