comparison src/org/tmatesoft/hg/internal/Pool2.java @ 264:6bb5e7ed051a

Optimize memory usage (reduce number of objects instantiated) when pooling file names and nodeids during manifest parsing
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 19 Aug 2011 03:36:25 +0200
parents
children 85b8efde5586
comparison
equal deleted inserted replaced
263:31f67be94e71 264:6bb5e7ed051a
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import org.tmatesoft.hg.util.SparseSet;
20
21 /**
22 *
23 * @author Artem Tikhomirov
24 * @author TMate Software Ltd.
25 */
26 public class Pool2<T> {
27 private final SparseSet<T> unify = new SparseSet<T>();
28
29 public Pool2() {
30 }
31
32 public Pool2(int sizeHint) {
33 }
34
35 public T unify(T t) {
36 T rv = unify.get(t);
37 if (rv == null) {
38 // first time we see a new value
39 unify.put(t);
40 rv = t;
41 }
42 return rv;
43 }
44
45 public boolean contains(T t) {
46 return unify.get(t) != null;
47 }
48
49 public void record(T t) {
50 unify.put(t);
51 }
52
53 public void clear() {
54 unify.clear();
55 }
56
57 public int size() {
58 return unify.size();
59 }
60
61 public void x() {
62 unify.dump();
63 }
64
65 @Override
66 public String toString() {
67 StringBuilder sb = new StringBuilder();
68 sb.append(Pool2.class.getSimpleName());
69 sb.append('@');
70 sb.append(Integer.toString(System.identityHashCode(this)));
71 sb.append(' ');
72 sb.append(unify.toString());
73 return sb.toString();
74 }
75 }