Mercurial > jhg
comparison src/org/tmatesoft/hg/util/PathPool.java @ 64:19e9e220bf68
Convenient commands constitute hi-level API. org.tmatesoft namespace, GPL2 statement
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 21 Jan 2011 05:56:43 +0100 |
parents | |
children | a3a2e5deb320 |
comparison
equal
deleted
inserted
replaced
63:a47530a2ea12 | 64:19e9e220bf68 |
---|---|
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@svnkit.com | |
16 */ | |
17 package org.tmatesoft.hg.util; | |
18 | |
19 import java.lang.ref.SoftReference; | |
20 import java.util.WeakHashMap; | |
21 | |
22 import org.tmatesoft.hg.core.Path; | |
23 | |
24 /** | |
25 * | |
26 * @author Artem Tikhomirov | |
27 * @author TMate Software Ltd. | |
28 */ | |
29 public class PathPool { | |
30 private final WeakHashMap<String, SoftReference<Path>> cache; | |
31 private final PathRewrite pathRewrite; | |
32 | |
33 public PathPool(PathRewrite rewrite) { | |
34 pathRewrite = rewrite; | |
35 cache = new WeakHashMap<String, SoftReference<Path>>(); | |
36 } | |
37 | |
38 public Path path(String p) { | |
39 p = pathRewrite.rewrite(p); | |
40 SoftReference<Path> sr = cache.get(p); | |
41 Path path = sr == null ? null : sr.get(); | |
42 if (path == null) { | |
43 path = Path.create(p); | |
44 cache.put(p, new SoftReference<Path>(path)); | |
45 } | |
46 return path; | |
47 } | |
48 | |
49 // XXX what would be parent of an empty path? | |
50 // Path shall have similar functionality | |
51 public Path parent(Path path) { | |
52 if (path.length() == 0) { | |
53 throw new IllegalArgumentException(); | |
54 } | |
55 for (int i = path.length() - 2 /*if path represents a dir, trailing char is slash, skip*/; i >= 0; i--) { | |
56 if (path.charAt(i) == '/') { | |
57 return get(path.subSequence(0, i+1).toString(), true); | |
58 } | |
59 } | |
60 return get("", true); | |
61 } | |
62 | |
63 private Path get(String p, boolean create) { | |
64 SoftReference<Path> sr = cache.get(p); | |
65 Path path = sr == null ? null : sr.get(); | |
66 if (path == null) { | |
67 if (create) { | |
68 path = Path.create(p); | |
69 cache.put(p, new SoftReference<Path>(path)); | |
70 } else if (sr != null) { | |
71 // cached path no longer used, clear cache entry - do not wait for RefQueue to step in | |
72 cache.remove(p); | |
73 } | |
74 } | |
75 return path; | |
76 } | |
77 } |