Skip to content

Commit decbd1b

Browse files
committed
Convert IndentStep to be roundtrip serializable.
1 parent cb346a4 commit decbd1b

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -15,19 +15,38 @@
1515
*/
1616
package com.diffplug.spotless.generic;
1717

18-
import java.io.Serializable;
19-
import java.util.Objects;
20-
2118
import com.diffplug.spotless.FormatterFunc;
2219
import com.diffplug.spotless.FormatterStep;
20+
import com.diffplug.spotless.FormatterStepEqualityOnStateSerialization;
2321

2422
/** Simple step which checks for consistent indentation characters. */
25-
public final class IndentStep {
23+
public final class IndentStep extends FormatterStepEqualityOnStateSerialization<IndentStep> {
24+
private static final long serialVersionUID = 1L;
2625

27-
private static final int DEFAULT_NUM_SPACES_PER_TAB = 4;
26+
final Type type;
27+
final int numSpacesPerTab;
28+
29+
private IndentStep(Type type, int numSpacesPerTab) {
30+
this.type = type;
31+
this.numSpacesPerTab = numSpacesPerTab;
32+
}
33+
34+
@Override
35+
public String getName() {
36+
return "indentWith" + type.tabSpace("Tabs", "Spaces");
37+
}
38+
39+
@Override
40+
protected IndentStep stateSupplier() {
41+
return this;
42+
}
43+
44+
@Override
45+
protected FormatterFunc stateToFormatter(IndentStep state) {
46+
return new Runtime(this)::format;
47+
}
2848

29-
// prevent direct instantiation
30-
private IndentStep() {}
49+
private static final int DEFAULT_NUM_SPACES_PER_TAB = 4;
3150

3251
public enum Type {
3352
TAB, SPACE;
@@ -49,32 +68,14 @@ public FormatterStep create(int numSpacesPerTab) {
4968

5069
/** Creates a step which will indent with the given type of whitespace, converting between tabs and spaces at the given ratio. */
5170
public static FormatterStep create(Type type, int numSpacesPerTab) {
52-
Objects.requireNonNull(type, "type");
53-
return FormatterStep.create("indentWith" + type.tabSpace("Tabs", "Spaces"),
54-
new State(type, numSpacesPerTab), State::toFormatter);
55-
}
56-
57-
private static class State implements Serializable {
58-
private static final long serialVersionUID = 1L;
59-
60-
final Type type;
61-
final int numSpacesPerTab;
62-
63-
State(Type type, int numSpacesPerTab) {
64-
this.type = type;
65-
this.numSpacesPerTab = numSpacesPerTab;
66-
}
67-
68-
FormatterFunc toFormatter() {
69-
return new Runtime(this)::format;
70-
}
71+
return new IndentStep(type, numSpacesPerTab);
7172
}
7273

7374
static class Runtime {
74-
final State state;
75+
final IndentStep state;
7576
final StringBuilder builder = new StringBuilder();
7677

77-
Runtime(State state) {
78+
Runtime(IndentStep state) {
7879
this.state = state;
7980
}
8081

0 commit comments

Comments
 (0)