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.util; kitaev@213: kitaev@213: import java.lang.ref.SoftReference; kitaev@213: import java.util.WeakHashMap; kitaev@213: kitaev@213: kitaev@213: /** kitaev@213: * Produces path from strings and caches result for reuse kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class PathPool implements Path.Source { kitaev@213: private final WeakHashMap> cache; kitaev@213: private final PathRewrite pathRewrite; kitaev@213: kitaev@213: public PathPool(PathRewrite rewrite) { kitaev@213: pathRewrite = rewrite; kitaev@213: cache = new WeakHashMap>(); kitaev@213: } kitaev@213: kitaev@213: public Path path(String p) { kitaev@213: p = pathRewrite.rewrite(p); kitaev@213: return get(p, true); kitaev@213: } kitaev@213: kitaev@213: // pipes path object through cache to reuse instance, if possible kitaev@213: public Path path(Path p) { kitaev@213: String s = pathRewrite.rewrite(p.toString()); kitaev@213: Path cached = get(s, false); kitaev@213: if (cached == null) { kitaev@213: cache.put(s, new SoftReference(cached = p)); kitaev@213: } kitaev@213: return cached; kitaev@213: } kitaev@213: kitaev@213: // XXX what would be parent of an empty path? kitaev@213: // Path shall have similar functionality kitaev@213: public Path parent(Path path) { kitaev@213: if (path.length() == 0) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: for (int i = path.length() - 2 /*if path represents a dir, trailing char is slash, skip*/; i >= 0; i--) { kitaev@213: if (path.charAt(i) == '/') { kitaev@213: return get(path.subSequence(0, i+1).toString(), true); kitaev@213: } kitaev@213: } kitaev@213: return get("", true); kitaev@213: } kitaev@213: kitaev@213: private Path get(String p, boolean create) { kitaev@213: SoftReference sr = cache.get(p); kitaev@213: Path path = sr == null ? null : sr.get(); kitaev@213: if (path == null) { kitaev@213: if (create) { kitaev@213: path = Path.create(p); kitaev@213: cache.put(p, new SoftReference(path)); kitaev@213: } else if (sr != null) { kitaev@213: // cached path no longer used, clear cache entry - do not wait for RefQueue to step in kitaev@213: cache.remove(p); kitaev@213: } kitaev@213: } kitaev@213: return path; kitaev@213: } kitaev@213: }