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