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

com.github.nill14.utils.init.impl.UnscopedProvider Maven / Gradle / Ivy

The newest version!
package com.github.nill14.utils.init.impl;

import javax.inject.Provider;

import com.github.nill14.utils.init.api.BindingKey;
import com.github.nill14.utils.init.api.IBeanDescriptor;
import com.github.nill14.utils.init.api.IPojoFactory;
import com.github.nill14.utils.init.api.IPropertyResolver;
import com.google.common.base.Preconditions;

public final class UnscopedProvider implements Provider {
	
	private final IPropertyResolver resolver;
	private final IPojoFactory pojoFactory;
	private final CallerContext context;
	private final BindingKey bindingKey;

	public UnscopedProvider(IPropertyResolver resolver, BindingKey bindingKey, IPojoFactory pojoFactory, CallerContext context) {
		this.resolver = Preconditions.checkNotNull(resolver);
		this.bindingKey = Preconditions.checkNotNull(bindingKey);
		this.pojoFactory = Preconditions.checkNotNull(pojoFactory);
		this.context = Preconditions.checkNotNull(context);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T get() {
		
		if (context.isConstructing(bindingKey)) {
			return (T) context.getInstance(bindingKey); 
		
		} else {
			ConstructionContext constructionContext = context.startConstructing(bindingKey);
			try {
				T instance = pojoFactory.newInstance(resolver, context);
				constructionContext.setInstanceIfUnset(instance);
				return instance;
				
			} finally {
				constructionContext.finishConstructing();
			}
		}
	}
	
	public IPropertyResolver getResolver() {
		return resolver;
	}
	
	public IBeanDescriptor getDescriptor() {
		return pojoFactory.getDescriptor();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy