Skip to content

Commit

Permalink
[MNG-6829] refactor: Replace any StringUtils#isEmpty(String) and #isN…
Browse files Browse the repository at this point in the history
…otEmpty(String) (#169)

* [MNG-6829] refactor: Replace any StringUtils#isEmpty(String) and #isNotEmpty(String)

### [Replace any StringUtils#isEmpty(String) and #isNotEmpty(String)](https://public.moderne.io/recipes/org.openrewrite.java.migrate.apache.commons.lang.IsNotEmptyToJdk)

A continuation of https://issues.apache.org/jira/browse/MNG-6825 and https://issues.apache.org/jira/browse/MNG-6829, where previously a request was made to instead of switching implementation to a different StringUtils, we should instead switch to using JDK internals where we can. This is a first such pull request to gauge interest before I can potentially make 47 more PRs to replace a total of 210 uses of isEmpty / isNotEmpty.


Co-authored-by: Moderne <team@moderne.io>

* Apply Spotless

---------

Co-authored-by: Moderne <team@moderne.io>
  • Loading branch information
timtebeek and TeamModerne authored May 9, 2023
1 parent cbdee3b commit 5a7875e
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.util.FilenameUtils;
import org.apache.maven.scm.util.ThreadSafeDateFormat;
Expand Down Expand Up @@ -302,15 +301,15 @@ public void setDate(String date) {
*/
public void setDate(String date, String userDatePattern) {
try {
if (!StringUtils.isEmpty(userDatePattern)) {
if (!(userDatePattern == null || userDatePattern.isEmpty())) {
SimpleDateFormat format = new SimpleDateFormat(userDatePattern);

this.date = format.parse(date);
} else {
this.date = TIMESTAMP_FORMAT_3.parse(date);
}
} catch (ParseException e) {
if (!StringUtils.isEmpty(userDatePattern)) {
if (!(userDatePattern == null || userDatePattern.isEmpty())) {
try {
this.date = TIMESTAMP_FORMAT_3.parse(date);
} catch (ParseException pe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.scm.CommandParameter;
import org.apache.maven.scm.CommandParameters;
import org.apache.maven.scm.NoSuchCommandScmException;
Expand Down Expand Up @@ -193,7 +192,7 @@ public BranchScmResult branch(ScmRepository repository, ScmFileSet fileSet, Stri
throws ScmException {
ScmBranchParameters scmBranchParameters = new ScmBranchParameters();

if (StringUtils.isNotEmpty(message)) {
if (message != null && !message.isEmpty()) {
scmBranchParameters.setMessage(message);
}

Expand Down Expand Up @@ -251,7 +250,7 @@ public ChangeLogScmResult changeLog(
throws ScmException {
ScmBranch scmBranch = null;

if (StringUtils.isNotEmpty(branch)) {
if (branch != null && !branch.isEmpty()) {
scmBranch = new ScmBranch(branch);
}
return changeLog(repository, fileSet, startDate, endDate, numDays, scmBranch, null);
Expand Down Expand Up @@ -324,11 +323,11 @@ public ChangeLogScmResult changeLog(
ScmVersion startRevision = null;
ScmVersion endRevision = null;

if (StringUtils.isNotEmpty(startTag)) {
if (startTag != null && !startTag.isEmpty()) {
startRevision = new ScmRevision(startTag);
}

if (StringUtils.isNotEmpty(endTag)) {
if (endTag != null && !endTag.isEmpty()) {
endRevision = new ScmRevision(endTag);
}

Expand Down Expand Up @@ -385,7 +384,7 @@ public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, St
throws ScmException {
ScmVersion scmVersion = null;

if (StringUtils.isNotEmpty(tag)) {
if (tag != null && !tag.isEmpty()) {
scmVersion = new ScmBranch(tag);
}

Expand Down Expand Up @@ -444,7 +443,7 @@ public CheckOutScmResult checkOut(ScmRepository repository, ScmFileSet fileSet,
throws ScmException {
ScmVersion scmVersion = null;

if (StringUtils.isNotEmpty(tag)) {
if (tag != null && !tag.isEmpty()) {
scmVersion = new ScmBranch(tag);
}

Expand Down Expand Up @@ -524,11 +523,11 @@ public DiffScmResult diff(ScmRepository repository, ScmFileSet fileSet, String s
ScmVersion startVersion = null;
ScmVersion endVersion = null;

if (StringUtils.isNotEmpty(startRevision)) {
if (startRevision != null && !startRevision.isEmpty()) {
startVersion = new ScmRevision(startRevision);
}

if (StringUtils.isNotEmpty(endRevision)) {
if (endRevision != null && !endRevision.isEmpty()) {
endVersion = new ScmRevision(endRevision);
}

Expand Down Expand Up @@ -601,7 +600,7 @@ public ExportScmResult export(ScmRepository repository, ScmFileSet fileSet, Stri
throws ScmException {
ScmVersion scmVersion = null;

if (StringUtils.isNotEmpty(tag)) {
if (tag != null && !tag.isEmpty()) {
scmVersion = new ScmRevision(tag);
}

Expand Down Expand Up @@ -656,7 +655,7 @@ public ListScmResult list(ScmRepository repository, ScmFileSet fileSet, boolean
throws ScmException {
ScmVersion scmVersion = null;

if (StringUtils.isNotEmpty(tag)) {
if (tag != null && !tag.isEmpty()) {
scmVersion = new ScmRevision(tag);
}

Expand Down Expand Up @@ -804,7 +803,7 @@ public TagScmResult tag(ScmRepository repository, ScmFileSet fileSet, String tag

parameters.setString(CommandParameter.TAG_NAME, tagName);

if (StringUtils.isNotEmpty(message)) {
if (message != null && !message.isEmpty()) {
parameters.setString(CommandParameter.MESSAGE, message);
}

Expand Down Expand Up @@ -957,7 +956,7 @@ private UpdateScmResult update(
throws ScmException {
ScmBranch scmBranch = null;

if (StringUtils.isNotEmpty(tag)) {
if (tag != null && !tag.isEmpty()) {
scmBranch = new ScmBranch(tag);
}

Expand Down Expand Up @@ -1017,7 +1016,7 @@ public UpdateScmResult update(
throws ScmException {
ScmBranch scmBranch = null;

if (StringUtils.isNotEmpty(tag)) {
if (tag != null && !tag.isEmpty()) {
scmBranch = new ScmBranch(tag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.plexus.util.cli.StreamConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -63,7 +62,7 @@ protected Date parseDate(String date, String userPattern, String defaultPattern,
String patternUsed = null;
Locale localeUsed = null;

if (StringUtils.isNotEmpty(userPattern)) {
if (userPattern != null && !userPattern.isEmpty()) {
if (locale != null) {
format = new SimpleDateFormat(userPattern, locale);
localeUsed = locale;
Expand All @@ -73,7 +72,7 @@ protected Date parseDate(String date, String userPattern, String defaultPattern,
}
patternUsed = userPattern;
} else {
if (StringUtils.isNotEmpty(defaultPattern)) {
if (defaultPattern != null && !defaultPattern.isEmpty()) {
if (locale != null) {
format = new SimpleDateFormat(defaultPattern, locale);
localeUsed = locale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.scm.ScmBranch;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
Expand Down Expand Up @@ -292,15 +291,15 @@ private void showError(ScmResult result) {

String providerMessage = result.getProviderMessage();

if (!StringUtils.isEmpty(providerMessage)) {
if (!(providerMessage == null || providerMessage.isEmpty())) {
System.err.println("Error message from the provider: " + providerMessage);
} else {
System.err.println("The provider didn't give a error message.");
}

String output = result.getCommandOutput();

if (!StringUtils.isEmpty(output)) {
if (!(output == null || output.isEmpty())) {
System.err.println("Command output:");

System.err.println(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ protected void setConnectionType(String connectionType) {

public String getConnectionUrl() {
boolean requireDeveloperConnection = !"connection".equals(connectionType.toLowerCase());
if (StringUtils.isNotEmpty(connectionUrl) && !requireDeveloperConnection) {
if ((connectionUrl != null && !connectionUrl.isEmpty()) && !requireDeveloperConnection) {
return connectionUrl;
} else if (StringUtils.isNotEmpty(developerConnectionUrl)) {
} else if (developerConnectionUrl != null && !developerConnectionUrl.isEmpty()) {
return developerConnectionUrl;
}
if (requireDeveloperConnection) {
Expand Down Expand Up @@ -272,15 +272,15 @@ public ScmRepository getScmRepository() throws ScmException {

providerRepo.setPushChanges(pushChanges);

if (!StringUtils.isEmpty(workItem)) {
if (!(workItem == null || workItem.isEmpty())) {
providerRepo.setWorkItem(workItem);
}

if (!StringUtils.isEmpty(username)) {
if (!(username == null || username.isEmpty())) {
providerRepo.setUser(username);
}

if (!StringUtils.isEmpty(password)) {
if (!(password == null || password.isEmpty())) {
providerRepo.setPassword(password);
}

Expand All @@ -289,24 +289,25 @@ public ScmRepository getScmRepository() throws ScmException {

loadInfosFromSettings(repo);

if (!StringUtils.isEmpty(username)) {
if (!(username == null || username.isEmpty())) {
repo.setUser(username);
}

if (!StringUtils.isEmpty(password)) {
if (!(password == null || password.isEmpty())) {
repo.setPassword(password);
}

if (!StringUtils.isEmpty(privateKey)) {
if (!(privateKey == null || privateKey.isEmpty())) {
repo.setPrivateKey(privateKey);
}

if (!StringUtils.isEmpty(passphrase)) {
if (!(passphrase == null || passphrase.isEmpty())) {
repo.setPassphrase(passphrase);
}
}

if (!StringUtils.isEmpty(tagBase) && repository.getProvider().equals("svn")) {
if (!(tagBase == null || tagBase.isEmpty())
&& repository.getProvider().equals("svn")) {
SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();

svnRepo.setTagBase(tagBase);
Expand Down Expand Up @@ -405,11 +406,11 @@ public void setExcludes(String excludes) {
}

public ScmVersion getScmVersion(String versionType, String version) throws MojoExecutionException {
if (StringUtils.isEmpty(versionType) && StringUtils.isNotEmpty(version)) {
if ((versionType == null || versionType.isEmpty()) && (version != null && !version.isEmpty())) {
throw new MojoExecutionException("You must specify the version type.");
}

if (StringUtils.isEmpty(version)) {
if (version == null || version.isEmpty()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ private void runGoals(String relativePathProjectDirectory) throws MojoExecutionE
protected String determineWorkingDirectoryPath(
File checkoutDirectory, String relativePathProjectDirectory, String goalsDirectory) {
File projectDirectory;
if (StringUtils.isNotEmpty(relativePathProjectDirectory)) {
if (relativePathProjectDirectory != null && !relativePathProjectDirectory.isEmpty()) {
projectDirectory = new File(checkoutDirectory, relativePathProjectDirectory);
} else {
projectDirectory = checkoutDirectory;
}

if (StringUtils.isEmpty(goalsDirectory)) {
if (goalsDirectory == null || goalsDirectory.isEmpty()) {
return projectDirectory.getPath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -143,24 +142,28 @@ public void execute() throws MojoExecutionException {

request.setDatePattern(dateFormat);

if (StringUtils.isNotEmpty(startDate)) {
if (startDate != null && !startDate.isEmpty()) {
request.setStartDate(parseDate(localFormat, startDate));
}

if (StringUtils.isNotEmpty(endDate)) {
if (endDate != null && !endDate.isEmpty()) {
request.setEndDate(parseDate(localFormat, endDate));
}

if (StringUtils.isNotEmpty(startScmVersion)) {
if (startScmVersion != null && !startScmVersion.isEmpty()) {
ScmVersion startRev = getScmVersion(
StringUtils.isEmpty(startScmVersionType) ? VERSION_TYPE_REVISION : startScmVersionType,
(startScmVersionType == null || startScmVersionType.isEmpty())
? VERSION_TYPE_REVISION
: startScmVersionType,
startScmVersion);
request.setStartRevision(startRev);
}

if (StringUtils.isNotEmpty(endScmVersion)) {
if (endScmVersion != null && !endScmVersion.isEmpty()) {
ScmVersion endRev = getScmVersion(
StringUtils.isEmpty(endScmVersionType) ? VERSION_TYPE_REVISION : endScmVersionType,
(endScmVersionType == null || endScmVersionType.isEmpty())
? VERSION_TYPE_REVISION
: endScmVersionType,
endScmVersion);
request.setEndRevision(endRev);
}
Expand All @@ -171,13 +174,14 @@ public void execute() throws MojoExecutionException {
request.setNumDays(numDays);
}

if (StringUtils.isNotEmpty(scmVersion)) {
if (scmVersion != null && !scmVersion.isEmpty()) {
ScmVersion rev = getScmVersion(
StringUtils.isEmpty(scmVersionType) ? VERSION_TYPE_REVISION : scmVersionType, scmVersion);
(scmVersionType == null || scmVersionType.isEmpty()) ? VERSION_TYPE_REVISION : scmVersionType,
scmVersion);
request.setRevision(rev);
}

if (StringUtils.isNotEmpty(scmBranch)) {
if (scmBranch != null && !scmBranch.isEmpty()) {
request.setScmBranch(new ScmBranch(scmBranch));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
*
Expand Down Expand Up @@ -110,7 +108,7 @@ private List<String> getConfLines() {
new BufferedReader(new FileReader(new File(getConfigDirectory(), "config")))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("#") && StringUtils.isNotEmpty(line)) {
if (!line.startsWith("#") && (line != null && !line.isEmpty())) {
lines.add(line);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.ScmFileStatus;
import org.apache.maven.scm.util.AbstractConsumer;
Expand All @@ -44,7 +43,7 @@ public void consumeLine(String line) {
if (logger.isDebugEnabled()) {
logger.debug(line);
}
if (StringUtils.isEmpty(line)) {
if (line == null || line.isEmpty()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void consumeLine(String line) {
}

if (infoItems.isEmpty()) {
if (!StringUtils.isEmpty(line)) {
if (!(line == null || line.isEmpty())) {
InfoItem infoItem = new InfoItem();
infoItem.setRevision(StringUtils.trim(line));
infoItem.setURL(scmFileSet.getBasedir().toPath().toUri().toASCIIString());
Expand Down
Loading

0 comments on commit 5a7875e

Please sign in to comment.