Mercurial > jhg
comparison hg4j/src/main/java/org/tmatesoft/hg/util/PathPool.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
author | Alexander Kitaev <kitaev@gmail.com> |
---|---|
date | Tue, 10 May 2011 10:52:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
212:edb2e2829352 | 213:6ec4af642ba8 |
---|---|
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@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.util; | |
18 | |
19 import java.lang.ref.SoftReference; | |
20 import java.util.WeakHashMap; | |
21 | |
22 | |
23 /** | |
24 * Produces path from strings and caches result for reuse | |
25 * | |
26 * @author Artem Tikhomirov | |
27 * @author TMate Software Ltd. | |
28 */ | |
29 public class PathPool implements Path.Source { | |
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 return get(p, true); | |
41 } | |
42 | |
43 // pipes path object through cache to reuse instance, if possible | |
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)); | |
49 } | |
50 return cached; | |
51 } | |
52 | |
53 // XXX what would be parent of an empty path? | |
54 // Path shall have similar functionality | |
55 public Path parent(Path path) { | |
56 if (path.length() == 0) { | |
57 throw new IllegalArgumentException(); | |
58 } | |
59 for (int i = path.length() - 2 /*if path represents a dir, trailing char is slash, skip*/; i >= 0; i--) { | |
60 if (path.charAt(i) == '/') { | |
61 return get(path.subSequence(0, i+1).toString(), true); | |
62 } | |
63 } | |
64 return get("", true); | |
65 } | |
66 | |
67 private Path get(String p, boolean create) { | |
68 SoftReference<Path> sr = cache.get(p); | |
69 Path path = sr == null ? null : sr.get(); | |
70 if (path == null) { | |
71 if (create) { | |
72 path = Path.create(p); | |
73 cache.put(p, new SoftReference<Path>(path)); | |
74 } else if (sr != null) { | |
75 // cached path no longer used, clear cache entry - do not wait for RefQueue to step in | |
76 cache.remove(p); | |
77 } | |
78 } | |
79 return path; | |
80 } | |
81 } |