comparison src/org/tmatesoft/hg/internal/IdentityPool.java @ 431:12f668401613

FIXMEs: awkward API refactored, what need to be internal got hidden; public aspects got captured in slim interfaces
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 29 Mar 2012 20:54:04 +0200
parents src/org/tmatesoft/hg/internal/Pool2.java@85b8efde5586
children
comparison
equal deleted inserted replaced
430:d280759c2a3f 431:12f668401613
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.Convertor;
20 import org.tmatesoft.hg.util.DirectHashSet;
21
22
23 /**
24 *
25 * @author Artem Tikhomirov
26 * @author TMate Software Ltd.
27 */
28 public class IdentityPool<T> implements Convertor<T> {
29 private final DirectHashSet<T> unify = new DirectHashSet<T>();
30
31 public IdentityPool() {
32 }
33
34 public IdentityPool(int sizeHint) {
35 }
36
37 public T mangle(T t) {
38 return unify(t);
39 }
40
41 public T unify(T t) {
42 T rv = unify.get(t);
43 if (rv == null) {
44 // first time we see a new value
45 unify.put(t);
46 rv = t;
47 }
48 return rv;
49 }
50
51 public boolean contains(T t) {
52 return unify.get(t) != null;
53 }
54
55 public void record(T t) {
56 unify.put(t);
57 }
58
59 public void clear() {
60 unify.clear();
61 }
62
63 public int size() {
64 return unify.size();
65 }
66
67 @Override
68 public String toString() {
69 StringBuilder sb = new StringBuilder();
70 sb.append(IdentityPool.class.getSimpleName());
71 sb.append('@');
72 sb.append(Integer.toString(System.identityHashCode(this)));
73 sb.append(' ');
74 sb.append(unify.toString());
75 return sb.toString();
76 }
77 }