1
1
/*
2
- * Copyright 2023 DiffPlug
2
+ * Copyright 2023-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 ;
22
21
import java .util .List ;
23
22
import java .util .Map ;
24
23
import java .util .Objects ;
25
24
25
+ import javax .annotation .Nullable ;
26
+
26
27
import com .diffplug .spotless .FormatterFunc ;
27
28
import com .diffplug .spotless .FormatterStep ;
28
29
import com .diffplug .spotless .JarState ;
29
30
import com .diffplug .spotless .Provisioner ;
31
+ import com .diffplug .spotless .RoundedStep ;
32
+
33
+ public class JsonPatchStep implements RoundedStep {
34
+ private static final long serialVersionUID = 1L ;
35
+ private static final String MAVEN_COORDINATE = "com.flipkart.zjsonpatch:zjsonpatch" ;
36
+ private static final String DEFAULT_VERSION = "0.4.14" ;
37
+ public static final String NAME = "apply-json-patch" ;
30
38
31
- public class JsonPatchStep {
32
- // https://mvnrepository.com/artifact/com.flipkart.zjsonpatch/zjsonpatch
33
- static final String MAVEN_COORDINATE = "com.flipkart.zjsonpatch:zjsonpatch" ;
34
- static final String DEFAULT_VERSION = "0.4.14" ;
39
+ private final JarState .Promised jarState ;
40
+ @ Nullable
41
+ private final List <Map <String , Object >> patch ;
42
+ @ Nullable
43
+ private final String patchString ;
35
44
36
- private JsonPatchStep () {}
45
+ private JsonPatchStep (JarState .Promised jarState ,
46
+ @ Nullable String patchString ,
47
+ @ Nullable List <Map <String , Object >> patch ) {
48
+ this .jarState = jarState ;
49
+ this .patchString = patchString ;
50
+ this .patch = patch ;
51
+ }
37
52
38
53
public static FormatterStep create (String patchString , Provisioner provisioner ) {
39
54
return create (DEFAULT_VERSION , patchString , provisioner );
@@ -43,7 +58,10 @@ public static FormatterStep create(String zjsonPatchVersion, String patchString,
43
58
Objects .requireNonNull (zjsonPatchVersion , "zjsonPatchVersion cannot be null" );
44
59
Objects .requireNonNull (patchString , "patchString cannot be null" );
45
60
Objects .requireNonNull (provisioner , "provisioner cannot be null" );
46
- return FormatterStep .createLazy ("apply-json-patch" , () -> new State (zjsonPatchVersion , patchString , provisioner ), State ::toFormatter );
61
+ return FormatterStep .create (NAME ,
62
+ new JsonPatchStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + ":" + zjsonPatchVersion , provisioner )), patchString , null ),
63
+ JsonPatchStep ::equalityState ,
64
+ State ::toFormatter );
47
65
}
48
66
49
67
public static FormatterStep create (List <Map <String , Object >> patch , Provisioner provisioner ) {
@@ -54,26 +72,31 @@ public static FormatterStep create(String zjsonPatchVersion, List<Map<String, Ob
54
72
Objects .requireNonNull (zjsonPatchVersion , "zjsonPatchVersion cannot be null" );
55
73
Objects .requireNonNull (patch , "patch cannot be null" );
56
74
Objects .requireNonNull (provisioner , "provisioner cannot be null" );
57
- return FormatterStep .createLazy ("apply-json-patch" , () -> new State (zjsonPatchVersion , patch , provisioner ), State ::toFormatter );
75
+ return FormatterStep .create (NAME ,
76
+ new JsonPatchStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + ":" + zjsonPatchVersion , provisioner )), null , patch ),
77
+ JsonPatchStep ::equalityState ,
78
+ State ::toFormatter );
79
+ }
80
+
81
+ private State equalityState () {
82
+ return new State (jarState .get (), patchString , patch );
58
83
}
59
84
60
85
static final class State implements Serializable {
61
86
private static final long serialVersionUID = 1L ;
62
87
63
88
private final JarState jarState ;
89
+ @ Nullable
64
90
private final List <Map <String , Object >> patch ;
91
+ @ Nullable
65
92
private final String patchString ;
66
93
67
- private State (String zjsonPatchVersion , List <Map <String , Object >> patch , Provisioner provisioner ) throws IOException {
68
- this .jarState = JarState .from (MAVEN_COORDINATE + ":" + zjsonPatchVersion , provisioner );
69
- this .patch = patch ;
70
- this .patchString = null ;
71
- }
72
-
73
- private State (String zjsonPatchVersion , String patchString , Provisioner provisioner ) throws IOException {
74
- this .jarState = JarState .from (MAVEN_COORDINATE + ":" + zjsonPatchVersion , provisioner );
75
- this .patch = null ;
94
+ State (JarState jarState ,
95
+ @ Nullable String patchString ,
96
+ @ Nullable List <Map <String , Object >> patch ) {
97
+ this .jarState = jarState ;
76
98
this .patchString = patchString ;
99
+ this .patch = patch ;
77
100
}
78
101
79
102
FormatterFunc toFormatter () throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException {
0 commit comments