1b5840353SAdam Hornáček /* 2b5840353SAdam Hornáček * The contents of this file are Copyright (c) 2012, Swaranga Sarma, DZone MVB 3b5840353SAdam Hornáček * made available under free license, 4b5840353SAdam Hornáček * http://javawithswaranga.blogspot.com/2011/10/generic-and-concurrent-object-pool.html 5b5840353SAdam Hornáček * https://dzone.com/articles/generic-and-concurrent-object : "Feel free to use 6b5840353SAdam Hornáček * it, change it, add more implementations. Happy coding!" 7b5840353SAdam Hornáček * Copyright (c) 2017, Chris Fraire <cfraire@me.com>. 8b5840353SAdam Hornáček */ 9b5840353SAdam Hornáček 10*9805b761SAdam Hornáček package org.opengrok.indexer.util; 11b5840353SAdam Hornáček 12b5840353SAdam Hornáček /** 13b5840353SAdam Hornáček * Represents an abstract implementation of {@link ObjectPool} that defines the 14b5840353SAdam Hornáček * procedure of returning an object to the pool. 15b5840353SAdam Hornáček * @author Swaranga 16b5840353SAdam Hornáček * @param <T> the type of pooled objects. 17b5840353SAdam Hornáček */ 18b5840353SAdam Hornáček public abstract class AbstractObjectPool<T> implements ObjectPool<T> { 19b5840353SAdam Hornáček 20b5840353SAdam Hornáček /** 21b5840353SAdam Hornáček * Returns the object to the pool. 22b5840353SAdam Hornáček * The method first validates the object if it is 23b5840353SAdam Hornáček * re-usable and then puts returns it to the pool. 24b5840353SAdam Hornáček * 25b5840353SAdam Hornáček * If the object validation fails, 26b5840353SAdam Hornáček * {@link #handleInvalidReturn(java.lang.Object)} is called. 27b5840353SAdam Hornáček * Some implementations 28b5840353SAdam Hornáček * will try to create a new object 29b5840353SAdam Hornáček * and put it into the pool; however 30b5840353SAdam Hornáček * this behaviour is subject to change 31b5840353SAdam Hornáček * from implementation to implementation. 32b5840353SAdam Hornáček */ release(T t)33b5840353SAdam Hornáček public final void release(T t) { 34b5840353SAdam Hornáček if (isValid(t)) { 35b5840353SAdam Hornáček returnToPool(t); 36b5840353SAdam Hornáček } else { 37b5840353SAdam Hornáček handleInvalidReturn(t); 38b5840353SAdam Hornáček } 39b5840353SAdam Hornáček } 40b5840353SAdam Hornáček handleInvalidReturn(T t)41b5840353SAdam Hornáček protected abstract void handleInvalidReturn(T t); 42b5840353SAdam Hornáček returnToPool(T t)43b5840353SAdam Hornáček protected abstract void returnToPool(T t); 44b5840353SAdam Hornáček isValid(T t)45b5840353SAdam Hornáček protected abstract boolean isValid(T t); 46b5840353SAdam Hornáček } 47