1
1
/*
2
- * Copyright 2016-2022 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package com .diffplug .spotless .generic ;
17
17
18
- import java .io .Serializable ;
19
- import java .util .Objects ;
20
-
21
18
import com .diffplug .spotless .FormatterFunc ;
22
19
import com .diffplug .spotless .FormatterStep ;
20
+ import com .diffplug .spotless .FormatterStepEqualityOnStateSerialization ;
23
21
24
22
/** 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 ;
26
25
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
+ }
28
48
29
- // prevent direct instantiation
30
- private IndentStep () {}
49
+ private static final int DEFAULT_NUM_SPACES_PER_TAB = 4 ;
31
50
32
51
public enum Type {
33
52
TAB , SPACE ;
@@ -49,32 +68,14 @@ public FormatterStep create(int numSpacesPerTab) {
49
68
50
69
/** Creates a step which will indent with the given type of whitespace, converting between tabs and spaces at the given ratio. */
51
70
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 );
71
72
}
72
73
73
74
static class Runtime {
74
- final State state ;
75
+ final IndentStep state ;
75
76
final StringBuilder builder = new StringBuilder ();
76
77
77
- Runtime (State state ) {
78
+ Runtime (IndentStep state ) {
78
79
this .state = state ;
79
80
}
80
81
0 commit comments