001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.schema.comparators; 021 022 023import java.io.Serializable; 024import java.util.Comparator; 025 026import org.apache.directory.api.i18n.I18n; 027import org.apache.directory.api.ldap.model.exception.LdapException; 028import org.apache.directory.api.ldap.model.schema.LdapComparator; 029import org.apache.directory.api.ldap.model.schema.SchemaManager; 030 031 032/** 033 * A serializable wrapper around a Comparator which uses delayed initialization 034 * of the underlying wrapped comparator which is JIT resolved from a static 035 * global registry. 036 * 037 * @param <E> The type of object to compare 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 */ 041public class SerializableComparator<E> extends LdapComparator<E> implements Serializable 042{ 043 /** The serial version UID */ 044 private static final long serialVersionUID = 2L; 045 046 /** the OID of the matchingRule for this comparator */ 047 private String matchingRuleOid; 048 049 /** the transient wrapped comparator */ 050 private transient Comparator<E> wrapped; 051 052 /** A reference to the schema manager */ 053 private transient SchemaManager schemaManager; 054 055 056 /** 057 * Creates a new instance of SerializableComparator. 058 * 059 * @param matchingRuleOid The MatchingRule OID 060 */ 061 public SerializableComparator( String matchingRuleOid ) 062 { 063 super( matchingRuleOid ); 064 this.matchingRuleOid = matchingRuleOid; 065 } 066 067 068 /** 069 * {@inheritDoc} 070 */ 071 @SuppressWarnings("unchecked") 072 @Override 073 public int compare( E o1, E o2 ) 074 { 075 if ( wrapped == null ) 076 { 077 try 078 { 079 wrapped = ( Comparator<E> ) schemaManager.lookupComparatorRegistry( matchingRuleOid ); 080 } 081 catch ( LdapException le ) 082 { 083 throw new RuntimeException( I18n.err( I18n.ERR_13723_MATCHING_RULE_NOT_FOUND, matchingRuleOid ), le ); 084 } 085 } 086 087 return wrapped.compare( o1, o2 ); 088 } 089 090 091 /** 092 * @param schemaManager the schemaManager to set 093 */ 094 @SuppressWarnings("unchecked") 095 @Override 096 public void setSchemaManager( SchemaManager schemaManager ) 097 { 098 if ( wrapped == null ) 099 { 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}