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.contentresolver;
21  
22  import org.apache.commons.lang3.ArrayUtils;
23  import org.settings4j.ContentResolver;
24  
25  /**
26   * The UnionContentResolver can be an container for many other {@link ContentResolver} which will be processed in sequence.
27   * <p>
28   * the first result of a ContentResolver which are a not-null value will be used as result.
29   * </p>
30   *
31   * @author Harald.Brabenetz
32   */
33  public class UnionContentResolver implements ContentResolver {
34  
35      private ContentResolver[] contentResolvers = new ContentResolver[0];
36  
37      /**
38       * Default Constructor.
39       */
40      public UnionContentResolver() {
41          super();
42      }
43  
44      /**
45       * Constructor with the first {@link ContentResolver}.
46       *
47       * @param contentResolver
48       *        with the first ContentResolver.
49       */
50      public UnionContentResolver(final ContentResolver contentResolver) {
51          super();
52          addContentResolverInternal(contentResolver);
53      }
54  
55      @Override
56      public void addContentResolver(final ContentResolver contentResolver) {
57          synchronized (this) {
58              addContentResolverInternal(contentResolver);
59          }
60      }
61  
62      private void addContentResolverInternal(final ContentResolver contentResolver) {
63          final ContentResolver[] contentResolversNew = new ContentResolver[this.contentResolvers.length + 1];
64          for (int i = 0; i < this.contentResolvers.length; i++) {
65              contentResolversNew[i] = this.contentResolvers[i];
66          }
67          contentResolversNew[this.contentResolvers.length] = contentResolver;
68  
69          this.contentResolvers = contentResolversNew;
70      }
71  
72      @Override
73      // SuppressWarnings PMD.ReturnEmptyArrayRatherThanNull: returning null for this byte-Arrays is OK.
74      @SuppressWarnings("PMD.ReturnEmptyArrayRatherThanNull")
75      public byte[] getContent(final String key) {
76          byte[] result = null;
77          for (final ContentResolver contentResolver : this.contentResolvers) {
78              result = contentResolver.getContent(key);
79              if (result != null) {
80                  return result;
81              }
82          }
83          return result;
84      }
85  
86      public ContentResolver[] getContentResolvers() {
87          return ArrayUtils.clone(this.contentResolvers);
88      }
89  
90  }