View Javadoc
1   /*
2    * #%L
3    * settings4j
4    * ===============================================================
5    * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.settings4j.connector;
21  
22  import org.apache.commons.lang3.Validate;
23  import org.settings4j.Connector;
24  import org.settings4j.ContentResolver;
25  import org.settings4j.Filter;
26  import org.settings4j.ObjectResolver;
27  
28  /**
29   * Wrapper to add a {@link Filter} which is used before the given {@link Connector} is called.
30   *
31   * @author Harald.Brabenetz
32   */
33  public class FilteredConnectorWrapper implements Connector {
34  
35      private final Connector targetConnector;
36      private final Filter filter;
37  
38      /**
39       * @param targetConnector The Connect where the settings should be read if the Filter allows it.
40       * @param filter the {@link Filter} which defines if an key should be read from the given Connector.
41       */
42      public FilteredConnectorWrapper(final Connector targetConnector, final Filter filter) {
43          super();
44          Validate.notNull(targetConnector, "FilteredConnectorWrapper needs a Connector Object");
45          Validate.notNull(filter, "FilteredConnectorWrapper needs a Filter Object");
46          this.targetConnector = targetConnector;
47          this.filter = filter;
48      }
49  
50      @Override
51      public void addConnector(final Connector connector) {
52          this.targetConnector.addConnector(connector);
53      }
54  
55      @Override
56      public byte[] getContent(final String key) {
57          if (!this.filter.isValid(key)) {
58              return null;
59          }
60          return this.targetConnector.getContent(key);
61      }
62  
63      @Override
64      public String getName() {
65          return this.targetConnector.getName();
66      }
67  
68      @Override
69      public Object getObject(final String key) {
70          if (!this.filter.isValid(key)) {
71              return null;
72          }
73          return this.targetConnector.getObject(key);
74      }
75  
76      @Override
77      public String getString(final String key) {
78          if (!this.filter.isValid(key)) {
79              return null;
80          }
81          return this.targetConnector.getString(key);
82      }
83  
84      @Override
85      public void init() {
86          this.targetConnector.init();
87      }
88  
89      @Override
90      public void setContentResolver(final ContentResolver contentResolver) {
91          this.targetConnector.setContentResolver(contentResolver);
92      }
93  
94      @Override
95      public void setName(final String name) {
96          this.targetConnector.setName(name);
97      }
98  
99      @Override
100     public void setObjectResolver(final ObjectResolver objectResolver) {
101         this.targetConnector.setObjectResolver(objectResolver);
102     }
103 
104 
105 }