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.i18n.I18n;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.schema.LdapComparator;
028import org.apache.directory.api.ldap.model.schema.SchemaObject;
029import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * A Comparator registry service default implementation.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class DefaultComparatorRegistry extends DefaultSchemaObjectRegistry<LdapComparator<?>>
040    implements ComparatorRegistry
041{
042    /** static class logger */
043    private static final Logger LOG = LoggerFactory.getLogger( DefaultComparatorRegistry.class );
044
045    /**
046     * Creates a new default ComparatorRegistry instance.
047     */
048    public DefaultComparatorRegistry()
049    {
050        super( SchemaObjectType.COMPARATOR, new OidRegistry<LdapComparator<?>>() );
051    }
052
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
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 ( LOG.isDebugEnabled() )
075                {
076                    LOG.debug( I18n.msg( I18n.MSG_13702_REMOVED_FROM_REGISTRY, removed, oid ) );
077                }
078            }
079        }
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public DefaultComparatorRegistry copy()
088    {
089        DefaultComparatorRegistry copy = new DefaultComparatorRegistry();
090
091        // Copy the base data
092        copy.copy( this );
093
094        return copy;
095    }
096
097
098    /**
099     * @see Object#toString()
100     */
101    @Override
102    public String toString()
103    {
104        StringBuilder sb = new StringBuilder();
105
106        sb.append( schemaObjectType ).append( ": " );
107        boolean isFirst = true;
108
109        for ( Map.Entry<String, LdapComparator<?>> entry : byName.entrySet() )
110        {
111            if ( isFirst )
112            {
113                isFirst = false;
114            }
115            else
116            {
117                sb.append( ", " );
118            }
119
120            LdapComparator<?> comparator = entry.getValue();
121
122            String fqcn = comparator.getFqcn();
123            int lastDotPos = fqcn.lastIndexOf( '.' );
124
125            sb.append( '<' ).append( comparator.getOid() ).append( ", " );
126
127            if ( lastDotPos > 0 )
128            {
129                sb.append( fqcn.substring( lastDotPos + 1 ) );
130            }
131            else
132            {
133                sb.append( fqcn );
134            }
135
136            sb.append( '>' );
137        }
138
139        return sb.toString();
140    }
141}