comparison src/org/tmatesoft/hg/internal/PathPool.java @ 431:12f668401613

FIXMEs: awkward API refactored, what need to be internal got hidden; public aspects got captured in slim interfaces
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 29 Mar 2012 20:54:04 +0200
parents src/org/tmatesoft/hg/util/PathPool.java@a415fe296a50
children
comparison
equal deleted inserted replaced
430:d280759c2a3f 431:12f668401613
1 /*
2 * Copyright (c) 2011-2012 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.internal;
18
19 import java.lang.ref.SoftReference;
20 import java.util.WeakHashMap;
21
22 import org.tmatesoft.hg.util.Convertor;
23 import org.tmatesoft.hg.util.Path;
24 import org.tmatesoft.hg.util.PathRewrite;
25
26
27 /**
28 * Produces path from strings and caches (weakly) result for reuse
29 *
30 * @author Artem Tikhomirov
31 * @author TMate Software Ltd.
32 */
33 public class PathPool implements Path.Source, Convertor<Path> {
34 private final WeakHashMap<CharSequence, SoftReference<Path>> cache;
35 private final PathRewrite pathRewrite;
36
37 public PathPool(PathRewrite rewrite) {
38 pathRewrite = rewrite;
39 cache = new WeakHashMap<CharSequence, SoftReference<Path>>();
40 }
41
42 /*
43 * (non-Javadoc)
44 * @see org.tmatesoft.hg.util.Path.Source#path(java.lang.CharSequence)
45 */
46 public Path path(CharSequence p) {
47 if (p instanceof Path) {
48 return asPath((Path) p);
49 }
50 p = pathRewrite.rewrite(p);
51 return get(p, true);
52 }
53
54 /*
55 * (non-Javadoc)
56 * @see org.tmatesoft.hg.util.Convertor#mangle(java.lang.Object)
57 */
58 public Path mangle(Path p) {
59 return asPath(p);
60 }
61
62 // pipes path object through cache to reuse instance, if possible
63 // TODO unify with Pool<Path>
64 private Path asPath(Path p) {
65 CharSequence s = pathRewrite.rewrite(p.toString());
66 // rewrite string, not path to avoid use of Path object as key
67 // in case pathRewrite does nothing and returns supplied instance
68 //
69 Path cached = get(s, false);
70 if (cached == null) {
71 cache.put(s, new SoftReference<Path>(cached = p));
72 }
73 return cached;
74 }
75
76 // XXX what would be parent of an empty path?
77 // Path shall have similar functionality
78 public Path parent(Path path) {
79 if (path.length() == 0) {
80 throw new IllegalArgumentException();
81 }
82 for (int i = path.length() - 2 /*if path represents a dir, trailing char is slash, skip*/; i >= 0; i--) {
83 if (path.charAt(i) == '/') {
84 return get(path.subSequence(0, i+1).toString(), true);
85 }
86 }
87 return get("", true);
88 }
89
90 // invoke when path pool is no longer in use, to ease gc work
91 public void clear() {
92 cache.clear();
93 }
94
95 private Path get(CharSequence p, boolean create) {
96 SoftReference<Path> sr = cache.get(p);
97 Path path = sr == null ? null : sr.get();
98 if (path == null) {
99 if (create) {
100 path = Path.create(p);
101 cache.put(p, new SoftReference<Path>(path));
102 } else if (sr != null) {
103 // cached path no longer used, clear cache entry - do not wait for RefQueue to step in
104 cache.remove(p);
105 }
106 }
107 return path;
108 }
109 }