View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   *****************************************************************************/
17  
18  package org.settings4j.objectresolver;
19  
20  import org.settings4j.ContentResolver;
21  import org.settings4j.Filter;
22  import org.settings4j.ObjectResolver;
23  
24  /**
25   * Wrapper to add a {@link Filter} which is used before the given {@link ObjectResolver} is called.
26   * 
27   * @author Harald.Brabenetz
28   */
29  public class FilteredObjectResolverWrapper implements ObjectResolver {
30  
31      private final ObjectResolver targetObjectResolver;
32      private final Filter filter;
33  
34      /**
35       * 
36       * @param targetObjectResolver The ObjectResolver where the settings should be read if the Filter allows it.
37       * @param filter the {@link Filter} which defines if an key should be read from the given ObjectResolver.
38       */
39      public FilteredObjectResolverWrapper(final ObjectResolver targetObjectResolver, final Filter filter) {
40          super();
41          if (targetObjectResolver == null) {
42              throw new IllegalArgumentException("FilteredConnectorWrapper needs a ObjectResolver Object");
43          }
44          if (filter == null) {
45              throw new IllegalArgumentException("FilteredConnectorWrapper needs a Filter Object");
46          }
47          this.targetObjectResolver = targetObjectResolver;
48          this.filter = filter;
49      }
50  
51      /** {@inheritDoc} */
52      public void addObjectResolver(final ObjectResolver objectResolver) {
53          this.targetObjectResolver.addObjectResolver(objectResolver);
54      }
55  
56      /** {@inheritDoc} */
57      public Object getObject(final String key, final ContentResolver contentResolver) {
58          if (!this.filter.isValid(key)) {
59              return null;
60          }
61          return this.targetObjectResolver.getObject(key, contentResolver);
62      }
63  }