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.ObjectResolver;
22
23 /**
24 * The UnionObjectResolver can be an container for many other {@link ObjectResolver}
25 * which will be processed in sequence.
26 * <p>
27 * the first result of a ObjectResolver which are a not-null value will be used as result.
28 *
29 * @author Harald.Brabenetz
30 */
31 public class UnionObjectResolver implements ObjectResolver {
32
33 private ObjectResolver[] objectResolvers = new ObjectResolver[0];
34
35 /** {@inheritDoc} */
36 public void addObjectResolver(final ObjectResolver objectResolver) {
37
38 final ObjectResolver[] objectResolversNew = new ObjectResolver[this.objectResolvers.length + 1];
39 for (int i = 0; i < this.objectResolvers.length; i++) {
40 objectResolversNew[i] = this.objectResolvers[i];
41 }
42 objectResolversNew[this.objectResolvers.length] = objectResolver;
43
44 this.objectResolvers = objectResolversNew;
45 }
46
47 /** {@inheritDoc} */
48 public Object getObject(final String key, final ContentResolver contentResolver) {
49
50 Object result = null;
51 for (int i = 0; i < this.objectResolvers.length; i++) {
52 result = this.objectResolvers[i].getObject(key, contentResolver);
53 if (result != null) {
54 return result;
55 }
56 }
57 return result;
58 }
59
60 }