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

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

package com.github.nill14.utils.init.impl;

import javax.inject.Provider;

import com.github.nill14.utils.init.api.IPojoInitializer;
import com.google.common.collect.ImmutableList;

@SuppressWarnings("serial")
public class ChainingPojoInitializer implements IPojoInitializer {
	
	private final ImmutableList> items;
	
	
	@SafeVarargs
	public ChainingPojoInitializer(IPojoInitializer... initializers) {
		items = ImmutableList.copyOf(initializers);
	}

	public ChainingPojoInitializer(ImmutableList> initializers) {
		items = initializers;
	}
	
	public static ChainingPojoInitializer defaultInitializer() {
		return new ChainingPojoInitializer<>(
				new AnnotationInjectInitializer(),
				new AnnotationLifecycleInitializer(),
				new EventBusPojoInitializer());
	}
	
	/**
	 * 
	 * @param extraInitializer The first initializer to execute
	 * @return
	 */
	public  ChainingPojoInitializer with(IPojoInitializer extraInitializer) {
		ImmutableList.Builder> builder = ImmutableList.builder();
		ImmutableList> initializers = builder.add(extraInitializer).addAll(items).build();
		return new ChainingPojoInitializer(initializers);
	}
	
	@Override
	public void init(Provider factory, T instance) {
		for (IPojoInitializer item : items) {
			item.init(factory, instance);
		}
	}

	@Override
	public void destroy(Provider factory, T instance) {
		for (IPojoInitializer item : items) {
			item.destroy(factory, instance);
		}
	}

}