1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.building;
20
21 import java.nio.file.Path;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.function.Supplier;
26
27 import org.apache.maven.model.Model;
28
29
30
31
32
33
34 class DefaultTransformerContext implements TransformerContext {
35 final Map<String, String> userProperties = new ConcurrentHashMap<>();
36
37 final Map<Path, Holder> modelByPath = new ConcurrentHashMap<>();
38
39 final Map<GAKey, Holder> modelByGA = new ConcurrentHashMap<>();
40
41 public static class Holder {
42 private volatile boolean set;
43 private volatile Model model;
44
45 Holder() {}
46
47 public static Model deref(Holder holder) {
48 return holder != null ? holder.get() : null;
49 }
50
51 public Model get() {
52 if (!set) {
53 synchronized (this) {
54 if (!set) {
55 try {
56 this.wait();
57 } catch (InterruptedException e) {
58
59 }
60 }
61 }
62 }
63 return model;
64 }
65
66 public Model computeIfAbsent(Supplier<Model> supplier) {
67 if (!set) {
68 synchronized (this) {
69 if (!set) {
70 this.set = true;
71 this.model = supplier.get();
72 this.notifyAll();
73 }
74 }
75 }
76 return model;
77 }
78 }
79
80 @Override
81 public String getUserProperty(String key) {
82 return userProperties.get(key);
83 }
84
85 @Override
86 public Model getRawModel(Path p) {
87 return Holder.deref(modelByPath.get(p));
88 }
89
90 @Override
91 public Model getRawModel(String groupId, String artifactId) {
92 return Holder.deref(modelByGA.get(new GAKey(groupId, artifactId)));
93 }
94
95 static class GAKey {
96 private final String groupId;
97 private final String artifactId;
98 private final int hashCode;
99
100 GAKey(String groupId, String artifactId) {
101 this.groupId = groupId;
102 this.artifactId = artifactId;
103 this.hashCode = Objects.hash(groupId, artifactId);
104 }
105
106 @Override
107 public int hashCode() {
108 return hashCode;
109 }
110
111 @Override
112 public boolean equals(Object obj) {
113 if (this == obj) {
114 return true;
115 }
116 if (!(obj instanceof GAKey)) {
117 return false;
118 }
119
120 GAKey other = (GAKey) obj;
121 return Objects.equals(artifactId, other.artifactId) && Objects.equals(groupId, other.groupId);
122 }
123 }
124 }