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