Skip to content

Commit

Permalink
Remove FormatterStep.Strict, an unnecessary implementation detail.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jan 14, 2022
1 parent 3ee9093 commit b3462c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 1 addition & 23 deletions lib/src/main/java/com/diffplug/spotless/FormatterStep.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<State extends Serializable> extends LazyForwardingEquality<State> 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
Expand Down
40 changes: 22 additions & 18 deletions lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

/**
Expand All @@ -32,7 +30,7 @@
* from the API.
*/
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
abstract class FormatterStepImpl<State extends Serializable> extends Strict<State> {
abstract class FormatterStepImpl<State extends Serializable> extends LazyForwardingEquality<State> implements FormatterStep {
private static final long serialVersionUID = 1L;

/** Transient because only the state matters. */
Expand Down Expand Up @@ -67,15 +65,19 @@ static final class Standard<State extends Serializable> 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() {
Expand All @@ -100,15 +102,17 @@ static class NeverUpToDate extends FormatterStepImpl<Integer> {
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);
}
}

Expand Down

0 comments on commit b3462c2

Please sign in to comment.