Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgrefer committed Dec 24, 2015
1 parent 7babcf6 commit 0a2dd2f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;

import io.freefair.android.injection.InjectionModule;
import io.freefair.android.injection.injector.InjectionContainer;
import io.freefair.android.injection.InjectorProvider;
import io.freefair.android.util.function.Suppliers;
Expand All @@ -12,10 +13,9 @@
@SuppressWarnings("unused")
public class InjectionApplication extends Application implements InjectorProvider {

private InjectionContainer injector;
private InjectionContainer injector = InjectionContainer.getInstance();

public InjectionApplication() {
injector = InjectionContainer.getInstance();
injector.registerSupplier(InjectionApplication.class, Suppliers.of(this));
}

Expand All @@ -28,4 +28,8 @@ public void onCreate() {
public InjectionContainer getInjector() {
return injector;
}

public void addModule(InjectionModule injectionModule) {
injectionModule.configure(getInjector());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import io.freefair.android.injection.annotation.InjectAttribute;
import io.freefair.android.injection.annotation.InjectResource;

/**
* Storage for Bindings between Fields and {@link android.view.View Views}, Fields and Attributes and Fields and Resources
*/
public class Bindings {

private static WeakHashMap<Class<?>, Map<Field, InjectAttribute>> attributeBindings = new WeakHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import java.util.Map;
import java.util.Set;

import io.freefair.android.injection.DefaultModule;
import io.freefair.android.injection.InjectionProvider;
import io.freefair.android.injection.TypeRegistration;
import io.freefair.android.util.function.Supplier;
import io.freefair.android.util.function.Optional;
import io.freefair.android.injection.annotation.Inject;
import io.freefair.android.injection.exceptions.InjectionException;
import io.freefair.android.util.function.Optional;
import io.freefair.android.util.function.Supplier;

public class InjectionContainer extends Injector {

Expand All @@ -25,7 +23,6 @@ public class InjectionContainer extends Injector {
public static InjectionContainer getInstance() {
if (instance == null) {
instance = new InjectionContainer();
new DefaultModule().configure(instance);
}
return instance;
}
Expand All @@ -39,14 +36,17 @@ private InjectionContainer() {
injectionFactories = new HashSet<>();
}

@SuppressWarnings("unused")
public <IMPL extends IFACE, IFACE> void registerType(Class<IMPL> impl, final Class<IFACE> iFace) {
this.registerProvider(new TypeRegistration<>(impl, iFace));
}

@SuppressWarnings("unused")
public <T> void registerSupplier(Class<T> type, Supplier<? extends T> supplier) {
injectionSupplier.put(type, supplier);
}

@SuppressWarnings("unused")
public void registerProvider(InjectionProvider injectionProvider) {
injectionFactories.add(injectionProvider);
}
Expand Down Expand Up @@ -127,4 +127,26 @@ private <T> Optional<T> queryFactories(Class<T> type, Object instance) {
}
return Optional.empty();
}

public static class TypeRegistration<IMPL extends IFACE, IFACE> implements InjectionProvider {

private final Class<IMPL> implClass;
private final Class<IFACE> iFace;

public TypeRegistration(Class<IMPL> implClass, Class<IFACE> iFace){
this.implClass = implClass;
this.iFace = iFace;
}

@Override
public boolean canProvide(Class<?> clazz) {
return clazz.isAssignableFrom(iFace);
}

@Override
@SuppressWarnings("unchecked")
public <T> T provide(Class<? super T> clazz, Object instance, Injector injector) {
return (T) injector.resolveValue(implClass, instance);
}
}
}

0 comments on commit 0a2dd2f

Please sign in to comment.