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

org.ak.trafficController.pool.ObjectPoolManager Maven / Gradle / Ivy

Go to download

This deals with different aspects like In memory queue, throttling, event management etc

There is a newer version: 2.1.0
Show newest version
package org.ak.trafficController.pool;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.logging.Logger;

import org.ak.trafficController.messaging.mem.InMemoryQueue;

public class ObjectPoolManager {
	
	static Logger LOGGER = Logger.getLogger(ObjectPoolManager.class.getName()); 
	
	Map map = new HashMap<>();
	InMemoryQueue cleanupChannel = new InMemoryQueue<>("cleanup");
	public ObjectPoolManager() {
		init();
	}
	public void init() {
		cleanupChannel.setDirectConsumer(t->{
			t.clean();
			addBackToPoolGeneric(t);
		});
	}
	public  void addObjectPool(Class c,ObjectPool pool) {
		map.put(c, pool);
	}
	public  ObjectPool getPool(Class cls, Supplier generator) {
		ObjectPool objectPool = map.get(cls);
		if (objectPool == null) {
			objectPool = new ObjectPool(generator);
			map.put(cls, objectPool);
		}
		return objectPool;
	}
	public  T getFromPool(Class cls, Supplier generator) {
		return getPool(cls, generator).getFromPool();
	}
	
	public  void addBackToPool(T obj) {
		cleanupChannel.add(obj);
	}
	
	public  void addBackToPoolGeneric(T obj) {
		ObjectPool objectPool = map.get(obj.getClass());
		if (!Objects.isNull(objectPool)) { 
			objectPool.addBackToPool(obj);
		}
	}
	
	static ObjectPoolManager opm = new ObjectPoolManager();
	public static ObjectPoolManager getInstance() {
		return opm;
	}
	
	public void printStats() {
		this.map.forEach((Class k, ObjectPool v)->{
			LOGGER.info(k + " - " + v.getCount());
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy