Skip to content

Commit

Permalink
LazyForwardingEquality can now unlazy a FormatterStep which has a d…
Browse files Browse the repository at this point in the history
…elegate field, a well as a `List<FormatterStep>`.

Fixes #1194 (comment)
  • Loading branch information
nedtwigg committed May 10, 2022
1 parent 2be1291 commit 424e42a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
32 changes: 32 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/DelegateFormatterStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless;

import java.util.Objects;

/** Superclass of all compound FormatterSteps necessary for {@link com.diffplug.spotless.LazyForwardingEquality#unlazy(java.lang.Object)}. */
abstract class DelegateFormatterStep implements FormatterStep {
protected final FormatterStep delegateStep;

DelegateFormatterStep(FormatterStep delegateStep) {
this.delegateStep = Objects.requireNonNull(delegateStep);
}

@Override
public final String getName() {
return delegateStep.getName();
}
}
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 All @@ -22,27 +22,19 @@

import javax.annotation.Nullable;

final class FilterByContentPatternFormatterStep implements FormatterStep {
private final FormatterStep delegateStep;
final class FilterByContentPatternFormatterStep extends DelegateFormatterStep {
final Pattern contentPattern;

FilterByContentPatternFormatterStep(FormatterStep delegateStep, String contentPattern) {
this.delegateStep = Objects.requireNonNull(delegateStep);
super(delegateStep);
this.contentPattern = Pattern.compile(Objects.requireNonNull(contentPattern));
}

@Override
public String getName() {
return delegateStep.getName();
}

@Override
public @Nullable String format(String raw, File file) throws Exception {
Objects.requireNonNull(raw, "raw");
Objects.requireNonNull(file, "file");

Matcher matcher = contentPattern.matcher(raw);

if (matcher.find()) {
return delegateStep.format(raw, file);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 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,20 +20,14 @@

import javax.annotation.Nullable;

final class FilterByFileFormatterStep implements FormatterStep {
private final FormatterStep delegateStep;
final class FilterByFileFormatterStep extends DelegateFormatterStep {
private final SerializableFileFilter filter;

FilterByFileFormatterStep(FormatterStep delegateStep, SerializableFileFilter filter) {
this.delegateStep = Objects.requireNonNull(delegateStep);
super(delegateStep);
this.filter = Objects.requireNonNull(filter);
}

@Override
public String getName() {
return delegateStep.getName();
}

@Override
public @Nullable String format(String raw, File file) throws Exception {
Objects.requireNonNull(raw, "raw");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,16 @@ static byte[] toBytes(Serializable obj) {
}

/** Ensures that the lazy state has been evaluated. */
public static void unlazy(LazyForwardingEquality<?> in) {
in.state();
public static void unlazy(Object in) {
if (in instanceof LazyForwardingEquality) {
((LazyForwardingEquality<?>) in).state();
} else if (in instanceof DelegateFormatterStep) {
unlazy(((DelegateFormatterStep) in).delegateStep);
} else if (in instanceof Iterable) {
Iterable<Object> cast = (Iterable<Object>) in;
for (Object c : cast) {
unlazy(c);
}
}
}
}

0 comments on commit 424e42a

Please sign in to comment.