Normand Briere
2017-05-07 314b34423070cf127464da79a53cddf6b1c38587
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package aurelienribon.tweenengine;
 
import java.util.ArrayList;
 
/**
 * A light pool of objects that can be resused to avoid allocation.
 * Based on Nathan Sweet pool implementation
 */
abstract class Pool<T> {
   private final ArrayList<T> objects;
   private final Callback<T> callback;
 
   protected abstract T create();
 
   public Pool(int initCapacity, Callback<T> callback) {
       this.objects = new ArrayList<T>(initCapacity);
       this.callback = callback;
   }
 
   public T get() {
       T obj = objects.isEmpty() ? create() : objects.remove(objects.size()-1);
       return obj;
   }
 
   public void free(T obj) {
       if (!objects.contains(obj)) {
           if (callback != null) callback.onPool(obj);
           objects.add(obj);
       }
   }
 
   public void clear() {
       objects.clear();
   }
 
   public int size() {
       return objects.size();
   }
 
   public void ensureCapacity(int minCapacity) {
       objects.ensureCapacity(minCapacity);
   }
 
   public interface Callback<T> {
       public void onPool(T obj);
   }
}