|
1 | 1 | /*
|
2 |
| - * Copyright 2021 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.
|
|
19 | 19 | import java.util.Objects;
|
20 | 20 | import java.util.stream.Collectors;
|
21 | 21 |
|
| 22 | +import javax.annotation.Nullable; |
22 | 23 | import javax.script.ScriptEngine;
|
23 | 24 | import javax.script.ScriptEngineManager;
|
24 | 25 |
|
25 | 26 | import com.diffplug.spotless.FormatterFunc;
|
26 | 27 | import com.diffplug.spotless.FormatterStep;
|
27 | 28 | import com.diffplug.spotless.JarState;
|
28 | 29 | import com.diffplug.spotless.Provisioner;
|
| 30 | +import com.diffplug.spotless.RoundedStep; |
29 | 31 |
|
30 |
| -public final class Jsr223Step { |
31 |
| - // prevent direct instantiation |
32 |
| - private Jsr223Step() {} |
| 32 | +public final class Jsr223Step implements RoundedStep { |
| 33 | + private static final long serialVersionUID = 1L; |
| 34 | + @Nullable |
| 35 | + private final JarState.Promised jarState; |
| 36 | + private final String engine; |
| 37 | + private final String script; |
33 | 38 |
|
34 |
| - public static FormatterStep create(String name, String dependency, CharSequence engine, CharSequence script, Provisioner provisioner) { |
| 39 | + private Jsr223Step(@Nullable JarState.Promised jarState, String engine, String script) { |
| 40 | + this.jarState = jarState; |
| 41 | + this.engine = engine; |
| 42 | + this.script = script; |
| 43 | + } |
| 44 | + |
| 45 | + public static FormatterStep create(String name, @Nullable String dependency, CharSequence engine, CharSequence script, Provisioner provisioner) { |
35 | 46 | Objects.requireNonNull(name, "name");
|
36 | 47 | Objects.requireNonNull(engine, "engine");
|
37 | 48 | Objects.requireNonNull(script, "script");
|
38 |
| - return FormatterStep.createLazy(name, |
39 |
| - () -> new State(dependency == null ? null : JarState.from(dependency, provisioner), engine, script), |
| 49 | + return FormatterStep.create(name, |
| 50 | + new Jsr223Step(dependency == null ? null : JarState.promise(() -> JarState.from(dependency, provisioner)), engine.toString(), script.toString()), |
| 51 | + Jsr223Step::equalityState, |
40 | 52 | State::toFormatter);
|
41 | 53 | }
|
42 | 54 |
|
| 55 | + private State equalityState() { |
| 56 | + return new State(jarState == null ? null : jarState.get(), engine, script); |
| 57 | + } |
| 58 | + |
43 | 59 | private static final class State implements Serializable {
|
44 | 60 | private static final long serialVersionUID = 1L;
|
45 | 61 |
|
| 62 | + @Nullable |
46 | 63 | private final JarState jarState;
|
47 | 64 | private final String engine;
|
48 | 65 | private final String script;
|
49 | 66 |
|
50 |
| - State(JarState jarState, CharSequence engine, CharSequence script) { |
| 67 | + State(@Nullable JarState jarState, CharSequence engine, CharSequence script) { |
51 | 68 | this.jarState = jarState;
|
52 | 69 | this.engine = engine.toString();
|
53 | 70 | this.script = script.toString();
|
|
0 commit comments