001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.injection;
018:
019: import java.lang.reflect.Field;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: /**
025: * Compound implementation of IFieldValueFactory. This field value factory will
026: * keep trying added factories until one returns a non-null value or all are
027: * tried.
028: *
029: *
030: * @see IFieldValueFactory
031: *
032: * @author Igor Vaynberg (ivaynberg)
033: *
034: */
035: public class CompoundFieldValueFactory implements IFieldValueFactory {
036: private List delegates = new ArrayList();
037:
038: /**
039: * Constructor
040: *
041: * @param factories
042: */
043: public CompoundFieldValueFactory(IFieldValueFactory[] factories) {
044: if (factories == null) {
045: throw new IllegalArgumentException(
046: "argument [factories] cannot be null");
047: }
048:
049: for (int i = 0; i < factories.length; i++) {
050: delegates.add(factories[i]);
051: }
052: }
053:
054: /**
055: * Constructor
056: *
057: * @param factories
058: */
059: public CompoundFieldValueFactory(List factories) {
060: if (factories == null) {
061: throw new IllegalArgumentException(
062: "argument [factories] cannot be null");
063: }
064: delegates.addAll(factories);
065: }
066:
067: /**
068: * Constructor
069: *
070: * @param f1
071: * @param f2
072: */
073: public CompoundFieldValueFactory(IFieldValueFactory f1,
074: IFieldValueFactory f2) {
075: if (f1 == null) {
076: throw new IllegalArgumentException(
077: "argument [f1] cannot be null");
078: }
079: if (f2 == null) {
080: throw new IllegalArgumentException(
081: "argument [f2] cannot be null");
082: }
083: delegates.add(f1);
084: delegates.add(f2);
085: }
086:
087: /**
088: * Adds a factory to the compound factory
089: *
090: * @param factory
091: */
092: public void addFactory(IFieldValueFactory factory) {
093: if (factory == null) {
094: throw new IllegalArgumentException(
095: "argument [factory] cannot be null");
096: }
097: delegates.add(factory);
098: }
099:
100: /**
101: * @see org.apache.wicket.injection.IFieldValueFactory#getFieldValue(java.lang.reflect.Field,
102: * java.lang.Object)
103: */
104: public Object getFieldValue(Field field, Object fieldOwner) {
105: Iterator it = delegates.iterator();
106: while (it.hasNext()) {
107: final IFieldValueFactory factory = (IFieldValueFactory) it
108: .next();
109: Object object = factory.getFieldValue(field, fieldOwner);
110: if (object != null) {
111: return object;
112: }
113: }
114: return null;
115: }
116:
117: /**
118: * @see org.apache.wicket.injection.IFieldValueFactory#supportsField(java.lang.reflect.Field)
119: */
120: public boolean supportsField(Field field) {
121: Iterator it = delegates.iterator();
122: while (it.hasNext()) {
123: final IFieldValueFactory factory = (IFieldValueFactory) it
124: .next();
125: if (factory.supportsField(field)) {
126: return true;
127: }
128: }
129: return false;
130: }
131:
132: }
|