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 package org.settings4j.contentresolver; 18 19 import org.settings4j.ContentResolver; 20 import org.settings4j.Filter; 21 22 /** 23 * Wrapper to add a {@link Filter} which is used before the given {@link ContentResolver} is called. 24 * 25 * @author Harald.Brabenetz 26 * 27 */ 28 public class FilteredContentResolverWrapper implements ContentResolver { 29 30 private final ContentResolver targetContentResolver; 31 private final Filter filter; 32 33 /** 34 * @param targetContentResolver The {@link ContentResolver} where the settings 35 * should be read if the Filter allows it. 36 * @param filter the {@link Filter} which defines if an key should be read from the given {@link ContentResolver}. 37 */ 38 public FilteredContentResolverWrapper(final ContentResolver targetContentResolver, final Filter filter) { 39 super(); 40 if (targetContentResolver == null) { 41 throw new IllegalArgumentException("FilteredConnectorWrapper needs a ContentResolver Object"); 42 } 43 if (filter == null) { 44 throw new IllegalArgumentException("FilteredConnectorWrapper needs a Filter Object"); 45 } 46 this.targetContentResolver = targetContentResolver; 47 this.filter = filter; 48 } 49 50 /** {@inheritDoc} */ 51 public void addContentResolver(final ContentResolver contentResolver) { 52 this.targetContentResolver.addContentResolver(contentResolver); 53 } 54 55 /** {@inheritDoc} */ 56 public byte[] getContent(final String key) { 57 if (!this.filter.isValid(key)) { 58 return null; 59 } 60 return this.targetContentResolver.getContent(key); 61 } 62 }