From b3462c2218d06c617c002bb83536c1a5883b18b7 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Jan 2022 15:27:42 -0800 Subject: [PATCH] Remove `FormatterStep.Strict`, an unnecessary implementation detail. --- CHANGES.md | 1 + .../com/diffplug/spotless/FormatterStep.java | 24 +---------- .../diffplug/spotless/FormatterStepImpl.java | 40 ++++++++++--------- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 61dd4cf630..1ba1965985 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [TBD lint release] * **BREAKING** Removed `isClean`, `applyTo`, and `applyToAndReturnResultIfDirty` from `Formatter` because users should instead use `PaddedCell.check()`. +* **BREAKING** Removed `FormatterStep.Strict` because it was unnecessary and unused implementation detail. ## [Unreleased] ### Changed diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java index 5729f676b2..763232f936 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,28 +67,6 @@ public default FormatterStep filterByFile(SerializableFileFilter filter) { return new FilterByFileFormatterStep(this, filter); } - /** - * Implements a FormatterStep in a strict way which guarantees correct and lazy implementation - * of up-to-date checks. This maximizes performance for cases where the FormatterStep is not - * actually needed (e.g. don't load eclipse setting file unless this step is actually running) - * while also ensuring that gradle can detect changes in a step's settings to determine that - * it needs to rerun a format. - */ - abstract class Strict extends LazyForwardingEquality implements FormatterStep { - private static final long serialVersionUID = 1L; - - /** - * Implements the formatting function strictly in terms - * of the input data and the result of {@link #calculateState()}. - */ - protected abstract String format(State state, String rawUnix, File file) throws Exception; - - @Override - public final String format(String rawUnix, File file) throws Exception { - return format(state(), rawUnix, file); - } - } - /** * @param name * The name of the formatter step diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index 061aff6af9..f9672d7dcd 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +20,6 @@ import java.util.Objects; import java.util.Random; -import com.diffplug.spotless.FormatterStep.Strict; - import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; /** @@ -32,7 +30,7 @@ * from the API. */ @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") -abstract class FormatterStepImpl extends Strict { +abstract class FormatterStepImpl extends LazyForwardingEquality implements FormatterStep { private static final long serialVersionUID = 1L; /** Transient because only the state matters. */ @@ -67,15 +65,19 @@ static final class Standard extends FormatterStepImp this.stateToFormatter = Objects.requireNonNull(stateToFormatter); } + private FormatterFunc func() throws Exception { + if (formatter != null) { + return formatter; + } + formatter = stateToFormatter.apply(state()); + return formatter; + } + @Override - protected String format(State state, String rawUnix, File file) throws Exception { - Objects.requireNonNull(state, "state"); + public String format(String rawUnix, File file) throws Exception { Objects.requireNonNull(rawUnix, "rawUnix"); Objects.requireNonNull(file, "file"); - if (formatter == null) { - formatter = stateToFormatter.apply(state()); - } - return formatter.apply(rawUnix, file); + return func().apply(rawUnix, file); } void cleanupFormatterFunc() { @@ -100,15 +102,17 @@ static class NeverUpToDate extends FormatterStepImpl { this.formatterSupplier = Objects.requireNonNull(formatterSupplier, "formatterSupplier"); } - @Override - protected String format(Integer state, String rawUnix, File file) throws Exception { - if (formatter == null) { - formatter = formatterSupplier.get(); - if (formatter instanceof FormatterFunc.Closeable) { - throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284"); - } + private FormatterFunc func() throws Exception { + if (formatter != null) { + return formatter; } - return formatter.apply(rawUnix, file); + formatter = formatterSupplier.get(); + return formatter; + } + + @Override + public String format(String rawUnix, File file) throws Exception { + return func().apply(rawUnix, file); } }