View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import org.apache.directory.api.ldap.model.entry.Value;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.schema.Normalizer;
26  import org.apache.directory.api.ldap.model.schema.SchemaManager;
27  
28  
29  /**
30   * Caches previously normalized values.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  @SuppressWarnings("serial")
35  public class CachingNormalizer extends Normalizer
36  {
37      /** Cache maximum size default */
38      public static final int CACHE_MAX = 250;
39  
40      /** The underlying decorated Normalizer */
41      protected final Normalizer normalizer;
42  
43  
44      // ------------------------------------------------------------------------
45      // C O N S T R U C T O R S
46      // ------------------------------------------------------------------------
47  
48      /**
49       * Creates a CachingNormalizer that decorates another normalizer using a
50       * default cache size.  This Normalizer delegates
51       * 
52       * @param normalizer the underlying Normalizer being decorated
53       */
54      public CachingNormalizer( Normalizer normalizer )
55      {
56          this( normalizer, CACHE_MAX );
57      }
58  
59  
60      /**
61       * Creates a CachingNormalizer that decorates another normalizer using a
62       * specified cache size.
63       * 
64       * @param normalizer the underlying Normalizer being decorated
65       * @param cacheSz the maximum size of the name cache
66       */
67      public CachingNormalizer( Normalizer normalizer, int cacheSz )
68      {
69          super( normalizer.getOid() );
70          this.normalizer = normalizer;
71      }
72  
73  
74      /**
75       * Overrides default behavior by returning the OID of the wrapped
76       * Normalizer.
77       */
78      @Override
79      public String getOid()
80      {
81          return normalizer.getOid();
82      }
83  
84  
85      /**
86       * Overrides default behavior by setting the OID of the wrapped Normalizer.
87       * 
88       * @param oid the object identifier to set
89       */
90      @Override
91      public void setOid( String oid )
92      {
93          super.setOid( oid );
94          normalizer.setOid( oid );
95      }
96  
97  
98      /**
99       * {@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 }