Skip to content

Commit 46635c6

Browse files
committed
Fixup the FormatterStep api.
1 parent c56968a commit 46635c6

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ static <RoundtripState extends Serializable, EqualityState extends Serializable>
127127
return new FormatterStepSerializationRoundtrip(name, roundtripInit, equalityFunc, formatterFunc);
128128
}
129129

130+
/**
131+
* @param name
132+
* The name of the formatter step.
133+
* @param roundTrip
134+
* The roundtrip serializable state of the step.
135+
* @param equalityFunc
136+
* A pure serializable function (method reference recommended) which takes the result of `roundTrip`,
137+
* and returns a serializable object whose serialized representation will be used for `.equals` and
138+
* `.hashCode` of the FormatterStep.
139+
* @param formatterFunc
140+
* A pure serializable function (method reference recommended) which takes the result of `equalityFunc`,
141+
* and returns a `FormatterFunc` which will be used for the actual formatting.
142+
* @return A FormatterStep which can be losslessly roundtripped through the java serialization machinery.
143+
*/
144+
static <RoundtripState extends Serializable, EqualityState extends Serializable> FormatterStep create(
145+
String name,
146+
RoundtripState roundTrip,
147+
SerializedFunction<RoundtripState, EqualityState> equalityFunc,
148+
SerializedFunction<EqualityState, FormatterFunc> formatterFunc) {
149+
return createLazy(name, () -> roundTrip, equalityFunc, formatterFunc);
150+
}
151+
130152
/**
131153
* @param name
132154
* The name of the formatter step

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* and such without interfering with buildcache keys.
3030
*/
3131
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
32-
public abstract class FormatterStepEqualityOnStateSerialization<State extends Serializable> implements FormatterStep, Serializable {
32+
abstract class FormatterStepEqualityOnStateSerialization<State extends Serializable> implements FormatterStep, Serializable {
3333
private static final long serialVersionUID = 1L;
3434

3535
protected abstract State stateSupplier() throws Exception;
@@ -80,11 +80,7 @@ private State state() throws Exception {
8080

8181
private byte[] serializedState() {
8282
if (serializedStateInternal == null) {
83-
try {
84-
serializedStateInternal = LazyForwardingEquality.toBytes(state());
85-
} catch (Exception e) {
86-
throw new RuntimeException(e);
87-
}
83+
serializedStateInternal = ThrowingEx.get(() -> LazyForwardingEquality.toBytes(state()));
8884
}
8985
return serializedStateInternal;
9086
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public String getName() {
4242

4343
@Override
4444
protected EqualityState stateSupplier() throws Exception {
45-
if (roundtripStateInternal != null) {
45+
if (roundtripStateInternal == null) {
4646
roundtripStateInternal = initializer.get();
4747
}
4848
return equalityStateExtractor.apply(roundtripStateInternal);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
import java.io.Serializable;
1919

2020
@FunctionalInterface
21-
interface SerializedFunction<T, R> extends Serializable, ThrowingEx.Function<T, R> {}
21+
public interface SerializedFunction<T, R> extends Serializable, ThrowingEx.Function<T, R> {
22+
static <T> SerializedFunction<T, T> identity() {
23+
return t -> t;
24+
}
25+
}

0 commit comments

Comments
 (0)