Skip to content

Commit 6608606

Browse files
committed
Refactored a bit to try to satisfy Codacy
1 parent 22215af commit 6608606

File tree

2 files changed

+134
-120
lines changed

2 files changed

+134
-120
lines changed

jme3-templates/src/com/jme3/gde/templates/gradledesktop/options/CachedOptionsContainer.java

Lines changed: 123 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -113,91 +113,95 @@ private void initialize() {
113113
private static List<TemplateLibrary> initLibaries(final MavenVersionChecker mavenVersionChecker, TemplateLibrary[] libraries) {
114114
List<TemplateLibrary> libs = new ArrayList<>(libraries.length);
115115
for (TemplateLibrary templateLibrary : libraries) {
116-
libs.add(new TemplateLibrary() {
117-
118-
private VersionInfo version;
119-
120-
{
121-
if (templateLibrary.getGroupId() != null && templateLibrary.getArtifactId() != null) {
122-
if (LIBRARY_VERSION_FILTERS.containsKey(templateLibrary)) {
123-
mavenVersionChecker.getAllVersions(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
124-
.whenComplete((result, exception) -> {
125-
if (exception != null || result == null) {
126-
logMavenCheckFailure(exception);
127-
128-
return;
129-
}
130-
131-
Predicate<VersionInfo> versionFilter = LIBRARY_VERSION_FILTERS.get(templateLibrary);
132-
Optional<VersionInfo> latestInfo = result.stream()
133-
.map((versionString) -> {
134-
return SemanticPlusTagVersionInfo.of(versionString);
135-
})
136-
.filter(versionFilter)
137-
.max(Comparator.naturalOrder());
138-
if (latestInfo.isPresent()) {
139-
version = latestInfo.get();
140-
}
141-
});
142-
} else {
143-
mavenVersionChecker.getLatestVersion(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
144-
.whenComplete((result, exception) -> {
145-
if (exception != null || result == null) {
146-
logMavenCheckFailure(exception);
147-
148-
return;
149-
}
150-
151-
version = SemanticPlusTagVersionInfo.of(result);
152-
});
153-
}
154-
}
155-
}
116+
libs.add(createTemplateLibrary(templateLibrary, mavenVersionChecker));
117+
}
156118

157-
private void logMavenCheckFailure(Throwable exception) {
158-
logger.log(Level.INFO, exception,
159-
() -> String.format("Failed to acquire version information for Maven artifact %s (%s:%s)", new Object[]{getLabel(), getGroupId(), getArtifactId()}));
160-
}
119+
return Collections.unmodifiableList(libs);
120+
}
161121

162-
@Override
163-
public String getLabel() {
164-
return templateLibrary.getLabel();
122+
private static TemplateLibrary createTemplateLibrary(TemplateLibrary templateLibrary, final MavenVersionChecker mavenVersionChecker) {
123+
return new TemplateLibrary() {
124+
125+
private VersionInfo version;
126+
127+
{
128+
if (templateLibrary.getGroupId() != null && templateLibrary.getArtifactId() != null) {
129+
if (LIBRARY_VERSION_FILTERS.containsKey(templateLibrary)) {
130+
mavenVersionChecker.getAllVersions(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
131+
.whenComplete((result, exception) -> {
132+
if (exception != null || result == null) {
133+
logMavenCheckFailure(exception);
134+
135+
return;
136+
}
137+
138+
Predicate<VersionInfo> versionFilter = LIBRARY_VERSION_FILTERS.get(templateLibrary);
139+
Optional<VersionInfo> latestInfo = result.stream()
140+
.map((versionString) -> {
141+
return SemanticPlusTagVersionInfo.of(versionString);
142+
})
143+
.filter(versionFilter)
144+
.max(Comparator.naturalOrder());
145+
if (latestInfo.isPresent()) {
146+
version = latestInfo.get();
147+
}
148+
});
149+
} else {
150+
mavenVersionChecker.getLatestVersion(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
151+
.whenComplete((result, exception) -> {
152+
if (exception != null || result == null) {
153+
logMavenCheckFailure(exception);
154+
155+
return;
156+
}
157+
158+
version = SemanticPlusTagVersionInfo.of(result);
159+
});
160+
}
165161
}
162+
}
166163

167-
@Override
168-
public String getDescription() {
169-
return templateLibrary.getDescription();
170-
}
164+
private void logMavenCheckFailure(Throwable exception) {
165+
logger.log(Level.INFO, exception,
166+
() -> String.format("Failed to acquire version information for Maven artifact %s (%s:%s)", new Object[]{getLabel(), getGroupId(), getArtifactId()}));
167+
}
171168

172-
@Override
173-
public boolean getIsCoreJmeLibrary() {
174-
return templateLibrary.getIsCoreJmeLibrary();
175-
}
169+
@Override
170+
public String getLabel() {
171+
return templateLibrary.getLabel();
172+
}
176173

177-
@Override
178-
public String getGroupId() {
179-
return templateLibrary.getGroupId();
180-
}
174+
@Override
175+
public String getDescription() {
176+
return templateLibrary.getDescription();
177+
}
181178

182-
@Override
183-
public String getArtifactId() {
184-
return templateLibrary.getArtifactId();
185-
}
179+
@Override
180+
public boolean getIsCoreJmeLibrary() {
181+
return templateLibrary.getIsCoreJmeLibrary();
182+
}
186183

187-
@Override
188-
public String toString() {
189-
return templateLibrary.getLabel();
190-
}
184+
@Override
185+
public String getGroupId() {
186+
return templateLibrary.getGroupId();
187+
}
191188

192-
@Override
193-
public VersionInfo getVersionInfo() {
194-
return version != null ? version : templateLibrary.getVersionInfo();
195-
}
189+
@Override
190+
public String getArtifactId() {
191+
return templateLibrary.getArtifactId();
192+
}
196193

197-
});
198-
}
194+
@Override
195+
public String toString() {
196+
return templateLibrary.getLabel();
197+
}
199198

200-
return Collections.unmodifiableList(libs);
199+
@Override
200+
public VersionInfo getVersionInfo() {
201+
return version != null ? version : templateLibrary.getVersionInfo();
202+
}
203+
204+
};
201205
}
202206

203207
public List<TemplateLibrary> getAdditionalLibraries() {
@@ -273,58 +277,60 @@ private static void initVersionList(List<String> result, Predicate<VersionInfo>
273277
final SortedSet<LibraryVersion> allVersions = new TreeSet<>(Comparator.comparing(LibraryVersion::getVersionInfo, Comparator.reverseOrder()));
274278
allVersions.addAll(Arrays.asList(versions));
275279
for (VersionInfo versionInfo : versionInfoList) {
276-
allVersions.add(new LibraryVersion() {
280+
allVersions.add(createLibraryVersion(groupId, artifactId, defaultPatchNotes, versionInfo));
281+
}
277282

278-
@Override
279-
public String getGroupId() {
280-
return groupId;
281-
}
283+
completedVersionsConsumer.accept(Collections.unmodifiableList(new ArrayList<>(allVersions)));
284+
}
282285

283-
@Override
284-
public String getArtifactId() {
285-
return artifactId;
286-
}
286+
private static LibraryVersion createLibraryVersion(String groupId, String artifactId, String defaultPatchNotes, VersionInfo versionInfo) {
287+
return new LibraryVersion() {
287288

288-
@Override
289-
public String getPatchNotesPath() {
290-
return defaultPatchNotes;
291-
}
289+
@Override
290+
public String getGroupId() {
291+
return groupId;
292+
}
292293

293-
@Override
294-
public String toString() {
295-
return getVersionInfo().getVersionString();
296-
}
294+
@Override
295+
public String getArtifactId() {
296+
return artifactId;
297+
}
297298

298-
@Override
299-
public VersionInfo getVersionInfo() {
300-
return versionInfo;
301-
}
299+
@Override
300+
public String getPatchNotesPath() {
301+
return defaultPatchNotes;
302+
}
302303

303-
@Override
304-
public int hashCode() {
305-
return Objects.hashCode(versionInfo.getVersionString());
306-
}
304+
@Override
305+
public String toString() {
306+
return getVersionInfo().getVersionString();
307+
}
307308

308-
@Override
309-
public boolean equals(Object obj) {
310-
if (this == obj) {
311-
return true;
312-
}
313-
if (obj == null) {
314-
return false;
315-
}
316-
if (!(obj instanceof LibraryVersion)) {
317-
return false;
318-
}
319-
final LibraryVersion other = (LibraryVersion) obj;
309+
@Override
310+
public VersionInfo getVersionInfo() {
311+
return versionInfo;
312+
}
320313

321-
return Objects.equals(getVersionInfo().getVersionString(), other.getVersionInfo().getVersionString());
322-
}
314+
@Override
315+
public int hashCode() {
316+
return Objects.hashCode(versionInfo.getVersionString());
317+
}
323318

324-
});
325-
}
319+
@Override
320+
public boolean equals(Object obj) {
321+
if (this == obj) {
322+
return true;
323+
}
324+
if (obj == null) {
325+
return false;
326+
}
327+
if (!(obj instanceof LibraryVersion)) {
328+
return false;
329+
}
330+
final LibraryVersion other = (LibraryVersion) obj;
326331

327-
completedVersionsConsumer.accept(Collections.unmodifiableList(new ArrayList<>(allVersions)));
332+
return Objects.equals(getVersionInfo().getVersionString(), other.getVersionInfo().getVersionString());
333+
}
334+
};
328335
}
329-
330336
}

jme3-templates/src/com/jme3/gde/templates/gradledesktop/options/SemanticPlusTagVersionInfo.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ public String getVersionString() {
108108

109109
@Override
110110
public int compareTo(VersionInfo o) {
111-
int result = Integer.compare((getMajor() != null ? getMajor() : 0), (o.getMajor() != null ? o.getMajor() : 0));
111+
int result = compareVersionDigit(getMajor(), o.getMajor());
112112
if (result != 0) {
113113
return result;
114114
}
115-
result = Integer.compare((getMinor() != null ? getMinor() : 0), (o.getMinor() != null ? o.getMinor() : 0));
115+
result = compareVersionDigit(getMinor(), o.getMinor());
116116
if (result != 0) {
117117
return result;
118118
}
119-
result = Integer.compare((getRelease() != null ? getRelease() : 0), (o.getRelease() != null ? o.getRelease() : 0));
119+
result = compareVersionDigit(getRelease(), o.getRelease());
120120
if (result != 0) {
121121
return result;
122122
}
@@ -129,6 +129,14 @@ public int compareTo(VersionInfo o) {
129129
return Collator.getInstance().compare(getVersionString(), o.getVersionString());
130130
}
131131

132+
private int compareVersionDigit(Integer versionDigit1, Integer versionDigit2) {
133+
if (versionDigit1 == null || versionDigit2 == null) {
134+
return 0;
135+
}
136+
137+
return Integer.compare(versionDigit1, versionDigit2);
138+
}
139+
132140
@Override
133141
public String toString() {
134142
return versionString;

0 commit comments

Comments
 (0)