From d09bc7437fe5f77e54e6944c1607871bb087b488 Mon Sep 17 00:00:00 2001 From: Dezhi Cai Date: Tue, 10 Sep 2019 14:22:22 +0800 Subject: [PATCH] [refactor] use utility method of Objects to simplify code (#284) * [refactor] use utility method of Objects to simplify code * [refactor] use utility method of Objects to simplify code --- .../org/apache/maven/artifact/versioning/VersionRange.java | 7 +++---- .../maven/artifact/resolver/DebugResolutionListener.java | 3 ++- .../maven/artifact/repository/MavenArtifactRepository.java | 3 ++- .../artifact/resolver/filter/ScopeArtifactFilter.java | 4 +++- .../src/main/java/org/apache/maven/plugin/CacheUtils.java | 2 +- .../apache/maven/plugin/DefaultPluginDescriptorCache.java | 3 ++- .../main/java/org/apache/maven/project/MavenProject.java | 3 ++- .../maven/project/artifact/DefaultMavenMetadataCache.java | 3 ++- .../java/org/apache/maven/toolchain/DefaultToolchain.java | 5 +++-- 9 files changed, 20 insertions(+), 13 deletions(-) diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java index c37eabb22e..af37d181f2 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.WeakHashMap; +import java.util.Objects; import org.apache.maven.artifact.Artifact; @@ -593,11 +594,9 @@ public class VersionRange VersionRange other = (VersionRange) obj; boolean equals = - recommendedVersion == other.recommendedVersion - || ( ( recommendedVersion != null ) && recommendedVersion.equals( other.recommendedVersion ) ); + Objects.equals(recommendedVersion, other.recommendedVersion); equals &= - restrictions == other.restrictions - || ( ( restrictions != null ) && restrictions.equals( other.restrictions ) ); + Objects.equals(restrictions, other.restrictions); return equals; } diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DebugResolutionListener.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DebugResolutionListener.java index 01aa80fcaf..e011760216 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DebugResolutionListener.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DebugResolutionListener.java @@ -20,6 +20,7 @@ package org.apache.maven.artifact.resolver; */ import java.util.HashSet; +import java.util.Objects; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -69,7 +70,7 @@ public class DebugResolutionListener String omittedVersion = omitted.getVersion(); String keptVersion = kept.getVersion(); - if ( omittedVersion != null ? !omittedVersion.equals( keptVersion ) : keptVersion != null ) + if (!Objects.equals(omittedVersion, keptVersion)) { logger.debug( indent + omitted + " (removed - nearer found: " + keptVersion + ")" ); } diff --git a/maven-core/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java b/maven-core/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java index 76bb604823..4c081a17d2 100644 --- a/maven-core/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java +++ b/maven-core/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java @@ -22,6 +22,7 @@ package org.apache.maven.artifact.repository; import java.io.File; import java.util.Collections; import java.util.List; +import java.util.Objects; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.metadata.ArtifactMetadata; @@ -353,7 +354,7 @@ public class MavenArtifactRepository protected static boolean eq( T s1, T s2 ) { - return s1 != null ? s1.equals( s2 ) : s2 == null; + return Objects.equals(s1, s2); } public Authentication getAuthentication() diff --git a/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ScopeArtifactFilter.java b/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ScopeArtifactFilter.java index 1d131b71a4..651acb4b7e 100644 --- a/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ScopeArtifactFilter.java +++ b/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ScopeArtifactFilter.java @@ -19,6 +19,8 @@ package org.apache.maven.artifact.resolver.filter; * under the License. */ +import java.util.Objects; + /** * Filter to only retain objects in the given artifactScope or better. * @@ -72,7 +74,7 @@ public class ScopeArtifactFilter private static boolean equals( T str1, T str2 ) { - return str1 != null ? str1.equals( str2 ) : str2 == null; + return Objects.equals(str1, str2); } } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/CacheUtils.java b/maven-core/src/main/java/org/apache/maven/plugin/CacheUtils.java index ccb9a65b38..ec874c6cca 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/CacheUtils.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/CacheUtils.java @@ -39,7 +39,7 @@ class CacheUtils @Deprecated public static boolean eq( T s1, T s2 ) { - return s1 != null ? s1.equals( s2 ) : s2 == null; + return Objects.equals(s1, s2); } /** diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java index 1e87998d71..ba62888c95 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginDescriptorCache.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.ArtifactUtils; @@ -212,7 +213,7 @@ public class DefaultPluginDescriptorCache private static boolean eq( T s1, T s2 ) { - return s1 != null ? s1.equals( s2 ) : s2 == null; + return Objects.equals(s1, s2); } } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 75c334808f..17bf836f9a 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.Objects; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; @@ -1061,7 +1062,7 @@ public class MavenProject private static boolean eq( T s1, T s2 ) { - return ( s1 != null ) ? s1.equals( s2 ) : s2 == null; + return Objects.equals(s1, s2); } @Override diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java index 056c052b1b..0b9c577558 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java @@ -27,6 +27,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import org.apache.maven.artifact.Artifact; @@ -201,7 +202,7 @@ public class DefaultMavenMetadataCache private static boolean eq( T s1, T s2 ) { - return s1 != null ? s1.equals( s2 ) : s2 == null; + return Objects.equals(s1, s2); } /** diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java index 59f5e13fbb..a32f87e94f 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; +import java.util.Objects; import org.apache.maven.toolchain.model.ToolchainModel; import org.codehaus.plexus.logging.Logger; @@ -137,7 +138,7 @@ public abstract class DefaultToolchain // should have been AbstractToolchain... DefaultToolchain other = (DefaultToolchain) obj; - if ( type == null ? other.type != null : !type.equals( other.type ) ) + if (!Objects.equals(type, other.type)) { return false; } @@ -145,7 +146,7 @@ public abstract class DefaultToolchain // should have been AbstractToolchain... Properties thisProvides = this.getModel().getProvides(); Properties otherProvides = other.getModel().getProvides(); - if ( thisProvides == null ? otherProvides != null : !thisProvides.equals( otherProvides ) ) + if (!Objects.equals(thisProvides, otherProvides)) { return false; }