1 package com.atlassian.seraph.util;
2
3 import java.util.Collection;
4 import java.util.Map;
5 import java.util.concurrent.ConcurrentHashMap;
6 import java.util.concurrent.ConcurrentMap;
7
8
9
10
11 public class CachedPathMapper implements IPathMapper
12 {
13 private static final String NULL = new String("NULL");
14
15 private final PathMapper delegate = new PathMapper();
16 private final ConcurrentMap<String, String> cacheMap;
17 private final ConcurrentMap<String, Collection<String>> cacheAllMap;
18
19
20
21
22
23
24
25
26
27
28
29
30 public CachedPathMapper()
31 {
32 this(new ConcurrentHashMap<String, String>(), new ConcurrentHashMap<String, Collection<String>>());
33 }
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public CachedPathMapper(final ConcurrentMap<String, String> cacheMap, final ConcurrentMap<String, Collection<String>> cacheAllMap)
48 {
49 this.cacheMap = cacheMap;
50 this.cacheAllMap = cacheAllMap;
51 }
52
53 public String get(final String path)
54 {
55
56 String result = cacheMap.get(path);
57 while (result == null)
58 {
59 String value = delegate.get(path);
60 if (value == null)
61 {
62 value = NULL;
63 }
64 cacheMap.putIfAbsent(path, value);
65 result = cacheMap.get(path);
66 }
67 return (result == NULL) ? null : result;
68 }
69
70 public Collection<String> getAll(final String path)
71 {
72 Collection<String> result = cacheAllMap.get(path);
73
74 while (result == null)
75 {
76 cacheAllMap.putIfAbsent(path, delegate.getAll(path));
77
78 result = cacheAllMap.get(path);
79 }
80 return result;
81 }
82
83 public void set(final Map<String, String> patterns)
84 {
85 for (final Map.Entry<String, String> entry2 : patterns.entrySet())
86 {
87 final Map.Entry<String, String> entry = entry2;
88 put(entry.getKey(), entry.getValue());
89 }
90 }
91
92 public void put(final String key, final String pattern)
93 {
94
95 delegate.put(key, pattern);
96 cacheMap.remove(key);
97 cacheAllMap.remove(key);
98 }
99
100 @Override
101 public String toString()
102 {
103 return delegate.toString();
104 }
105 }