Pool.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package holeg.utility.pooling;
  2. import java.util.ArrayList;
  3. import java.util.stream.Stream;
  4. /**
  5. * An abstract implementation of simple pooling. To reduce constructing time and garbage collection
  6. * when many objects are dynamically needed. The Pool keeps track of the objects that are borrowed
  7. * via the {@link #get()} Method.
  8. *
  9. * @param <T> Type of the pooled objects.
  10. */
  11. public abstract class Pool<T> {
  12. int borrowedCount = 0;
  13. private int poolCapacity = 1000;
  14. private final ArrayList<T> poolList = new ArrayList<>(poolCapacity);
  15. private final ArrayList<T> borrowedList = new ArrayList<>(poolCapacity);
  16. /**
  17. * Constructs the Pool and populate it.
  18. */
  19. public Pool() {
  20. populatePool(poolCapacity);
  21. }
  22. /**
  23. * This method is called to create an object instance for the pool. This method is abstract to
  24. * handle all type of parametrized constructors of the pooled object type {@link T}.
  25. *
  26. * @return a new created instance of an object for pooling
  27. */
  28. public abstract T create();
  29. /**
  30. * Returns an object from the pool.
  31. *
  32. * @return the pooled object
  33. */
  34. public T get() {
  35. checkCapacity(borrowedCount);
  36. T poolObject = poolList.get(borrowedCount);
  37. borrowedCount++;
  38. borrowedList.add(poolObject);
  39. return poolObject;
  40. }
  41. /**
  42. * Resets the pool to start returning pooled objects from the beginning.
  43. * <br>
  44. * Attention:
  45. * <br>
  46. * All references to previous borrowed objects are still valid, but the {@link #get()} method will
  47. * start giving objects from the start of the pool. Be sure to don't use old references after the
  48. * call of this method.
  49. */
  50. public void clear() {
  51. borrowedList.clear();
  52. borrowedCount = 0;
  53. }
  54. /**
  55. * Returns the amount of borrowed objects.
  56. *
  57. * @return the amount of borrowed objects
  58. */
  59. public int getBorrowedCount() {
  60. return borrowedCount;
  61. }
  62. /**
  63. * Returns a stream of all objects that are currently borrowed.
  64. *
  65. * @return stream of borrowed objects
  66. */
  67. public Stream<T> getBorrowedStream() {
  68. return borrowedList.stream();
  69. }
  70. /**
  71. * Creates an amount of objects to the pool. The {@link #create()} method is called to instantiate
  72. * the objects.
  73. *
  74. * @param amount amount of new created objects
  75. */
  76. private void populatePool(int amount) {
  77. for (int i = 0; i < amount; i++) {
  78. poolList.add(create());
  79. }
  80. }
  81. /**
  82. * This method is called to check if the current capacity can hold the requested capacity. If the
  83. * current capacity cannot hold the requested capacity the actual capacity is increased to two
  84. * times the requested capacity.
  85. *
  86. * @param requestedCapacity the requested amount of pooled objects
  87. */
  88. private void checkCapacity(int requestedCapacity) {
  89. if (poolCapacity <= requestedCapacity) {
  90. int newSize = 2 * requestedCapacity;
  91. populatePool(newSize - poolCapacity);
  92. poolCapacity = 2 * requestedCapacity;
  93. }
  94. }
  95. }