comparison src/org/tmatesoft/hg/util/PathPool.java @ 141:8248aae33f7d

Adopt FileIterator moving towards WCStatusCollector parameterizing. Improved path handling, move 'em around
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 17 Feb 2011 04:08:34 +0100
parents 4a948ec83980
children 1792b37650f2
comparison
equal deleted inserted replaced
140:1c1891ad1c73 141:8248aae33f7d
19 import java.lang.ref.SoftReference; 19 import java.lang.ref.SoftReference;
20 import java.util.WeakHashMap; 20 import java.util.WeakHashMap;
21 21
22 22
23 /** 23 /**
24 * 24 * Produces path from strings and caches result for reuse
25 *
25 * @author Artem Tikhomirov 26 * @author Artem Tikhomirov
26 * @author TMate Software Ltd. 27 * @author TMate Software Ltd.
27 */ 28 */
28 public class PathPool { 29 public class PathPool implements Path.Source {
29 private final WeakHashMap<String, SoftReference<Path>> cache; 30 private final WeakHashMap<String, SoftReference<Path>> cache;
30 private final PathRewrite pathRewrite; 31 private final PathRewrite pathRewrite;
31 32
32 public PathPool(PathRewrite rewrite) { 33 public PathPool(PathRewrite rewrite) {
33 pathRewrite = rewrite; 34 pathRewrite = rewrite;
34 cache = new WeakHashMap<String, SoftReference<Path>>(); 35 cache = new WeakHashMap<String, SoftReference<Path>>();
35 } 36 }
36 37
37 public Path path(String p) { 38 public Path path(String p) {
38 p = pathRewrite.rewrite(p); 39 p = pathRewrite.rewrite(p);
39 SoftReference<Path> sr = cache.get(p); 40 return get(p, true);
40 Path path = sr == null ? null : sr.get(); 41 }
41 if (path == null) { 42
42 path = Path.create(p); 43 // pipes path object through cache to reuse instance, if possible
43 cache.put(p, new SoftReference<Path>(path)); 44 public Path path(Path p) {
45 String s = pathRewrite.rewrite(p.toString());
46 Path cached = get(s, false);
47 if (cached == null) {
48 cache.put(s, new SoftReference<Path>(cached = p));
44 } 49 }
45 return path; 50 return cached;
46 } 51 }
47 52
48 // XXX what would be parent of an empty path? 53 // XXX what would be parent of an empty path?
49 // Path shall have similar functionality 54 // Path shall have similar functionality
50 public Path parent(Path path) { 55 public Path parent(Path path) {