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 */
020 package org.apache.directory.shared.ldap.schema.normalizers;
021
022
023 import javax.naming.NamingException;
024
025 import org.apache.directory.shared.ldap.entry.Value;
026 import org.apache.directory.shared.ldap.schema.Normalizer;
027 import org.apache.directory.shared.ldap.util.SynchronizedLRUMap;
028
029
030 /**
031 * Caches previously normalized values.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 * @version $Rev: 798550 $
035 */
036 public class CachingNormalizer implements Normalizer
037 {
038 private static final long serialVersionUID = 1L;
039
040 /** Cache maximum size default */
041 public static final int CACHE_MAX = 250;
042
043 /** Least recently used cache */
044 private final SynchronizedLRUMap cache;
045
046 /** The underlying decorated Normalizer */
047 protected final Normalizer normalizer;
048
049
050 // ------------------------------------------------------------------------
051 // C O N S T R U C T O R S
052 // ------------------------------------------------------------------------
053
054 /**
055 * Creates a CachingNormalizer that decorates another normalizer using a
056 * default cache size.
057 *
058 * @param normalizer the underlying Normalizer being decorated
059 */
060 public CachingNormalizer( Normalizer normalizer )
061 {
062 this( normalizer, CACHE_MAX );
063 }
064
065
066 /**
067 * Creates a CachingNormalizer that decorates another normalizer using a
068 * specified cache size.
069 *
070 * @param normalizer the underlying Normalizer being decorated
071 * @param cacheSz the maximum size of the name cache
072 */
073 public CachingNormalizer( Normalizer normalizer, int cacheSz )
074 {
075 this.normalizer = normalizer;
076 cache = new SynchronizedLRUMap( cacheSz );
077 }
078
079
080 /**
081 * {@inheritDoc}
082 */
083 public Value<?> normalize( Value<?> value ) throws NamingException
084 {
085 if ( value == null )
086 {
087 return null;
088 }
089
090 Value<?> result =(Value<?>)cache.get( value );
091
092 if ( result != null )
093 {
094 return result;
095 }
096
097 Value<?> normalized = normalizer.normalize( value );
098 cache.put( value, normalized );
099 return normalized;
100 }
101
102
103 /**
104 * {@inheritDoc}
105 */
106 public String normalize( String value ) throws NamingException
107 {
108 if ( value == null )
109 {
110 return null;
111 }
112
113 String normalized =(String)cache.get( value );
114
115 if ( normalized != null )
116 {
117 return normalized;
118 }
119
120 normalized = normalizer.normalize( value );
121 cache.put( value, normalized );
122 return normalized;
123 }
124 }