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 .yaml ;
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 ;
25
24
import com .diffplug .spotless .FormatterStep ;
26
25
import com .diffplug .spotless .JarState ;
27
26
import com .diffplug .spotless .Provisioner ;
27
+ import com .diffplug .spotless .RoundedStep ;
28
28
29
29
/**
30
30
* Simple YAML formatter which reformats the file according to Jackson YAMLFactory.
31
31
*/
32
32
// https://stackoverflow.com/questions/14515994/convert-json-string-to-pretty-print-json-output-using-jackson
33
33
// https://stackoverflow.com/questions/60891174/i-want-to-load-a-yaml-file-possibly-edit-the-data-and-then-dump-it-again-how
34
- public class JacksonYamlStep {
35
- static final String MAVEN_COORDINATE = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" ;
36
- // https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml
37
- static final String DEFAULT_VERSION = "2.14.1" ;
34
+ public class JacksonYamlStep implements RoundedStep {
35
+ private static final long serialVersionUID = 1L ;
36
+ private static final String MAVEN_COORDINATE = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" ;
37
+ private static final String DEFAULT_VERSION = "2.14.1" ;
38
+ public static final String NAME = "jacksonYaml" ;
38
39
39
- private JacksonYamlStep () {}
40
+ private final JarState .Promised jarState ;
41
+ private final JacksonYamlConfig jacksonConfig ;
42
+
43
+ private JacksonYamlStep (JarState .Promised jarState , JacksonYamlConfig jacksonConfig ) {
44
+ this .jarState = jarState ;
45
+ this .jacksonConfig = jacksonConfig ;
46
+ }
40
47
41
48
public static String defaultVersion () {
42
49
return DEFAULT_VERSION ;
@@ -47,28 +54,29 @@ public static FormatterStep create(JacksonYamlConfig jacksonConfig,
47
54
Provisioner provisioner ) {
48
55
Objects .requireNonNull (jacksonConfig , "jacksonConfig cannot be null" );
49
56
Objects .requireNonNull (provisioner , "provisioner cannot be null" );
50
- return FormatterStep .createLazy ("yaml" ,
51
- () -> new State (jacksonConfig , jacksonVersion , provisioner ),
57
+ return FormatterStep .create (NAME ,
58
+ new JacksonYamlStep (JarState .promise (() -> JarState .from (MAVEN_COORDINATE + jacksonVersion , provisioner )), jacksonConfig ),
59
+ JacksonYamlStep ::equalityState ,
52
60
State ::toFormatter );
53
61
}
54
62
55
63
public static FormatterStep create (Provisioner provisioner ) {
56
64
return create (new JacksonYamlConfig (), defaultVersion (), provisioner );
57
65
}
58
66
67
+ private State equalityState () {
68
+ return new State (jarState .get (), jacksonConfig );
69
+ }
70
+
59
71
private static final class State implements Serializable {
60
72
private static final long serialVersionUID = 1L ;
61
73
62
74
private final JacksonYamlConfig jacksonConfig ;
63
-
64
75
private final JarState jarState ;
65
76
66
- private State (JacksonYamlConfig jacksonConfig ,
67
- String jacksonVersion ,
68
- Provisioner provisioner ) throws IOException {
77
+ State (JarState jarState , JacksonYamlConfig jacksonConfig ) {
78
+ this .jarState = jarState ;
69
79
this .jacksonConfig = jacksonConfig ;
70
-
71
- this .jarState = JarState .from (JacksonYamlStep .MAVEN_COORDINATE + jacksonVersion , provisioner );
72
80
}
73
81
74
82
FormatterFunc toFormatter () throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException ,
0 commit comments