Skip to content

Commit

Permalink
Configuration refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemures committed Aug 22, 2016
1 parent 4431051 commit 2b57474
Show file tree
Hide file tree
Showing 23 changed files with 256 additions and 189 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.smoothie;

import android.app.Application;
import toothpick.Configuration;
import toothpick.configuration.Configuration;
import toothpick.Scope;
import toothpick.Toothpick;
import toothpick.registries.FactoryRegistryLocator;
Expand All @@ -18,7 +18,7 @@ public void onCreate() {
// Please note that the fully qualified name should be used instead of an import.
// (see https://github.com/stephanenicolas/toothpick/wiki/Factory-and-Member-Injector-registries)
// If you're not using the reflection free configuration, the next 3 lines can be omitted
Configuration.setConfiguration(Configuration.reflectionFree());
Configuration.setConfiguration(Configuration.production().reflectionFree());
MemberInjectorRegistryLocator.setRootRegistry(new com.example.smoothie.MemberInjectorRegistry());
FactoryRegistryLocator.setRootRegistry(new com.example.smoothie.FactoryRegistry());

Expand Down
171 changes: 0 additions & 171 deletions toothpick-runtime/src/main/java/toothpick/Configuration.java

This file was deleted.

1 change: 1 addition & 0 deletions toothpick-runtime/src/main/java/toothpick/ScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.inject.Provider;
import toothpick.config.Binding;
import toothpick.config.Module;
import toothpick.configuration.Configuration;
import toothpick.registries.FactoryRegistryLocator;

import static java.lang.String.format;
Expand Down
1 change: 1 addition & 0 deletions toothpick-runtime/src/main/java/toothpick/Toothpick.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package toothpick;

import java.util.concurrent.ConcurrentHashMap;
import toothpick.configuration.Configuration;

/**
* Main class to access toothpick features.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package toothpick.configuration;

import toothpick.Factory;
import toothpick.MemberInjector;
import toothpick.config.Binding;

/**
* Strategy pattern that allows to change various behaviors of Toothpick.
* The default configuration is {@link #production()} and {@link #reflection()}.
* A custom configuration can be created and used by toothpick,
* it is even possible to use a composition of the built-in configurations.
*/
public class Configuration {

public static Configuration instance = new Configuration();

private ReflectionConfiguration reflectionConfiguration = new ReflectionOnConfiguration();
private RuntimeCheckConfiguration runtimeCheckConfiguration = new RuntimeCheckOffConfiguration();

/**
* Allows to pass custom configurations.
*
* @param configuration the configuration to use
*/
public static void setConfiguration(Configuration configuration) {
instance = configuration;
}

/**
* Performs many runtime checks. This configuration
* reduces performance. It should be used only during development.
* The checks performed are:
* <ul>
* <li>cycle detection: check that not 2 classes depend on each other. Note that if of them uses a Lazy instance
* of the other or a Producer, then there is no such cycle.</li>
* <li>illegal binding detection: check no scope annotated class is used as the target of a binding.</li>
* </ul>
*
* @return a development configuration.
*/
public static Configuration development() {
final Configuration configuration = new Configuration();
configuration.runtimeCheckConfiguration = new RuntimeCheckOnConfiguration();
return configuration;
}

/**
* Performs no runtime checks. This configuration
* is faster than {@link #development()}.
* It can be used in production.
*
* @return a production configuration.
*/
public static Configuration production() {
final Configuration configuration = new Configuration();
configuration.runtimeCheckConfiguration = new RuntimeCheckOffConfiguration();
return configuration;
}

/**
* Set reflection mode.
* It is slower than {@link #reflectionFree()} but it does not
* need any additional setup of the annotation processors.
* It can be used in production or development.
*
* @return a configuration set up to use reflection.
*/
public Configuration reflection() {
this.reflectionConfiguration = new ReflectionOnConfiguration();
return this;
}

/**
* Set reflection free mode.
* It is faster than {@link #reflection()} but it needs
* some additional setup of the annotation processors.
* It can be used in production or development.
*
* @return an optimized reflection free configuration.
*/
public Configuration reflectionFree() {
this.reflectionConfiguration = new ReflectionOffConfiguration();
return this;
}

public void checkIllegalBinding(Binding binding) {
runtimeCheckConfiguration.checkIllegalBinding(binding);
}

public void checkCyclesStart(Class clazz) {
runtimeCheckConfiguration.checkCyclesStart(clazz);
}

public void checkCyclesEnd(Class clazz) {
runtimeCheckConfiguration.checkCyclesEnd(clazz);
}

public <T> Factory<T> getFactory(Class<T> clazz) {
return reflectionConfiguration.getFactory(clazz);
}

public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {
return reflectionConfiguration.getMemberInjector(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package toothpick;
package toothpick.configuration;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package toothpick;
package toothpick.configuration;

/**
* Thrown when a binding is illegal.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package toothpick.configuration;

import toothpick.Factory;
import toothpick.MemberInjector;

interface ReflectionConfiguration {
<T> Factory<T> getFactory(Class<T> clazz);
<T> MemberInjector<T> getMemberInjector(Class<T> clazz);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package toothpick.configuration;

import toothpick.Factory;
import toothpick.MemberInjector;
import toothpick.registries.FactoryRegistryLocator;
import toothpick.registries.MemberInjectorRegistryLocator;

class ReflectionOffConfiguration implements ReflectionConfiguration {
@Override
public <T> Factory<T> getFactory(Class<T> clazz) {
return FactoryRegistryLocator.getFactoryUsingRegistries(clazz);
}

@Override
public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {
return MemberInjectorRegistryLocator.getMemberInjectorUsingRegistries(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package toothpick.configuration;

import toothpick.Factory;
import toothpick.MemberInjector;
import toothpick.registries.FactoryRegistryLocator;
import toothpick.registries.MemberInjectorRegistryLocator;

class ReflectionOnConfiguration implements ReflectionConfiguration {
@Override
public <T> Factory<T> getFactory(Class<T> clazz) {
return FactoryRegistryLocator.getFactoryUsingReflection(clazz);
}

@Override
public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {
return MemberInjectorRegistryLocator.getMemberInjectorUsingReflection(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package toothpick.configuration;

import toothpick.config.Binding;

interface RuntimeCheckConfiguration {
void checkIllegalBinding(Binding binding);
void checkCyclesStart(Class clazz);
void checkCyclesEnd(Class clazz);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package toothpick.configuration;

import toothpick.config.Binding;

class RuntimeCheckOffConfiguration implements RuntimeCheckConfiguration {
@Override
public void checkIllegalBinding(Binding binding) {
}

@Override
public void checkCyclesStart(Class clazz) {
}

@Override
public void checkCyclesEnd(Class clazz) {
}
}
Loading

0 comments on commit 2b57474

Please sign in to comment.