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.commons.flatfile.entityfactory;
018
019import org.apache.commons.flatfile.Entity;
020import org.apache.commons.flatfile.EntityFactory;
021
022/**
023 * Delegates to <i>0..n</i> EntityFactories.
024 * @version $Revision: 1301247 $ $Date: 2012-03-15 17:19:19 -0500 (Thu, 15 Mar 2012) $
025 */
026public class CompositeEntityFactory implements EntityFactory {
027
028    private final EntityFactory[] delegates;
029
030    /**
031     * Create a new CompositeEntityFactory.
032     * @param delegates EntityFactory[]
033     */
034    public CompositeEntityFactory(EntityFactory... delegates) {
035        this.delegates = delegates == null ? new EntityFactory[0] : delegates;
036    }
037
038    /**
039     * {@inheritDoc}
040     */
041    public Entity getEntity(Object cue) {
042        for (EntityFactory delegate : delegates) {
043            Entity result = delegate.getEntity(cue);
044            if (result != null) {
045                return result;
046            }
047        }
048        return null;
049    }
050
051}