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

com.artemis.ComponentPool Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.artemis;

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);
	}

	@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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy