Android Open Source - InfuseFactory Infuser Global






From Project

Back to project page InfuseFactory.

License

The source code is released under:

/** * Copyright (c) 2014 Lazu Ioan-Bogdan * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from ...

If you think the Android project InfuseFactory listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.factory.infuse.internal;
/*from  ww w .  ja v a  2 s.c  o  m*/
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.support.v4.app.Fragment;

import com.factory.InfuseFactory;
import com.factory.infuse.Infuser;
import com.factory.infuse.internal.InfuseReflection.ScopeType;
import com.factory.infuse.internal.base.AbsInfuser;

public class InfuserGlobal extends AbsInfuser implements Infuser {

  @Override
  @SuppressWarnings("unchecked")
  public <T> T resolveInstance(Class<T> clazz) {
    if(existsGlobally(clazz)) {
      return (T)extractGlobally(clazz);
    }
    
    ScopeType scope = reflection.extractClassScope(clazz);
    
    if(scope == ScopeType.SINGLETON_GLOBAL) {
      return resolveInstanceSingleton(clazz);
    } else {
      return getInstanceWithScope(clazz, buildArguments(), scope); 
    }
  }    
  
  @Override
  @SuppressWarnings("unchecked")
  public <T> T resolveInstance(Class<T> clazz, Object outerInstance) {
    if(existsGlobally(clazz)) {
      return (T)extractGlobally(clazz);
    }
    
    ScopeType scope = reflection.extractClassScope(clazz);
    
    if(scope == ScopeType.SINGLETON_GLOBAL) {
      return resolveInstanceSingleton(clazz);
    } else {
      return getInstanceWithScope(clazz, buildArguments(outerInstance), scope);
    }
  }
  
  @Override
  @SuppressWarnings("unchecked")
  public <T> T resolveInstanceSingleton(Class<T> clazz) {
    if(globalScope.peekScoped(clazz)) {
      return (T) globalScope.obtainScoped(clazz);
    } else {
      return getInstanceWithScope(clazz, buildArguments(), ScopeType.SINGLETON_GLOBAL);
    }
  }
  
  protected boolean existsGlobally(Class<?> clazz) {
    return globalScope.peekScoped(clazz);
  }
  
  protected Object extractGlobally(Class<?> clazz) {
    if(globalScope.peekScoped(clazz)) return globalScope.obtainScoped(clazz);
    else throw new IllegalStateException("Scould never happen!");
  }
  
  @Override
  protected Map<Class<?>, Object> buildArguments(Object... additional) {
    Map<Class<?>, Object> args = new HashMap<Class<?>, Object>();
    
    args.putAll(globalScope.obtainScope());
    for (int i = 0; i < additional.length; i++) {
      args.put(additional[i].getClass(), additional[i]);
    }
    
    return args;
  }
  
  @SuppressWarnings("unchecked")
  private <T> T getInstanceWithScope(Class<T> clazz, Map<Class<?>, Object> arguments, ScopeType scope) {
    if(scope == ScopeType.SINGLETON_SCOPED) {
      try {
        throw new RuntimeException("@ScopedSingleton not allowed for" +
            " global infuser for class: " + clazz.getSimpleName() +
            " . Use @Singleton instead.");
      } catch(RuntimeException e) {
        if(InfuseFactory.TOUGH) throw e;
        else if(InfuseFactory.DEBUG) e.printStackTrace();
      }
    }
    
    T result = null;
    
    if(scope == ScopeType.SINGLETON_GLOBAL && globalScope.peekScoped(clazz)) {
      return (T)globalScope.obtainScoped(clazz);
    }
    
    result = reflection.getClassInstance(clazz, arguments);
    
    if(scope == ScopeType.SINGLETON_GLOBAL) {
      globalScope.markScoped(clazz, result);
    }
    
    return result;
  }
  
  public void infuseMembers(Fragment fragment) {
    throw new UnsupportedOperationException("Must be called on a scoped infuser!");
  }  
  
  public void infuseViews(Fragment fragment) {
    throw new UnsupportedOperationException("Must be called on a scoped infuser!");
  }
  
  public void infuseMembers(Activity activity) {
    throw new UnsupportedOperationException("Must be called on a scoped infuser!");
  }
  
  public void infuseViews(Activity activity) {
    throw new UnsupportedOperationException("Must be called on a scoped infuser!");    
  }

  private static Infuser instance = new InfuserGlobal();
  public static Infuser getInfuserInstance() {
    return instance;
  }
}




Java Source Code List

com.example.infuseexample.ExampleApplication.java
com.example.infuseexample.MainActivity.java
com.example.infuseexample.MainFragmentActivity.java
com.example.other.mock.DatabaseAdapter.java
com.example.other.mock.NetworkingAdapter.java
com.example.other.mock.PreferencesAdapter.java
com.example.other.ui.adapter.TestListAdapter.java
com.example.other.ui.listener.HideListener.java
com.example.other.ui.listener.ListListener.java
com.factory.InfuseFactory.java
com.factory.android.InfuseActivityActionBar.java
com.factory.android.InfuseActivity.java
com.factory.android.InfuseApplication.java
com.factory.android.InfuseFragment.java
com.factory.infuse.InfuseCreator.java
com.factory.infuse.Infuser.java
com.factory.infuse.Scope.java
com.factory.infuse.annotation.InfuseView.java
com.factory.infuse.annotation.Infuse.java
com.factory.infuse.annotation.InitializeViews.java
com.factory.infuse.annotation.Initialize.java
com.factory.infuse.annotation.Instantiate.java
com.factory.infuse.annotation.ScopedSingleton.java
com.factory.infuse.annotation.Singleton.java
com.factory.infuse.annotation.bindings.BindAdapter.java
com.factory.infuse.annotation.bindings.BindOnClick.java
com.factory.infuse.annotation.bindings.BindOnItemClick.java
com.factory.infuse.annotation.bindings.BindOnItemLongClick.java
com.factory.infuse.annotation.bindings.BindOnScroll.java
com.factory.infuse.annotation.bindings.BindOnText.java
com.factory.infuse.annotation.bindings.BindOnTouch.java
com.factory.infuse.internal.InfuseReflection.java
com.factory.infuse.internal.InfuserGlobal.java
com.factory.infuse.internal.InfuserScoped.java
com.factory.infuse.internal.ViewResolver.java
com.factory.infuse.internal.base.AbsInfuser.java
com.factory.infuse.internal.lock.SharedLock.java
com.factory.infuse.internal.scope.GlobalScope.java
com.factory.infuse.internal.scope.LocalScope.java
com.factory.infuse.internal.scope.ScopeFactory.java
com.factory.java.InfuseObject.java