Skip to content

Commit

Permalink
injector: Move builder to separate class and utils to separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Feb 3, 2017
1 parent b13f365 commit d915781
Show file tree
Hide file tree
Showing 9 changed files with 993 additions and 810 deletions.
Expand Up @@ -19,11 +19,8 @@
import com.speedment.common.injector.annotation.Execute;
import com.speedment.common.injector.annotation.ExecuteBefore;
import com.speedment.common.injector.annotation.Inject;
import com.speedment.common.injector.exception.NoDefaultConstructorException;
import com.speedment.common.injector.internal.InjectorImpl;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
Expand All @@ -40,16 +37,6 @@
*/
public interface Injector {

/**
* Returns a stream of all the instances associated with the specified class
* signature.
*
* @param <T> the class signature of the injectable type
* @param type the expected type
* @return the automatically resolved instances
*/
<T> Stream<T> stream(Class<T> type);

/**
* Returns the instance associated with the specified class signature.
* <p>
Expand All @@ -75,6 +62,16 @@ public interface Injector {
*/
<T> Optional<T> get(Class<T> type);

/**
* Returns a stream of all the instances associated with the specified class
* signature.
*
* @param <T> the class signature of the injectable type
* @param type the expected type
* @return the automatically resolved instances
*/
<T> Stream<T> stream(Class<T> type);

/**
* Returns a stream of all the injectable types that are accessible from
* this Injector.
Expand Down Expand Up @@ -113,15 +110,15 @@ public interface Injector {
*
* @return the new injector builder
*/
Builder newBuilder();
InjectorBuilder newBuilder();

/**
* Returns a new builder for the {@link Injector} interface that uses the
* default implementation.
*
* @return the builder
*/
static Builder builder() {
static InjectorBuilder builder() {
return InjectorImpl.builder();
}

Expand All @@ -132,159 +129,7 @@ static Builder builder() {
* @param classLoader the class loader to use
* @return the builder
*/
static Builder builder(ClassLoader classLoader) {
static InjectorBuilder builder(ClassLoader classLoader) {
return InjectorImpl.builder(classLoader);
}

/**
* Builder pattern for the {@link Injector} interface.
*/
interface Builder {

/**
* Appends a class that can be automatically dependency injected into
* other classes to the builder. Classes can be appended in any order.
* The final injection order will be determined once the
* {@link #build()}-method is called.
* <p>
* If a class has already been passed as injectable with the same
* InjectorKey, the previous one will be replaced by this new one. The
* old one will never be instantiated.
* <p>
* This method will not replace any previous injectables.
*
* @param injectableType the type that should be injectable
* @return a reference to this builder
*
* @throws NoDefaultConstructorException if the specified type does not
* have a default constructor.
*/
Builder put(Class<?> injectableType)
throws NoDefaultConstructorException;

/**
* Appends a class that can be automatically dependency injected into
* other classes to the builder. Classes can be appended in any order.
* The final injection order will be determined once the
* {@link #build()}-method is called.
* <p>
* If a class has already been passed as injectable with the same key,
* the previous one will be replaced by this new one. The old one will
* never be instantiated.
*
* @param key the key to check uniqueness with
* @param injectableType the type that should be injectable
* @return a reference to this builder
*
* @throws NoDefaultConstructorException if the specified type does not
* have a default constructor.
*/
Builder put(String key, Class<?> injectableType)
throws NoDefaultConstructorException;

/**
* Puts one or multiple classes contained in an InjectBundle that can be
* automatically dependency injected into other classes to the builder.
* Classes can be appended in any order. The final injection order will
* be determined once the {@link #build()}-method is called.
* <p>
* If an injectable class has already been passed as injectable with the
* same key, the previous one will be replaced by this new one. The old
* one will never be instantiated.
*
* @param bundleClass containing the injectable classes that shall be
* appended
* @return a reference to this builder
*
* @throws NoDefaultConstructorException if the specified type does not
* have a default constructor.
*/
Builder putInBundle(Class<? extends InjectBundle> bundleClass)
throws NoDefaultConstructorException;

/**
* Overrides a particular configuration parameter in the config file
* with the specified value.
*
* @param key the key to override
* @param value the new value
* @return a reference to this builder
*/
Builder putParam(String key, String value);

/**
* Sets the location of the configuration file.
*
* @param configFile the new config file location
* @return a reference to this builder
*/
Builder withConfigFileLocation(Path configFile);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#INITIALIZED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> Builder beforeInitialized(Class<T> injectableType, Consumer<T> action);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#RESOLVED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> Builder beforeResolved(Class<T> injectableType, Consumer<T> action);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#STARTED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> Builder beforeStarted(Class<T> injectableType, Consumer<T> action);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#STOPPED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> Builder beforeStopped(Class<T> injectableType, Consumer<T> action);

/**
* Builds the {@link Injector} instance, organizing the
* injectable instances based on their internal dependencies.
*
* @return the built instance
*
* @throws InstantiationException if one of the injectables can not
* be instantiated.
*/
Injector build() throws InstantiationException;
}
}
@@ -0,0 +1,157 @@
/**
*
* Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.common.injector;

import com.speedment.common.injector.exception.NoDefaultConstructorException;
import java.nio.file.Path;
import java.util.function.Consumer;

/**
* Builder pattern for the {@link Injector} interface.
*
* @author Emil Forslund
* @since 1.2.0
*/
public interface InjectorBuilder {

/**
* Appends a class that can be automatically dependency injected into
* other classes to the builder. Classes can be appended in any order.
* The final injection order will be determined once the
* {@link #build()}-method is called.
* <p>
* If a class has already been passed as injectable with the same
* InjectorKey, the previous one will be replaced by this new one. The
* old one will never be instantiated.
* <p>
* This method will not replace any previous injectables.
*
* @param injectableType the type that should be injectable
* @return a reference to this builder
*
* @throws NoDefaultConstructorException if the specified type does not
* have a default constructor.
*/
InjectorBuilder withComponent(Class<?> injectableType)
throws NoDefaultConstructorException;

/**
* Puts one or multiple classes contained in an InjectBundle that can be
* automatically dependency injected into other classes to the builder.
* Classes can be appended in any order. The final injection order will
* be determined once the {@link #build()}-method is called.
* <p>
* If an injectable class has already been passed as injectable with the
* same key, the previous one will be replaced by this new one. The old
* one will never be instantiated.
*
* @param bundleClass containing the injectable classes that shall be
* appended
* @return a reference to this builder
*
* @throws NoDefaultConstructorException if the specified type does not
* have a default constructor.
*/
InjectorBuilder withBundle(Class<? extends InjectBundle> bundleClass)
throws NoDefaultConstructorException;

/**
* Overrides a particular configuration parameter in the config file
* with the specified value.
*
* @param name the key to override
* @param value the new value
* @return a reference to this builder
*/
InjectorBuilder withParam(String name, String value);

/**
* Sets the location of the configuration file.
*
* @param configFile the new config file location
* @return a reference to this builder
*/
InjectorBuilder withConfigFileLocation(Path configFile);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#INITIALIZED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> InjectorBuilder beforeInitialized(Class<T> injectableType, Consumer<T> action);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#RESOLVED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> InjectorBuilder beforeResolved(Class<T> injectableType, Consumer<T> action);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#STARTED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> InjectorBuilder beforeStarted(Class<T> injectableType, Consumer<T> action);

/**
* Appends an action that must be executed sometime before specified
* type reaches the {@link State#STOPPED} state. The action will
* have access to the instance as it is configured. This is equivalent
* to using the {@link ExecuteBefore}-annotation on a method in the
* class.
*
* @param <T> the injectable type
* @param injectableType the injectable type
* @param action action to be applied before state is reached
* @return a reference to this builder
*/
<T> InjectorBuilder beforeStopped(Class<T> injectableType, Consumer<T> action);

/**
* Builds the {@link Injector} instance, organizing the
* injectable instances based on their internal dependencies.
*
* @return the built instance
*
* @throws InstantiationException if one of the injectables can not
* be instantiated.
*/
Injector build() throws InstantiationException;

}

0 comments on commit d915781

Please sign in to comment.