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.normalizers;
021
022
023import org.apache.directory.shared.ldap.model.entry.Value;
024import org.apache.directory.shared.ldap.model.exception.LdapException;
025import org.apache.directory.shared.ldap.model.schema.Normalizer;
026import org.apache.directory.shared.ldap.model.schema.SchemaManager;
027import org.apache.directory.shared.ldap.model.schema.registries.Registries;
028
029
030/**
031 * Caches previously normalized values.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035@SuppressWarnings("serial")
036public class CachingNormalizer extends Normalizer
037{
038    /** Cache maximum size default */
039    public static final int CACHE_MAX = 250;
040
041    /** The underlying decorated Normalizer */
042    protected final Normalizer normalizer;
043
044
045    // ------------------------------------------------------------------------
046    // C O N S T R U C T O R S
047    // ------------------------------------------------------------------------
048
049    /**
050     * Creates a CachingNormalizer that decorates another normalizer using a
051     * default cache size.  This Normalizer delegates 
052     * 
053     * @param normalizer the underlying Normalizer being decorated
054     */
055    public CachingNormalizer( Normalizer normalizer )
056    {
057        this( normalizer, CACHE_MAX );
058    }
059
060
061    /**
062     * Creates a CachingNormalizer that decorates another normalizer using a
063     * specified cache size.
064     * 
065     * @param normalizer the underlying Normalizer being decorated
066     * @param cacheSz the maximum size of the name cache
067     */
068    public CachingNormalizer( Normalizer normalizer, int cacheSz )
069    {
070        super( normalizer.getOid() );
071        this.normalizer = normalizer;
072    }
073
074
075    /**
076     * Overrides default behavior by returning the OID of the wrapped 
077     * Normalizer.
078     */
079    @Override
080    public String getOid()
081    {
082        return normalizer.getOid();
083    }
084
085
086    /**
087     * Overrides default behavior by setting the OID of the wrapped Normalizer.
088     * 
089     * @param oid the object identifier to set
090     */
091    @Override
092    public void setOid( String oid )
093    {
094        super.setOid( oid );
095        normalizer.setOid( oid );
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    public Value<?> normalize( Value<?> value ) throws LdapException
103    {
104        if ( value == null )
105        {
106            return null;
107        }
108
109        Value<?> normalized = normalizer.normalize( value );
110        
111        return normalized;
112    }
113
114
115    /**
116     * {@inheritDoc}
117     */
118    public String normalize( String value ) throws LdapException
119    {
120        if ( value == null )
121        {
122            return null;
123        }
124
125        String normalized = normalizer.normalize( value );
126
127        return normalized;
128    }
129
130
131    /**
132     * {@inheritDoc}
133     */
134    public void setRegistries( Registries registries )
135    {
136        normalizer.setRegistries( registries );
137    }
138
139
140    /**
141     * Sets the SchemaManager
142     * 
143     * @param schemaManager The SchemaManager
144     */
145    public void setSchemaManager( SchemaManager schemaManager )
146    {
147        normalizer.setSchemaManager( schemaManager );
148    }
149}