Skip to content

Commit

Permalink
[Feature & Fix] New API getContext & removed unused fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rh-id committed Nov 27, 2021
1 parent 46780c3 commit 93a2803
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
26 changes: 13 additions & 13 deletions provider/src/main/java/m/co/rh/id/aprovider/DefaultProvider.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package m.co.rh.id.aprovider;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.ArrayList;
Expand Down Expand Up @@ -36,21 +34,19 @@ private static synchronized ThreadPoolExecutor initThreadPool() {
private Map<Class, Object> mObjectMap;
private List<ProviderModule> mModuleList;
private List<LazyFutureProviderRegister> mAsyncRegisterList;
private Handler mHandler;
private ThreadPoolExecutor mThreadPoolExecutor;
private boolean mIsDisposed;

DefaultProvider(Context context, ProviderModule rootModule) {
this(context, rootModule, new Handler(Looper.getMainLooper()), initThreadPool());
this(context, rootModule, initThreadPool());
}

DefaultProvider(Context context, ProviderModule rootModule, Handler handler, ThreadPoolExecutor threadPoolExecutor) {
DefaultProvider(Context context, ProviderModule rootModule, ThreadPoolExecutor threadPoolExecutor) {
mContext = context;
mObjectMap = new ConcurrentHashMap<>();
mObjectMap.put(ProviderRegistry.class, this);
mModuleList = Collections.synchronizedList(new ArrayList<>());
mAsyncRegisterList = Collections.synchronizedList(new ArrayList<>());
mHandler = handler;
mThreadPoolExecutor = threadPoolExecutor;
registerModule(rootModule);
// after all things are registered, trigger load for all futures
Expand Down Expand Up @@ -111,12 +107,9 @@ public <I> ProviderValue<I> tryLazyGet(Class<I> clazz) {
return () -> tryGet(clazz);
}

private <I> I exactGet(Class<I> clazz) {
Object result = mObjectMap.get(clazz);
if (result != null) {
return processObject(result);
}
throw new DefaultProviderNullPointerException(clazz.getName() + " not found");
@Override
public Context getContext() {
return mContext;
}

@Override
Expand All @@ -136,7 +129,6 @@ public synchronized void dispose() {
mObjectMap = null;
mAsyncRegisterList.clear();
mAsyncRegisterList = null;
mHandler = null;
mThreadPoolExecutor = null;
mContext = null;
}
Expand Down Expand Up @@ -171,6 +163,14 @@ public <I> void registerFactory(Class<I> clazz, ProviderValue<I> providerValue)
register(new FactoryProviderRegister<>(clazz, providerValue));
}

private <I> I exactGet(Class<I> clazz) {
Object result = mObjectMap.get(clazz);
if (result != null) {
return processObject(result);
}
throw new DefaultProviderNullPointerException(clazz.getName() + " not found");
}

private synchronized void checkDisposed() {
if (mIsDisposed) {
throw new IllegalStateException("This provider was disposed, please create new instance");
Expand Down
5 changes: 5 additions & 0 deletions provider/src/main/java/m/co/rh/id/aprovider/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ static Provider createProvider(Context context, ProviderModule rootModule) {
*/
<I> ProviderValue<I> tryLazyGet(Class<I> clazz);

/**
* Get the context that was supplied when creating this provider.
*/
Context getContext();

/**
* Clear all registered object from provider, and perform disposal/clean up of for all {@link ProviderModule}
* this provider will not be able to be used once this method is called, new one will need to be instantiated
Expand Down

0 comments on commit 93a2803

Please sign in to comment.