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