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

org.androidtransfuse.bootstrap.Bootstraps Maven / Gradle / Ivy

There is a newer version: 0.3.0-beta-14
Show newest version
/**
 * Copyright 2011-2015 John Ericksen
 *
 * 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 org.androidtransfuse.bootstrap;

import org.androidtransfuse.scope.Scope;
import org.androidtransfuse.scope.ScopeKey;
import org.androidtransfuse.scope.Scopes;
import org.androidtransfuse.util.GeneratedCodeRepository;
import org.androidtransfuse.util.Namer;
import org.androidtransfuse.util.Providers;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

/**
 * @author John Ericksen
 */
public final class Bootstraps {

    public static final String BOOTSTRAPS_INJECTOR_PACKAGE = "org.androidtransfuse.bootstrap";
    public static final String BOOTSTRAPS_INJECTOR_NAME = Namer.name("Bootstraps").append("Factory").build();
    public static final String BOOTSTRAPS_INJECTOR_METHOD = "inject";
    public static final String BOOTSTRAPS_INJECTOR_GET = "get";
    public static final String IMPL_EXT = "Bootstrap";

    private static final GeneratedCodeRepository REPOSITORY =
            new GeneratedCodeRepository(BOOTSTRAPS_INJECTOR_PACKAGE, BOOTSTRAPS_INJECTOR_NAME) {
                @Override
                public BootstrapInjector findClass(Class clazz) {

                    try {
                        Class bootstrapClass = Class.forName(getGeneratedClassName(clazz));
                        return new BootstrapInjectorReflectionProxy(bootstrapClass);
                    } catch (ClassNotFoundException e) {
                        return null;
                    }

                }

                @Override
                public String getGeneratedClassName(Class clazz){
                    return Namer.name(clazz.getName()).append(IMPL_EXT).build();
                }

                @Override
                public String getGeneratedTypeName() {
                    return "@Bootstrap";
                }
            };

    private Bootstraps(){
        //private utility constructor
    }

    @SuppressWarnings("unchecked")
    public static  void inject(T input){
        REPOSITORY.get(input.getClass()).inject(input);
    }

    @SuppressWarnings("unchecked")
    public static  BootstrapInjector getInjector(Class clazz){
        return REPOSITORY.get(clazz);
    }

    public interface BootstrapInjector{

        void inject(T input);

        void inject(Scopes scopes, T input);

         BootstrapInjector add(Class scope, ScopeKey bindType, S instance);
    }

    public abstract static class BootstrapsInjectorAdapter implements BootstrapInjector{

        public static final String SCOPE_SINGLETONS_METHOD = "scopeSingletons";

        private final Map , Map> scoped = new HashMap , Map>();

        public  BootstrapInjector add(Class scope, ScopeKey bindType, S instance){
            if(!scoped.containsKey(scope)){
                scoped.put(scope, new HashMap());
            }
            scoped.get(scope).put(bindType, instance);
            return this;
        }

        @SuppressWarnings("unchecked")
        protected void scopeSingletons(Scopes scopes){
            for (Map.Entry, Map> scopedEntry : scoped.entrySet()) {
                Scope scope = scopes.getScope(scopedEntry.getKey());

                if(scope != null){
                    for (Map.Entry scopingEntry : scopedEntry.getValue().entrySet()) {
                        scope.getScopedObject(scopingEntry.getKey(), Providers.of(scopingEntry.getValue()));
                    }
                }
            }
        }
    }
}