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

com.artemis.ComponentPool Maven / Gradle / Ivy

The newest version!
package com.artemis;

import com.artemis.annotations.InitialPoolSize;
import com.artemis.utils.Bag;
import com.artemis.utils.reflect.ClassReflection;
import com.artemis.utils.reflect.ReflectionException;

public class ComponentPool {
	private final Bag cache;
	private Class type;

	ComponentPool(Class type) {
		this.type = type;
		cache = new Bag(type);

		for(int i = initialPoolSize(); i > 0; i--) {
			try {
				cache.add(ClassReflection.newInstance(type));
			} catch (ReflectionException e) {
				throw new InvalidComponentException(type, e.getMessage(), e);
			}
		}
	}

	private int initialPoolSize() {
		if(type.isAnnotationPresent(InitialPoolSize.class)) {
			final InitialPoolSize initialPoolSize = type.getAnnotation(InitialPoolSize.class);
			return initialPoolSize.size();
		}
		//Default behavior
		return 0;
	}

	@SuppressWarnings("unchecked")
	 T obtain() {
		try {
			return (T) ((cache.size() > 0)
				? cache.removeLast()
				: ClassReflection.newInstance(type));
		} catch (ReflectionException e) {
			throw new InvalidComponentException(type, e.getMessage(), e);
		}
	}

	void free(T component) {
		component.reset();
		cache.add(component);
	}

	int cacheSize() {
		return cache.size();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy