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

io.baratine.inject.Injector Maven / Gradle / Ivy

/*
 * Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
 *
 * This file is part of Baratine(TM)(TM)
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Baratine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Baratine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Baratine; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package io.baratine.inject;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.inject.Provider;

import io.baratine.config.IncludeGenerator;
import io.baratine.convert.Convert;
import io.baratine.spi.ServiceManagerProvider;

/**
 * The injection manager interface
 */
public interface Injector
{
  /**
   * Returns an instance provider for the given Key, which combines
   * a Type with &64;Qualifier annotations.
   */
   Provider provider(Key key);
  
  /**
   * Returns an instance provider for the given InjectionPoint.
   */
   Provider provider(InjectionPoint atPoint);
  
  /**
   * Returns an injected instance for the given type. The instance returned
   * depends on the bindings of the inject manager.
   */
   T instance(Class type);

  /**
   * Returns an injected instance for the given Key, which combines
   * a type with annotations.
   */
   T instance(Key key);
  
  /**
   * Returns an injected instance for the given InjectionPoint.
   */
   T instance(InjectionPoint ip);
  
  /**
   * Consumer for injecting dependencies.
   */
   Consumer injector(Class type);

  /**
   * Returns the bindings associated with a key.
   */
   List> bindings(Key key);
  
  /**
   * Returns the type converter from a source class to a target class.
   */
   Convert converter(Class source, Class target);

  /**
   * Creates a new manager.
   */
  public static InjectorBuilder newManager(ClassLoader classLoader)
  {
    return ServiceManagerProvider.current().injectManager(classLoader);
  }

  public static InjectorBuilder newManager()
  {
    return newManager(Thread.currentThread().getContextClassLoader());
  }
  
  public interface InjectorBuilder
  {
    default InjectorBuilder include(Class includeClass)
    {
      throw new UnsupportedOperationException(getClass().getName());
    }
    
     BindingBuilder bean(Class impl);
     BindingBuilder bean(T instance);
    
     BindingBuilder provider(Provider provider);
    
    // BindingBuilder function(Function,T> function);
    
     BindingBuilder provider(Key parent, Method m);
    
    InjectorBuilder autoBind(InjectAutoBind autoBind);
    
    Injector get();
  }
  
  public interface BindingBuilder
  {
    BindingBuilder to(Class api);
    BindingBuilder to(Key key);

    BindingBuilder priority(int priority);
    BindingBuilder scope(Class scopeType);
  }
  
  public interface IncludeInject extends IncludeGenerator
  {
  }
  
  public interface InjectAutoBind
  {
     Provider provider(Injector manager, Key key);
    
    default  Provider provider(Injector injector, 
                                     InjectionPoint ip)
    {
      return provider(injector, ip.key());
    }
  }
}