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 */ 017package org.apache.wicket.injection; 018 019import java.lang.reflect.Field; 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.List; 023 024import org.apache.wicket.util.lang.Args; 025 026/** 027 * Compound implementation of IFieldValueFactory. This field value factory will keep trying added 028 * factories until one returns a non-null value or all are tried. 029 * 030 * 031 * @see IFieldValueFactory 032 * 033 * @author Igor Vaynberg (ivaynberg) 034 * 035 */ 036public class CompoundFieldValueFactory implements IFieldValueFactory 037{ 038 private final List<IFieldValueFactory> delegates = new ArrayList<>(); 039 040 /** 041 * Constructor 042 * 043 * @param factories 044 */ 045 public CompoundFieldValueFactory(final IFieldValueFactory[] factories) 046 { 047 Args.notNull(factories, "factories"); 048 049 delegates.addAll(Arrays.asList(factories)); 050 } 051 052 /** 053 * Constructor 054 * 055 * @param factories 056 */ 057 public CompoundFieldValueFactory(final List<IFieldValueFactory> factories) 058 { 059 Args.notNull(factories, "factories"); 060 061 delegates.addAll(factories); 062 } 063 064 /** 065 * Constructor 066 * 067 * @param f1 068 * @param f2 069 */ 070 public CompoundFieldValueFactory(final IFieldValueFactory f1, final IFieldValueFactory f2) 071 { 072 Args.notNull(f1, "f1"); 073 Args.notNull(f2, "f2"); 074 075 delegates.add(f1); 076 delegates.add(f2); 077 } 078 079 /** 080 * Adds a factory to the compound factory 081 * 082 * @param factory 083 */ 084 public void addFactory(final IFieldValueFactory factory) 085 { 086 Args.notNull(factory, "factory"); 087 088 delegates.add(factory); 089 } 090 091 /** 092 * @see org.apache.wicket.injection.IFieldValueFactory#getFieldValue(java.lang.reflect.Field, 093 * java.lang.Object) 094 */ 095 @Override 096 public Object getFieldValue(final Field field, final Object fieldOwner) 097 { 098 for (IFieldValueFactory factory : delegates) 099 { 100 Object object = factory.getFieldValue(field, fieldOwner); 101 if (object != null) 102 { 103 return object; 104 } 105 } 106 return null; 107 } 108 109 /** 110 * @see org.apache.wicket.injection.IFieldValueFactory#supportsField(java.lang.reflect.Field) 111 */ 112 @Override 113 public boolean supportsField(final Field field) 114 { 115 for (IFieldValueFactory factory : delegates) 116 { 117 if (factory.supportsField(field)) 118 { 119 return true; 120 } 121 } 122 return false; 123 } 124 125}