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.commons.inject.api;
018    
019    import java.util.Arrays;
020    import java.util.Collection;
021    
022    import org.apache.commons.inject.api.bind.IModule;
023    import org.apache.commons.inject.impl.DefaultInjectorBuilder;
024    
025    /**
026     * This class provides the anchor point to create {@link IInjector injectors} via
027     * factory methods.
028     */
029    public class CommonsInject {
030            /**
031             * Creates a new {@link IInjector injector}, which is configured by invoking
032             * the given modules.
033             * @param pModules The modules, which provide the injectors bindings.
034             * @return A new {@link IInjector injector}.
035             * @see #build(Collection)
036             */
037            public static IInjector build(IModule... pModules) {
038                    if (pModules == null) {
039                            throw new NullPointerException("The module list must not be null.");
040                    }
041                    final Collection<IModule> modules = Arrays.asList(pModules);
042                    return build(modules);
043            }
044            /**
045             * Creates a new {@link IInjector injector}, which is configured by invoking
046             * the given modules.
047             * @param pModules The modules, which provide the injectors bindings.
048             * @return A new {@link IInjector injector}.
049             * @see #build(IModule...)
050             */
051            public static IInjector build(Collection<IModule> pModules) {
052                    if (pModules == null) {
053                            throw new NullPointerException("The module list must not be null.");
054                    }
055                    if (pModules.isEmpty()) {
056                            throw new IllegalArgumentException("The module list must not be empty.");
057                    }
058                    return new DefaultInjectorBuilder(pModules).build();
059            }
060            /**
061             * An alternative to {@link #build(IModule...)}, which provides additional
062             * control on the injectors configuration. Rather than directly returning
063             * an {@link IInjector injector}, this method returns an {@link IInjectorBuilder
064             * injector builder}.
065             * @return A new {@link IInjectorBuilder injector builder}.
066             * @see #build(IModule...)
067             * @see #build(Collection)
068             */
069            public static IInjectorBuilder newBuilder() {
070                    return new DefaultInjectorBuilder();
071            }
072    }