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