tikhomirov@195: /* tikhomirov@195: * Copyright (c) 2011 TMate Software Ltd tikhomirov@195: * tikhomirov@195: * This program is free software; you can redistribute it and/or modify tikhomirov@195: * it under the terms of the GNU General Public License as published by tikhomirov@195: * the Free Software Foundation; version 2 of the License. tikhomirov@195: * tikhomirov@195: * This program is distributed in the hope that it will be useful, tikhomirov@195: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@195: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@195: * GNU General Public License for more details. tikhomirov@195: * tikhomirov@195: * For information on how to redistribute this software under tikhomirov@195: * the terms of a license other than GNU General Public License tikhomirov@195: * contact TMate Software at support@hg4j.com tikhomirov@195: */ tikhomirov@195: package org.tmatesoft.hg.internal; tikhomirov@195: tikhomirov@195: import java.util.HashMap; tikhomirov@195: tikhomirov@262: import org.tmatesoft.hg.util.SparseSet; tikhomirov@262: tikhomirov@195: /** tikhomirov@195: * Instance pooling. tikhomirov@195: * tikhomirov@195: * @author Artem Tikhomirov tikhomirov@195: * @author TMate Software Ltd. tikhomirov@195: */ tikhomirov@195: public class Pool { tikhomirov@262: private final HashMap unify; tikhomirov@262: // private final SparseSet unify = new SparseSet(); tikhomirov@262: tikhomirov@262: public Pool() { tikhomirov@262: unify = new HashMap(); tikhomirov@262: } tikhomirov@262: tikhomirov@262: public Pool(int sizeHint) { tikhomirov@262: if (sizeHint <= 0) { tikhomirov@262: unify = new HashMap(); tikhomirov@262: } else { tikhomirov@262: unify = new HashMap(sizeHint * 4 / 3, 0.75f); tikhomirov@262: } tikhomirov@262: } tikhomirov@195: tikhomirov@195: public T unify(T t) { tikhomirov@195: T rv = unify.get(t); tikhomirov@195: if (rv == null) { tikhomirov@195: // first time we see a new value tikhomirov@195: unify.put(t, t); tikhomirov@195: rv = t; tikhomirov@195: } tikhomirov@195: return rv; tikhomirov@195: } tikhomirov@196: tikhomirov@262: public boolean contains(T t) { tikhomirov@262: return unify.containsKey(t); tikhomirov@262: } tikhomirov@262: tikhomirov@262: public void record(T t) { tikhomirov@262: unify.put(t, t); tikhomirov@262: } tikhomirov@262: tikhomirov@262: public void clear() { tikhomirov@262: unify.clear(); tikhomirov@262: } tikhomirov@262: tikhomirov@262: public int size() { tikhomirov@262: return unify.size(); tikhomirov@262: } tikhomirov@262: tikhomirov@196: @Override tikhomirov@196: public String toString() { tikhomirov@196: StringBuilder sb = new StringBuilder(); tikhomirov@196: sb.append(Pool.class.getSimpleName()); tikhomirov@196: sb.append('<'); tikhomirov@262: // if (!unify.isEmpty()) { tikhomirov@262: // sb.append(unify.keySet().iterator().next().getClass().getName()); tikhomirov@262: // } tikhomirov@196: sb.append('>'); tikhomirov@196: sb.append(':'); tikhomirov@196: sb.append(unify.size()); tikhomirov@196: return sb.toString(); tikhomirov@196: } tikhomirov@195: }