فهرست منبع

Refactored a bit to try to satisfy Codacy

Toni Helenius 2 سال پیش
والد
کامیت
66086063b5

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

@@ -113,91 +113,95 @@ public class CachedOptionsContainer {
     private static List<TemplateLibrary> initLibaries(final MavenVersionChecker mavenVersionChecker, TemplateLibrary[] libraries) {
         List<TemplateLibrary> libs = new ArrayList<>(libraries.length);
         for (TemplateLibrary templateLibrary : libraries) {
-            libs.add(new TemplateLibrary() {
-
-                private VersionInfo version;
-
-                {
-                    if (templateLibrary.getGroupId() != null && templateLibrary.getArtifactId() != null) {
-                        if (LIBRARY_VERSION_FILTERS.containsKey(templateLibrary)) {
-                            mavenVersionChecker.getAllVersions(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
-                                    .whenComplete((result, exception) -> {
-                                        if (exception != null || result == null) {
-                                            logMavenCheckFailure(exception);
-
-                                    return;
-                                }
-
-                                Predicate<VersionInfo> versionFilter = LIBRARY_VERSION_FILTERS.get(templateLibrary);
-                                Optional<VersionInfo> latestInfo = result.stream()
-                                        .map((versionString) -> {
-                                    return SemanticPlusTagVersionInfo.of(versionString);
-                                        })
-                                        .filter(versionFilter)
-                                        .max(Comparator.naturalOrder());
-                                if (latestInfo.isPresent()) {
-                                    version = latestInfo.get();
-                                        }
-                                    });
-                        } else {
-                            mavenVersionChecker.getLatestVersion(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
-                                    .whenComplete((result, exception) -> {
-                                        if (exception != null || result == null) {
-                                            logMavenCheckFailure(exception);
-
-                                            return;
-                                }
-
-                                version = SemanticPlusTagVersionInfo.of(result);
-                                    });
-                        }
-                    }
-                }
+            libs.add(createTemplateLibrary(templateLibrary, mavenVersionChecker));
+        }
 
-                private void logMavenCheckFailure(Throwable exception) {
-                    logger.log(Level.INFO, exception,
-                            () -> String.format("Failed to acquire version information for Maven artifact %s (%s:%s)", new Object[]{getLabel(), getGroupId(), getArtifactId()}));
-                }
+        return Collections.unmodifiableList(libs);
+    }
 
-                @Override
-                public String getLabel() {
-                    return templateLibrary.getLabel();
+    private static TemplateLibrary createTemplateLibrary(TemplateLibrary templateLibrary, final MavenVersionChecker mavenVersionChecker) {
+        return new TemplateLibrary() {
+
+            private VersionInfo version;
+
+            {
+                if (templateLibrary.getGroupId() != null && templateLibrary.getArtifactId() != null) {
+                    if (LIBRARY_VERSION_FILTERS.containsKey(templateLibrary)) {
+                        mavenVersionChecker.getAllVersions(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
+                                .whenComplete((result, exception) -> {
+                                    if (exception != null || result == null) {
+                                        logMavenCheckFailure(exception);
+
+                                        return;
+                                    }
+
+                                    Predicate<VersionInfo> versionFilter = LIBRARY_VERSION_FILTERS.get(templateLibrary);
+                                    Optional<VersionInfo> latestInfo = result.stream()
+                                            .map((versionString) -> {
+                                                return SemanticPlusTagVersionInfo.of(versionString);
+                                            })
+                                            .filter(versionFilter)
+                                            .max(Comparator.naturalOrder());
+                                    if (latestInfo.isPresent()) {
+                                        version = latestInfo.get();
+                                    }
+                                });
+                    } else {
+                        mavenVersionChecker.getLatestVersion(templateLibrary.getGroupId(), templateLibrary.getArtifactId())
+                                .whenComplete((result, exception) -> {
+                                    if (exception != null || result == null) {
+                                        logMavenCheckFailure(exception);
+
+                                        return;
+                                    }
+
+                                    version = SemanticPlusTagVersionInfo.of(result);
+                                });
+                    }
                 }
+            }
 
-                @Override
-                public String getDescription() {
-                    return templateLibrary.getDescription();
-                }
+            private void logMavenCheckFailure(Throwable exception) {
+                logger.log(Level.INFO, exception,
+                        () -> String.format("Failed to acquire version information for Maven artifact %s (%s:%s)", new Object[]{getLabel(), getGroupId(), getArtifactId()}));
+            }
 
-                @Override
-                public boolean getIsCoreJmeLibrary() {
-                    return templateLibrary.getIsCoreJmeLibrary();
-                }
+            @Override
+            public String getLabel() {
+                return templateLibrary.getLabel();
+            }
 
-                @Override
-                public String getGroupId() {
-                    return templateLibrary.getGroupId();
-                }
+            @Override
+            public String getDescription() {
+                return templateLibrary.getDescription();
+            }
 
-                @Override
-                public String getArtifactId() {
-                    return templateLibrary.getArtifactId();
-                }
+            @Override
+            public boolean getIsCoreJmeLibrary() {
+                return templateLibrary.getIsCoreJmeLibrary();
+            }
 
-                @Override
-                public String toString() {
-                    return templateLibrary.getLabel();
-                }
+            @Override
+            public String getGroupId() {
+                return templateLibrary.getGroupId();
+            }
 
-                @Override
-                public VersionInfo getVersionInfo() {
-                    return version != null ? version : templateLibrary.getVersionInfo();
-                }
+            @Override
+            public String getArtifactId() {
+                return templateLibrary.getArtifactId();
+            }
 
-            });
-        }
+            @Override
+            public String toString() {
+                return templateLibrary.getLabel();
+            }
 
-        return Collections.unmodifiableList(libs);
+            @Override
+            public VersionInfo getVersionInfo() {
+                return version != null ? version : templateLibrary.getVersionInfo();
+            }
+
+        };
     }
 
     public List<TemplateLibrary> getAdditionalLibraries() {
@@ -273,58 +277,60 @@ public class CachedOptionsContainer {
         final SortedSet<LibraryVersion> allVersions = new TreeSet<>(Comparator.comparing(LibraryVersion::getVersionInfo, Comparator.reverseOrder()));
         allVersions.addAll(Arrays.asList(versions));
         for (VersionInfo versionInfo : versionInfoList) {
-            allVersions.add(new LibraryVersion() {
+            allVersions.add(createLibraryVersion(groupId, artifactId, defaultPatchNotes, versionInfo));
+        }
 
-                @Override
-                public String getGroupId() {
-                    return groupId;
-                }
+        completedVersionsConsumer.accept(Collections.unmodifiableList(new ArrayList<>(allVersions)));
+    }
 
-                @Override
-                public String getArtifactId() {
-                    return artifactId;
-                }
+    private static LibraryVersion createLibraryVersion(String groupId, String artifactId, String defaultPatchNotes, VersionInfo versionInfo) {
+        return new LibraryVersion() {
 
-                @Override
-                public String getPatchNotesPath() {
-                    return defaultPatchNotes;
-                }
+            @Override
+            public String getGroupId() {
+                return groupId;
+            }
 
-                @Override
-                public String toString() {
-                    return getVersionInfo().getVersionString();
-                }
+            @Override
+            public String getArtifactId() {
+                return artifactId;
+            }
 
-                @Override
-                public VersionInfo getVersionInfo() {
-                    return versionInfo;
-                }
+            @Override
+            public String getPatchNotesPath() {
+                return defaultPatchNotes;
+            }
 
-                @Override
-                public int hashCode() {
-                    return Objects.hashCode(versionInfo.getVersionString());
-                }
+            @Override
+            public String toString() {
+                return getVersionInfo().getVersionString();
+            }
 
-                @Override
-                public boolean equals(Object obj) {
-                    if (this == obj) {
-                        return true;
-                    }
-                    if (obj == null) {
-                        return false;
-                    }
-                    if (!(obj instanceof LibraryVersion)) {
-                        return false;
-                    }
-                    final LibraryVersion other = (LibraryVersion) obj;
+            @Override
+            public VersionInfo getVersionInfo() {
+                return versionInfo;
+            }
 
-                    return Objects.equals(getVersionInfo().getVersionString(), other.getVersionInfo().getVersionString());
-                }
+            @Override
+            public int hashCode() {
+                return Objects.hashCode(versionInfo.getVersionString());
+            }
 
-            });
-        }
+            @Override
+            public boolean equals(Object obj) {
+                if (this == obj) {
+                    return true;
+                }
+                if (obj == null) {
+                    return false;
+                }
+                if (!(obj instanceof LibraryVersion)) {
+                    return false;
+                }
+                final LibraryVersion other = (LibraryVersion) obj;
 
-        completedVersionsConsumer.accept(Collections.unmodifiableList(new ArrayList<>(allVersions)));
+                return Objects.equals(getVersionInfo().getVersionString(), other.getVersionInfo().getVersionString());
+            }
+        };
     }
-
 }

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

@@ -108,15 +108,15 @@ public final class SemanticPlusTagVersionInfo implements VersionInfo {
 
     @Override
     public int compareTo(VersionInfo o) {
-        int result = Integer.compare((getMajor() != null ? getMajor() : 0), (o.getMajor() != null ? o.getMajor() : 0));
+        int result = compareVersionDigit(getMajor(), o.getMajor());
         if (result != 0) {
             return result;
         }
-        result = Integer.compare((getMinor() != null ? getMinor() : 0), (o.getMinor() != null ? o.getMinor() : 0));
+        result = compareVersionDigit(getMinor(), o.getMinor());
         if (result != 0) {
             return result;
         }
-        result = Integer.compare((getRelease() != null ? getRelease() : 0), (o.getRelease() != null ? o.getRelease() : 0));
+        result = compareVersionDigit(getRelease(), o.getRelease());
         if (result != 0) {
             return result;
         }
@@ -129,6 +129,14 @@ public final class SemanticPlusTagVersionInfo implements VersionInfo {
         return Collator.getInstance().compare(getVersionString(), o.getVersionString());
     }
 
+    private int compareVersionDigit(Integer versionDigit1, Integer versionDigit2) {
+        if (versionDigit1 == null || versionDigit2 == null) {
+            return 0;
+        }
+
+        return Integer.compare(versionDigit1, versionDigit2);
+    }
+
     @Override
     public String toString() {
         return versionString;