1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package io.github.tonywasher.joceanus.themis.parser.maven;
19
20 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataMap;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
24 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
25 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
26 import io.github.tonywasher.joceanus.themis.parser.base.ThemisChar;
27 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32
33
34
35 public class ThemisMavenVersionCache
36 implements MetisDataMap<String, String>, MetisFieldItem {
37
38
39
40 private static final MetisFieldSet<ThemisMavenVersionCache> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisMavenVersionCache.class);
41
42
43
44
45 static {
46 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PARENT, ThemisMavenVersionCache::getParent);
47 }
48
49
50
51
52 private final Map<String, String> theMap;
53
54
55
56
57 private ThemisMavenVersionCache theParent;
58
59
60
61
62 ThemisMavenVersionCache() {
63 theMap = new HashMap<>();
64 }
65
66 @Override
67 public MetisFieldSet<ThemisMavenVersionCache> getDataFieldSet() {
68 return FIELD_DEFS;
69 }
70
71 @Override
72 public String formatObject(final OceanusDataFormatter pFormatter) {
73 return getClass().getSimpleName();
74 }
75
76 @Override
77 public Map<String, String> getUnderlyingMap() {
78 return theMap;
79 }
80
81
82
83
84
85
86 private ThemisMavenVersionCache getParent() {
87 return theParent;
88 }
89
90
91
92
93
94
95 void setParent(final ThemisMavenVersionCache pParent) {
96 theParent = pParent;
97 }
98
99
100
101
102
103
104
105 public void addToCache(final ThemisMavenId pId) throws OceanusException {
106
107 final String myPrefix = buildPrefix(pId);
108 final String myProposed = pId.getVersion();
109
110
111 if (myProposed == null) {
112 throw new ThemisDataException("Proposed version for " + myPrefix + " is null");
113 }
114
115
116 theMap.put(myPrefix, myProposed);
117 }
118
119
120
121
122
123
124
125
126 public ThemisMavenId lookUpVersion(final ThemisMavenId pId) throws OceanusException {
127
128 final String myPrefix = buildPrefix(pId);
129 final String myProposed = pId.getVersion();
130
131
132 final String myVersion = lookUpVersionInCache(myPrefix);
133
134
135 if (myVersion != null) {
136
137 if (myProposed == null) {
138 return new ThemisMavenId(pId, myVersion);
139
140
141 } else if (myProposed.equals(myVersion)) {
142 return pId;
143
144
145 } else {
146 throw new ThemisDataException("Conflicting explicit dependency version for " + myPrefix
147 + " Required=" + myVersion
148 + " Proposed=" + myProposed);
149 }
150
151
152 } else {
153
154 if (myProposed == null) {
155 throw new ThemisDataException("Proposed version for " + myPrefix + " is null");
156 }
157
158
159 theMap.put(myPrefix, pId.getVersion());
160 return pId;
161 }
162 }
163
164
165
166
167
168
169
170 void importDependencies(final ThemisMavenVersionCache pChild) throws OceanusException {
171
172 for (Map.Entry<String, String> myEntry : pChild.getUnderlyingMap().entrySet()) {
173
174 final String myPrefix = myEntry.getKey();
175 final String myProposed = myEntry.getValue();
176
177
178 final String myVersion = lookUpVersionInCache(myPrefix);
179
180
181 if (myVersion != null && !myVersion.equals(myProposed)) {
182 throw new ThemisDataException("Conflicting imported version for " + myPrefix
183 + " Required=" + myVersion
184 + " Proposed=" + myProposed);
185 }
186
187
188 theMap.put(myPrefix, myProposed);
189 }
190 }
191
192
193
194
195
196
197
198 private String lookUpVersionInCache(final String pPrefix) {
199
200 final String myVersion = theMap.get(pPrefix);
201 return (theParent == null || myVersion != null) ? myVersion : theParent.lookUpVersionInCache(pPrefix);
202 }
203
204
205
206
207
208
209
210 private String buildPrefix(final ThemisMavenId pId) {
211 return pId.getGroupId() + ThemisChar.COLON + pId.getArtifactId();
212 }
213 }