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.shared.ldap.model.schema.registries;
021
022
023import org.apache.directory.shared.ldap.model.exception.LdapException;
024import org.apache.directory.shared.ldap.model.schema.LdapComparator;
025import org.apache.directory.shared.ldap.model.schema.SchemaObject;
026import org.apache.directory.shared.ldap.model.schema.SchemaObjectType;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * A Comparator registry service default implementation.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class DefaultComparatorRegistry extends DefaultSchemaObjectRegistry<LdapComparator<?>>
037    implements ComparatorRegistry
038{
039    /** static class logger */
040    private static final Logger LOG = LoggerFactory.getLogger( DefaultComparatorRegistry.class );
041
042    /** A speedup for debug */
043    private static final boolean DEBUG = LOG.isDebugEnabled();
044
045
046    /**
047     * Creates a new default ComparatorRegistry instance.
048     */
049    public DefaultComparatorRegistry()
050    {
051        super( SchemaObjectType.COMPARATOR, new OidRegistry<LdapComparator<?>>() );
052    }
053
054
055    /**
056     * {@inheritDoc}
057     */
058    public void unregisterSchemaElements( String schemaName ) throws LdapException
059    {
060        if ( schemaName == null )
061        {
062            return;
063        }
064
065        // Loop on all the SchemaObjects stored and remove those associated
066        // with the give schemaName
067        for ( LdapComparator<?> comparator : this )
068        {
069            if ( schemaName.equalsIgnoreCase( comparator.getSchemaName() ) )
070            {
071                String oid = comparator.getOid();
072                SchemaObject removed = unregister( oid );
073
074                if ( DEBUG )
075                {
076                    LOG.debug( "Removed {} with oid {} from the registry", removed, oid );
077                }
078            }
079        }
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    public DefaultComparatorRegistry copy()
087    {
088        DefaultComparatorRegistry copy = new DefaultComparatorRegistry();
089
090        // Copy the base data
091        copy.copy( this );
092
093        return copy;
094    }
095
096
097    /**
098     * @see Object#toString()
099     */
100    public String toString()
101    {
102        StringBuilder sb = new StringBuilder();
103
104        sb.append( schemaObjectType ).append( ": " );
105        boolean isFirst = true;
106
107        for ( String name : byName.keySet() )
108        {
109            if ( isFirst )
110            {
111                isFirst = false;
112            }
113            else
114            {
115                sb.append( ", " );
116            }
117
118            LdapComparator<?> comparator = byName.get( name );
119
120            String fqcn = comparator.getFqcn();
121            int lastDotPos = fqcn.lastIndexOf( '.' );
122
123            sb.append( '<' ).append( comparator.getOid() ).append( ", " );
124
125            if ( lastDotPos > 0 )
126            {
127                sb.append( fqcn.substring( lastDotPos + 1 ) );
128            }
129            else
130            {
131                sb.append( fqcn );
132            }
133
134            sb.append( '>' );
135        }
136
137        return sb.toString();
138    }
139}