source: other-projects/the-macronizer/trunk/src/java/util/Pool.java@ 29855

Last change on this file since 29855 was 29855, checked in by davidb, 9 years ago

John's code after refactoring by Tom over the summer of 2014/2015

File size: 1.2 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package util;
6
7import java.util.HashMap;
8import java.util.Map;
9
10/**
11 * A pool of objects. When the getCanonicalObject(Object) method is invoked, if
12 * the pool contains an Object equal to this Object as determined by the
13 * equals(Object) method, then the Object from the pool is returned. Otherwise,
14 * the Object is added to the pool and a reference to this Object is returned.
15 *
16 * @author John Cocks
17 */
18public class Pool<T> {
19
20 private Map<T, T> pool;
21
22 /**
23 * Constructs a new Pool which is initially empty.
24 */
25 public Pool() {
26 pool = new HashMap<T, T>();
27 }
28
29 /**
30 * Returns a canonical representation of the Object.
31 * @param obj The Object to pool.
32 * @return The canonical representation of the Object.
33 */
34 public T getCanonicalObject(T obj) {
35 if (pool.containsKey(obj)) {
36 return pool.get(obj);
37 } else {
38 pool.put(obj, obj);
39 return obj;
40 }
41 }
42
43 /**
44 * Removes all pooled Objects from this Pool.
45 */
46 public void clear() {
47 pool.clear();
48 }
49}
Note: See TracBrowser for help on using the repository browser.