Skip to content

Commit 6d4e268

Browse files
committed
Move NeverUpToDateStep out of lib and into testlib only.
1 parent 7ea3958 commit 6d4e268

File tree

5 files changed

+100
-9
lines changed

5 files changed

+100
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ static <RoundtripState extends Serializable, EqualityState extends Serializable>
151151
static <State extends Serializable> FormatterStep createLazy(
152152
String name,
153153
ThrowingEx.Supplier<State> stateSupplier,
154-
ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
155-
return new FormatterStepImpl.Standard<>(name, stateSupplier, stateToFormatter);
154+
SerializedFunction<State, FormatterFunc> stateToFormatter) {
155+
return createLazy(name, stateSupplier, SerializedFunction.identity(), stateToFormatter);
156156
}
157157

158158
/**
@@ -168,7 +168,7 @@ static <State extends Serializable> FormatterStep createLazy(
168168
static <State extends Serializable> FormatterStep create(
169169
String name,
170170
State state,
171-
ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
171+
SerializedFunction<State, FormatterFunc> stateToFormatter) {
172172
Objects.requireNonNull(state, "state");
173173
return createLazy(name, () -> state, stateToFormatter);
174174
}
@@ -185,7 +185,7 @@ static <State extends Serializable> FormatterStep create(
185185
static FormatterStep createNeverUpToDateLazy(
186186
String name,
187187
ThrowingEx.Supplier<FormatterFunc> functionSupplier) {
188-
return new FormatterStepImpl.NeverUpToDate(name, functionSupplier);
188+
return new NeverUpToDateStep(name, functionSupplier);
189189
}
190190

191191
/**

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.diffplug.spotless.FormatterFunc;
3535
import com.diffplug.spotless.FormatterStep;
3636
import com.diffplug.spotless.LineEnding;
37+
import com.diffplug.spotless.NeverUpToDateStep;
3738
import com.diffplug.spotless.ResourceHarness;
3839
import com.diffplug.spotless.TestProvisioner;
3940

@@ -56,7 +57,7 @@ public BuildServiceParameters.None getParameters() {
5657
Bundle(String name, FormatterFunc function) throws IOException {
5758
this.name = name;
5859
file = setFile("src/test." + name).toContent("CCC");
59-
FormatterStep step = FormatterStep.createNeverUpToDate(name, function);
60+
FormatterStep step = NeverUpToDateStep.create(name, function);
6061
source = createFormatTask(name, step);
6162
check = createCheckTask(name, source);
6263
apply = createApplyTask(name, source);

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import com.diffplug.spotless.Formatter;
4040
import com.diffplug.spotless.FormatterStep;
4141
import com.diffplug.spotless.LineEnding;
42+
import com.diffplug.spotless.NeverUpToDateStep;
4243
import com.diffplug.spotless.maven.MavenIntegrationHarness;
4344

4445
class PluginFingerprintTest extends MavenIntegrationHarness {
@@ -162,7 +163,7 @@ private static Model readPom(String xml) throws Exception {
162163
}
163164

164165
private static FormatterStep formatterStep(String name) {
165-
return FormatterStep.createNeverUpToDate(name, input -> input);
166+
return NeverUpToDateStep.create(name, input -> input);
166167
}
167168

168169
private static Formatter formatter(FormatterStep... steps) {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless;
17+
18+
import java.io.File;
19+
import java.util.Objects;
20+
21+
/**
22+
* Formatter which is equal to itself, but not to any other Formatter.
23+
*/
24+
public class NeverUpToDateStep implements FormatterStep {
25+
/**
26+
* @param name
27+
* The name of the formatter step
28+
* @param functionSupplier
29+
* A supplier which will lazily generate the function
30+
* used by the formatter step
31+
* @return A FormatterStep which will never report that it is up-to-date, because
32+
* it is not equal to the serialized representation of itself.
33+
*/
34+
public static FormatterStep createLazy(
35+
String name,
36+
ThrowingEx.Supplier<FormatterFunc> functionSupplier) {
37+
return new NeverUpToDateStep(name, functionSupplier);
38+
}
39+
40+
/**
41+
* @param name
42+
* The name of the formatter step
43+
* @param function
44+
* The function used by the formatter step
45+
* @return A FormatterStep which will never report that it is up-to-date, because
46+
* it is not equal to the serialized representation of itself.
47+
*/
48+
public static FormatterStep create(
49+
String name,
50+
FormatterFunc function) {
51+
Objects.requireNonNull(function, "function");
52+
return createLazy(name, () -> function);
53+
}
54+
55+
private static final long serialVersionUID = 1L;
56+
57+
private final String name;
58+
private final ThrowingEx.Supplier<FormatterFunc> formatterSupplier;
59+
private transient FormatterFunc formatter; // initialized lazily
60+
61+
NeverUpToDateStep(String name, ThrowingEx.Supplier<FormatterFunc> formatterSupplier) {
62+
this.name = name;
63+
this.formatterSupplier = Objects.requireNonNull(formatterSupplier, "formatterSupplier");
64+
}
65+
66+
@Override
67+
public String getName() {
68+
return name;
69+
}
70+
71+
@Override
72+
public String format(String rawUnix, File file) throws Exception {
73+
if (formatter == null) {
74+
formatter = formatterSupplier.get();
75+
if (formatter instanceof FormatterFunc.Closeable) {
76+
throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284");
77+
}
78+
}
79+
return formatter.apply(rawUnix, file);
80+
}
81+
82+
@Override
83+
public void close() throws Exception {
84+
if (formatter instanceof FormatterFunc.Closeable) {
85+
((FormatterFunc.Closeable) formatter).close();
86+
formatter = null;
87+
}
88+
}
89+
}

testlib/src/test/java/com/diffplug/spotless/PaddedCellTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,7 @@ private void wellBehaved(FormatterFunc step, String input, PaddedCell.Type expec
4747

4848
private void testCase(FormatterFunc step, String input, PaddedCell.Type expectedOutputType, String expectedSteps, String canonical, boolean misbehaved) throws IOException {
4949
List<FormatterStep> formatterSteps = new ArrayList<>();
50-
formatterSteps.add(FormatterStep.createNeverUpToDate("step", step));
50+
formatterSteps.add(NeverUpToDateStep.create("step", step));
5151
try (Formatter formatter = Formatter.builder()
5252
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
5353
.encoding(StandardCharsets.UTF_8)

0 commit comments

Comments
 (0)