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