1
1
/*
2
- * Copyright 2021-2023 DiffPlug
2
+ * Copyright 2021-2024 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 .json ;
17
17
18
- import java .io .IOException ;
19
18
import java .io .Serializable ;
20
19
import java .lang .reflect .Constructor ;
21
20
import java .lang .reflect .InvocationTargetException ;
26
25
import com .diffplug .spotless .FormatterStep ;
27
26
import com .diffplug .spotless .JarState ;
28
27
import com .diffplug .spotless .Provisioner ;
28
+ import com .diffplug .spotless .RoundedStep ;
29
29
30
30
/**
31
31
* Simple JSON formatter which reformats the file according to the org.json library's default pretty-printing, but has no ability to customise more than the indentation size.
32
32
*/
33
- public final class JsonSimpleStep {
33
+ public final class JsonSimpleStep implements RoundedStep {
34
+ private static final long serialVersionUID = 1L ;
34
35
private static final String MAVEN_COORDINATE = "org.json:json:" ;
35
36
private static final String DEFAULT_VERSION = "20210307" ;
37
+ public static final String NAME = "jsonSimple" ;
38
+
39
+ private final JarState .Promised jarState ;
40
+ private final int indentSpaces ;
41
+
42
+ private JsonSimpleStep (JarState .Promised jarState , int indentSpaces ) {
43
+ this .indentSpaces = indentSpaces ;
44
+ this .jarState = jarState ;
45
+ }
36
46
37
47
public static FormatterStep create (int indent , Provisioner provisioner ) {
38
48
Objects .requireNonNull (provisioner , "provisioner cannot be null" );
39
- return FormatterStep .createLazy ("json" , () -> new State (indent , provisioner ), State ::toFormatter );
49
+ return FormatterStep .create (NAME ,
50
+ new JsonSimpleStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + DEFAULT_VERSION , provisioner )), indent ),
51
+ JsonSimpleStep ::equalityState ,
52
+ State ::toFormatter );
53
+ }
54
+
55
+ private State equalityState () {
56
+ return new State (jarState .get (), indentSpaces );
40
57
}
41
58
42
59
private static final class State implements Serializable {
@@ -45,9 +62,9 @@ private static final class State implements Serializable {
45
62
private final int indentSpaces ;
46
63
private final JarState jarState ;
47
64
48
- private State (int indent , Provisioner provisioner ) throws IOException {
65
+ State (JarState jarState , int indent ) {
66
+ this .jarState = jarState ;
49
67
this .indentSpaces = indent ;
50
- this .jarState = JarState .from (MAVEN_COORDINATE + DEFAULT_VERSION , provisioner );
51
68
}
52
69
53
70
FormatterFunc toFormatter () {
@@ -94,8 +111,4 @@ private String format(Constructor<?> constructor, Method toString, String input)
94
111
}
95
112
}
96
113
}
97
-
98
- private JsonSimpleStep () {
99
- // cannot be directly instantiated
100
- }
101
114
}
0 commit comments