Skip to content

Commit 8adef86

Browse files
authored
[openrewrite] StringRulesRecipes (#2656)
2 parents e4e51a8 + 443e6ef commit 8adef86

File tree

28 files changed

+109
-95
lines changed

28 files changed

+109
-95
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.Optional;
27+
import java.util.Objects;
2828
import java.util.Properties;
2929

3030
import javax.annotation.Nullable;
@@ -127,7 +127,7 @@ public FormatterStep build() {
127127
var roundtrippableState = new EquoStep(formatterVersion, settingProperties, settingXml, FileSignature.promise(settingsFiles), JarState.promise(() -> {
128128
P2QueryResult query;
129129
try {
130-
if (null != cacheDirectory) {
130+
if (cacheDirectory != null) {
131131
CacheLocations.override_p2data = cacheDirectory.toPath().resolve("dev/equo/p2-data").toFile();
132132
}
133133
query = createModelWithMirrors().query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW);
@@ -190,8 +190,8 @@ static class EquoStep implements Serializable {
190190
ImmutableMap<String, String> stepProperties) {
191191

192192
this.semanticVersion = semanticVersion;
193-
this.settingProperties = Optional.ofNullable(settingProperties).orElse(new ArrayList<>());
194-
this.settingXml = Optional.ofNullable(settingXml).orElse(new ArrayList<>());
193+
this.settingProperties = Objects.requireNonNullElse(settingProperties, new ArrayList<>());
194+
this.settingXml = Objects.requireNonNullElse(settingXml, new ArrayList<>());
195195
this.settingsPromise = settingsPromise;
196196
this.jarPromise = jarPromise;
197197
this.stepProperties = stepProperties;
@@ -218,8 +218,8 @@ public static class State implements Serializable {
218218
public State(String semanticVersion, JarState jarState, List<String> settingProperties, List<String> settingXml, FileSignature settingsFiles, ImmutableMap<String, String> stepProperties) {
219219
this.semanticVersion = semanticVersion;
220220
this.jarState = jarState;
221-
this.settingProperties = Optional.ofNullable(settingProperties).orElse(new ArrayList<>());
222-
this.settingXml = Optional.ofNullable(settingXml).orElse(new ArrayList<>());
221+
this.settingProperties = Objects.requireNonNullElse(settingProperties, new ArrayList<>());
222+
this.settingXml = Objects.requireNonNullElse(settingXml, new ArrayList<>());
223223
this.settingsFiles = settingsFiles;
224224
this.stepProperties = stepProperties;
225225
}

lib-extra/src/main/java/com/diffplug/spotless/extra/cpp/EclipseCdtFormatterStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private static FormatterFunc apply(EquoBasedStepBuilder.State state) throws Exce
7070
} catch (InvocationTargetException exceptionWrapper) {
7171
Throwable throwable = exceptionWrapper.getTargetException();
7272
Exception exception = throwable instanceof Exception e ? e : null;
73-
throw null == exception ? exceptionWrapper : exception;
73+
throw exception == null ? exceptionWrapper : exception;
7474
}
7575
});
7676
}

lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static FormatterFunc apply(EquoBasedStepBuilder.State state) throws Exce
9696
} catch (InvocationTargetException exceptionWrapper) {
9797
Throwable throwable = exceptionWrapper.getTargetException();
9898
Exception exception = throwable instanceof Exception e ? e : null;
99-
throw null == exception ? exceptionWrapper : exception;
99+
throw exception == null ? exceptionWrapper : exception;
100100
}
101101
});
102102
}

lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static FormatterFunc applyWithoutFile(String className, EclipseBasedStep
6868
} catch (InvocationTargetException exceptionWrapper) {
6969
Throwable throwable = exceptionWrapper.getTargetException();
7070
Exception exception = throwable instanceof Exception e ? e : null;
71-
throw null == exception ? exceptionWrapper : exception;
71+
throw exception == null ? exceptionWrapper : exception;
7272
}
7373
};
7474
}
@@ -86,7 +86,7 @@ public String applyWithFile(String unix, File file) throws Exception {
8686
} catch (InvocationTargetException exceptionWrapper) {
8787
Throwable throwable = exceptionWrapper.getTargetException();
8888
Exception exception = throwable instanceof Exception e ? e : null;
89-
throw null == exception ? exceptionWrapper : exception;
89+
throw exception == null ? exceptionWrapper : exception;
9090
}
9191
}
9292
});

lib/src/main/java/com/diffplug/spotless/FilterByContentPatternFormatterStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public boolean equals(Object o) {
6565
}
6666
FilterByContentPatternFormatterStep that = (FilterByContentPatternFormatterStep) o;
6767
return Objects.equals(delegateStep, that.delegateStep)
68-
&& Objects.equals(onMatch, that.onMatch)
68+
&& onMatch == that.onMatch
6969
&& Objects.equals(contentPattern.pattern(), that.contentPattern.pattern());
7070
}
7171

lib/src/main/java/com/diffplug/spotless/JarState.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.NoSuchElementException;
3030
import java.util.Objects;
3131
import java.util.Set;
32-
import java.util.stream.Collectors;
3332

3433
import javax.annotation.Nullable;
3534

@@ -132,7 +131,7 @@ private static JarState provisionWithTransitives(boolean withTransitives, Collec
132131
Objects.requireNonNull(provisioner, "provisioner");
133132
Set<File> jars = provisioner.provisionWithTransitives(withTransitives, mavenCoordinates);
134133
if (jars.isEmpty()) {
135-
throw new NoSuchElementException("Resolved to an empty result: " + mavenCoordinates.stream().collect(Collectors.joining(", ")));
134+
throw new NoSuchElementException("Resolved to an empty result: " + String.join(", ", mavenCoordinates));
136135
}
137136
FileSignature fileSignature = FileSignature.signAsSet(jars);
138137
return new JarState(fileSignature);

lib/src/main/java/com/diffplug/spotless/Jvm.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ private Support(String formatterName, Comparator<? super V> formatterVersionComp
8989
*/
9090
public Support<V> add(int minimumJvmVersion, V maxFormatterVersion) {
9191
Objects.requireNonNull(maxFormatterVersion);
92-
if (null != jvm2fmtMaxVersion.put(minimumJvmVersion, maxFormatterVersion)) {
92+
if (jvm2fmtMaxVersion.put(minimumJvmVersion, maxFormatterVersion) != null) {
9393
throw new IllegalArgumentException("Added duplicate entry for JVM %d+.".formatted(minimumJvmVersion));
9494
}
95-
if (null != fmtMaxVersion2jvmVersion.put(maxFormatterVersion, minimumJvmVersion)) {
95+
if (fmtMaxVersion2jvmVersion.put(maxFormatterVersion, minimumJvmVersion) != null) {
9696
throw new IllegalArgumentException("Added duplicate entry for formatter version %s.".formatted(maxFormatterVersion));
9797
}
9898
verifyVersionRangesDoNotIntersect(jvm2fmtMaxVersion, minimumJvmVersion, maxFormatterVersion);
@@ -101,7 +101,7 @@ public Support<V> add(int minimumJvmVersion, V maxFormatterVersion) {
101101

102102
public Support<V> addMin(int minimumJvmVersion, V minFormatterVersion) {
103103
Objects.requireNonNull(minFormatterVersion);
104-
if (null != jvm2fmtMinVersion.put(minimumJvmVersion, minFormatterVersion)) {
104+
if (jvm2fmtMinVersion.put(minimumJvmVersion, minFormatterVersion) != null) {
105105
throw new IllegalArgumentException("Added duplicate entry for JVM %d+.".formatted(minimumJvmVersion));
106106
}
107107
verifyVersionRangesDoNotIntersect(jvm2fmtMinVersion, minimumJvmVersion, minFormatterVersion);
@@ -110,24 +110,24 @@ public Support<V> addMin(int minimumJvmVersion, V minFormatterVersion) {
110110

111111
private void verifyVersionRangesDoNotIntersect(NavigableMap<Integer, V> jvm2fmtVersion, int minimumJvmVersion, V formatterVersion) {
112112
Map.Entry<Integer, V> lower = jvm2fmtVersion.lowerEntry(minimumJvmVersion);
113-
if ((null != lower) && (fmtVersionComparator.compare(formatterVersion, lower.getValue()) <= 0)) {
113+
if ((lower != null) && (fmtVersionComparator.compare(formatterVersion, lower.getValue()) <= 0)) {
114114
throw new IllegalArgumentException("%d/%s should be lower than %d/%s".formatted(minimumJvmVersion, formatterVersion, lower.getKey(), lower.getValue()));
115115
}
116116
Map.Entry<Integer, V> higher = jvm2fmtVersion.higherEntry(minimumJvmVersion);
117-
if ((null != higher) && (fmtVersionComparator.compare(formatterVersion, higher.getValue()) >= 0)) {
117+
if ((higher != null) && (fmtVersionComparator.compare(formatterVersion, higher.getValue()) >= 0)) {
118118
throw new IllegalArgumentException("%d/%s should be higher than %d/%s".formatted(minimumJvmVersion, formatterVersion, higher.getKey(), higher.getValue()));
119119
}
120120
}
121121

122122
/** @return Highest formatter version recommended for this JVM (null, if JVM not supported) */
123123
@Nullable public V getRecommendedFormatterVersion() {
124124
Integer configuredJvmVersionOrNull = jvm2fmtMaxVersion.floorKey(Jvm.version());
125-
return null == configuredJvmVersionOrNull ? null : jvm2fmtMaxVersion.get(configuredJvmVersionOrNull);
125+
return configuredJvmVersionOrNull == null ? null : jvm2fmtMaxVersion.get(configuredJvmVersionOrNull);
126126
}
127127

128128
@Nullable public V getMinimumRequiredFormatterVersion() {
129129
Integer configuredJvmVersionOrNull = jvm2fmtMinVersion.floorKey(Jvm.version());
130-
return null == configuredJvmVersionOrNull ? null : jvm2fmtMinVersion.get(configuredJvmVersionOrNull);
130+
return configuredJvmVersionOrNull == null ? null : jvm2fmtMinVersion.get(configuredJvmVersionOrNull);
131131
}
132132

133133
/**
@@ -151,7 +151,7 @@ private String buildUnsupportedFormatterMessage(V fmtVersion) {
151151
}
152152
// check if the formatter version is too low for the jvm version
153153
V minimumFormatterVersion = getMinimumRequiredFormatterVersion();
154-
if ((null != minimumFormatterVersion) && (fmtVersionComparator.compare(fmtVersion, minimumFormatterVersion) < 0)) {
154+
if ((minimumFormatterVersion != null) && (fmtVersionComparator.compare(fmtVersion, minimumFormatterVersion) < 0)) {
155155
return "You are running Spotless on JVM %d. This requires %s of at least %s (you are using %s).%n".formatted(Jvm.version(), fmtName, minimumFormatterVersion, fmtVersion);
156156
}
157157
// otherwise all is well
@@ -162,11 +162,11 @@ private String buildUpgradeJvmMessage(V fmtVersion) {
162162
StringBuilder builder = new StringBuilder();
163163
builder.append("You are running Spotless on JVM %d".formatted(Jvm.version()));
164164
V recommendedFmtVersionOrNull = getRecommendedFormatterVersion();
165-
if (null != recommendedFmtVersionOrNull) {
165+
if (recommendedFmtVersionOrNull != null) {
166166
builder.append(", which limits you to %s %s.%n".formatted(fmtName, recommendedFmtVersionOrNull));
167167
} else {
168168
Entry<V, Integer> nextFmtVersionOrNull = fmtMaxVersion2jvmVersion.ceilingEntry(fmtVersion);
169-
if (null != nextFmtVersionOrNull) {
169+
if (nextFmtVersionOrNull != null) {
170170
builder.append(". %s %s requires JVM %d+".formatted(fmtName, fmtVersion, nextFmtVersionOrNull.getValue()));
171171
}
172172
builder.append(".%n".formatted());
@@ -176,10 +176,10 @@ private String buildUpgradeJvmMessage(V fmtVersion) {
176176

177177
private int getRequiredJvmVersion(V fmtVersion) {
178178
Entry<V, Integer> entry = fmtMaxVersion2jvmVersion.ceilingEntry(fmtVersion);
179-
if (null == entry) {
179+
if (entry == null) {
180180
entry = fmtMaxVersion2jvmVersion.lastEntry();
181181
}
182-
if (null != entry) {
182+
if (entry != null) {
183183
V maxKnownFmtVersion = jvm2fmtMaxVersion.get(entry.getValue());
184184
if (fmtVersionComparator.compare(fmtVersion, maxKnownFmtVersion) <= 0) {
185185
return entry.getValue();
@@ -227,20 +227,20 @@ private String buildUpgradeFormatterMessage(V fmtVersion) {
227227
// check if the formatter is not supported on this jvm
228228
V minimumFormatterVersion = getMinimumRequiredFormatterVersion();
229229
V recommendedFmtVersionOrNull = getRecommendedFormatterVersion();
230-
if ((null != minimumFormatterVersion) && (fmtVersionComparator.compare(fmtVersion, minimumFormatterVersion) < 0)) {
230+
if ((minimumFormatterVersion != null) && (fmtVersionComparator.compare(fmtVersion, minimumFormatterVersion) < 0)) {
231231
builder.append("You are running Spotless on JVM %d. This requires %s of at least %s.%n".formatted(Jvm.version(), fmtName, minimumFormatterVersion));
232232
builder.append("You are using %s %s.%n".formatted(fmtName, fmtVersion));
233-
if (null != recommendedFmtVersionOrNull) {
233+
if (recommendedFmtVersionOrNull != null) {
234234
builder.append("%s %s is the recommended version, which may have fixed this problem.%n".formatted(fmtName, recommendedFmtVersionOrNull));
235235
}
236236
// check if the formatter is outdated on this jvm
237-
} else if (null != recommendedFmtVersionOrNull && (fmtVersionComparator.compare(fmtVersion, recommendedFmtVersionOrNull) < 0)) {
237+
} else if (recommendedFmtVersionOrNull != null && (fmtVersionComparator.compare(fmtVersion, recommendedFmtVersionOrNull) < 0)) {
238238
builder.append("%s %s is currently being used, but outdated.%n".formatted(fmtName, fmtVersion));
239239
builder.append("%s %s is the recommended version, which may have fixed this problem.%n".formatted(fmtName, recommendedFmtVersionOrNull));
240240
builder.append("%s %s requires JVM %d+.".formatted(fmtName, recommendedFmtVersionOrNull, getRequiredJvmVersion(recommendedFmtVersionOrNull)));
241241
} else {
242242
V higherFormatterVersionOrNull = fmtMaxVersion2jvmVersion.higherKey(fmtVersion);
243-
if (null != higherFormatterVersionOrNull) {
243+
if (higherFormatterVersionOrNull != null) {
244244
builder.append(buildUpgradeJvmMessage(fmtVersion));
245245
Integer higherJvmVersion = fmtMaxVersion2jvmVersion.get(higherFormatterVersionOrNull);
246246
builder.append("If you upgrade your JVM to %d+, then you can use %s %s, which may have fixed this problem.".formatted(higherJvmVersion, fmtName, higherFormatterVersionOrNull));

lib/src/main/java/com/diffplug/spotless/biome/BiomeExecutableDownloader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public Path ensureDownloaded(String version) throws IOException, InterruptedExce
145145
logger.debug("Ensuring that Biome for platform '{}' is downloaded", platform);
146146
var existing = findDownloaded(version);
147147
if (existing.isPresent()) {
148-
logger.debug("Biome was already downloaded, using executable at '{}'", existing.get());
149-
return existing.get();
148+
logger.debug("Biome was already downloaded, using executable at '{}'", existing.orElseThrow());
149+
return existing.orElseThrow();
150150
} else {
151151
logger.debug("Biome was not yet downloaded, attempting to download executable");
152152
return download(version);

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
241241
Optional<String> yearToken = getYearToken(licenseHeader);
242242
if (yearToken.isPresent()) {
243243
this.yearToday = String.valueOf(YearMonth.now().getYear());
244-
int yearTokenIndex = licenseHeader.indexOf(yearToken.get());
244+
int yearTokenIndex = licenseHeader.indexOf(yearToken.orElseThrow());
245245
this.beforeYear = licenseHeader.substring(0, yearTokenIndex);
246-
this.afterYear = licenseHeader.substring(yearTokenIndex + yearToken.get().length());
246+
this.afterYear = licenseHeader.substring(yearTokenIndex + yearToken.orElseThrow().length());
247247
this.yearSepOrFull = yearSeparator;
248248
this.updateYearWithLatest = updateYearWithLatest;
249249

0 commit comments

Comments
 (0)