All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.picocontainer.containers.CompositePicoContainer Maven / Gradle / Ivy

There is a newer version: 2.15
Show newest version
/*****************************************************************************
 * Copyright (c) PicoContainer Organization. All rights reserved.            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *                                                                           *
 * Idea by Rachel Davies, Original code by various                           *
 *****************************************************************************/
package org.picocontainer.containers;

import org.picocontainer.*;

import java.lang.reflect.Type;
import java.lang.annotation.Annotation;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Collections;

/**
 * CompositePicoContainer takes a var-args list of containers and will query them
 * in turn for getComponent(*) and getComponentAdapter(*) requests.  Methods returning
 * lists and getParent/accept will not function.
 */
public class CompositePicoContainer implements PicoContainer, Converting, Serializable {

    private final PicoContainer[] containers;
    private Converters compositeConverter = new CompositeConverters();

    public class CompositeConverters implements Converters {
        public boolean canConvert(Type type) {
            for (PicoContainer container : containers) {
                if (container instanceof Converting && ((Converting) container).getConverters().canConvert(type)) {
                    return true;
                }
            }
            return false;
        }

        public Object convert(String paramValue, Type type) {
            for (PicoContainer container : containers) {
                if (container instanceof Converting) {
                    Converters converter = ((Converting) container).getConverters();
                    if (converter.canConvert(type)) {
                        return converter.convert(paramValue, type);
                    }
                }
            }
            return null;
        }
    }

    public CompositePicoContainer(PicoContainer... containers) {
        this.containers = containers;
    }

    public  T getComponent(Class componentType) {
        for (PicoContainer container : containers) {
            T inst = container.getComponent(componentType);
            if (inst != null) {
                return inst;
            }
        }
        return null;
    }

    public Object getComponent(Object componentKeyOrType, Type into) {
        for (PicoContainer container : containers) {
            Object inst = container.getComponent(componentKeyOrType, into);
            if (inst != null) {
                return inst;
            }
        }
        return null;
    }

    public Object getComponent(Object componentKeyOrType) {
        for (PicoContainer container : containers) {
            Object inst = container.getComponent(componentKeyOrType);
            if (inst != null) {
                return inst;
            }
        }
        return null;
    }

    public ComponentAdapter getComponentAdapter(Object componentKey) {
        for (PicoContainer container : containers) {
            ComponentAdapter inst = container.getComponentAdapter(componentKey);
            if (inst != null) {
                return inst;
            }
        }
        return null;
    }

    public  ComponentAdapter getComponentAdapter(Class componentType, NameBinding nameBinding) {
        for (PicoContainer container : containers) {
            ComponentAdapter inst = container.getComponentAdapter(componentType, nameBinding);
            if (inst != null) {
                return inst;
            }
        }
        return null;
    }

    public  ComponentAdapter getComponentAdapter(Class componentType, Class binding) {
        for (PicoContainer container : containers) {
            ComponentAdapter inst = container.getComponentAdapter(componentType, binding);
            if (inst != null) {
                return inst;
            }
        }
        return null;
    }

    public  T getComponent(Class componentType, Class binding) {
        return null;
    }

    public List getComponents() {
        return Collections.emptyList();
    }

    public PicoContainer getParent() {
        return null;
    }

    public Collection> getComponentAdapters() {
        return Collections.emptyList();
    }

    public  List> getComponentAdapters(Class componentType) {
        return Collections.emptyList();
    }

    public  List> getComponentAdapters(Class componentType, Class binding) {
        return Collections.emptyList();
    }

    public  List getComponents(Class componentType) {
        return Collections.emptyList();
    }

    public void accept(PicoVisitor visitor) {
    }

    public Converters getConverters() {
        return compositeConverter;
    }
}