1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.internal;
20
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import org.apache.maven.building.Source;
26 import org.apache.maven.model.building.ModelCache;
27 import org.eclipse.aether.RepositorySystemSession;
28
29
30
31
32
33
34 public class DefaultModelCache implements ModelCache {
35
36 private static final String KEY = DefaultModelCache.class.getName();
37
38 private final Map<Object, Object> cache;
39
40 public static ModelCache newInstance(RepositorySystemSession session) {
41 Map<Object, Object> cache;
42 if (session.getCache() == null) {
43 cache = new ConcurrentHashMap<>();
44 } else {
45 cache = (Map) session.getCache().get(session, KEY);
46 if (cache == null) {
47 cache = new ConcurrentHashMap<>();
48 session.getCache().put(session, KEY, cache);
49 }
50 }
51 return new DefaultModelCache(cache);
52 }
53
54 private DefaultModelCache(Map<Object, Object> cache) {
55 this.cache = cache;
56 }
57
58 public Object get(Source path, String tag) {
59 return get(new SourceCacheKey(path, tag));
60 }
61
62 public void put(Source path, String tag, Object data) {
63 put(new SourceCacheKey(path, tag), data);
64 }
65
66 public Object get(String groupId, String artifactId, String version, String tag) {
67 return get(new GavCacheKey(groupId, artifactId, version, tag));
68 }
69
70 public void put(String groupId, String artifactId, String version, String tag, Object data) {
71 put(new GavCacheKey(groupId, artifactId, version, tag), data);
72 }
73
74 protected Object get(Object key) {
75 return cache.get(key);
76 }
77
78 protected void put(Object key, Object data) {
79 cache.put(key, data);
80 }
81
82 static class GavCacheKey {
83
84 private final String gav;
85
86 private final String tag;
87
88 private final int hash;
89
90 GavCacheKey(String groupId, String artifactId, String version, String tag) {
91 this(gav(groupId, artifactId, version), tag);
92 }
93
94 GavCacheKey(String gav, String tag) {
95 this.gav = gav;
96 this.tag = tag;
97 this.hash = Objects.hash(gav, tag);
98 }
99
100 private static String gav(String groupId, String artifactId, String version) {
101 StringBuilder sb = new StringBuilder();
102 if (groupId != null) {
103 sb.append(groupId);
104 }
105 sb.append(":");
106 if (artifactId != null) {
107 sb.append(artifactId);
108 }
109 sb.append(":");
110 if (version != null) {
111 sb.append(version);
112 }
113 return sb.toString();
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (null == obj || !getClass().equals(obj.getClass())) {
122 return false;
123 }
124 GavCacheKey that = (GavCacheKey) obj;
125 return Objects.equals(this.gav, that.gav) && Objects.equals(this.tag, that.tag);
126 }
127
128 @Override
129 public int hashCode() {
130 return hash;
131 }
132
133 @Override
134 public String toString() {
135 return "GavCacheKey{" + "gav='" + gav + '\'' + ", tag='" + tag + '\'' + '}';
136 }
137 }
138
139 private static final class SourceCacheKey {
140 private final Source source;
141
142 private final String tag;
143
144 private final int hash;
145
146 SourceCacheKey(Source source, String tag) {
147 this.source = source;
148 this.tag = tag;
149 this.hash = Objects.hash(source, tag);
150 }
151
152 @Override
153 public String toString() {
154 return "SourceCacheKey{" + "source=" + source + ", tag='" + tag + '\'' + '}';
155 }
156
157 @Override
158 public boolean equals(Object obj) {
159 if (this == obj) {
160 return true;
161 }
162 if (null == obj || !getClass().equals(obj.getClass())) {
163 return false;
164 }
165 SourceCacheKey that = (SourceCacheKey) obj;
166 return Objects.equals(this.source, that.source) && Objects.equals(this.tag, that.tag);
167 }
168
169 @Override
170 public int hashCode() {
171 return hash;
172 }
173 }
174 }