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