diff --git a/src/main/java/com/diffplug/gradle/spotless/FormatExtensionStandardImpl.java b/src/main/java/com/diffplug/gradle/spotless/FormatExtensionStandardImpl.java new file mode 100644 index 0000000000..6f14724fef --- /dev/null +++ b/src/main/java/com/diffplug/gradle/spotless/FormatExtensionStandardImpl.java @@ -0,0 +1,86 @@ +/* + * Copyright 2016 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.gradle.spotless; + +import java.io.File; +import java.io.Serializable; +import java.util.Objects; +import java.util.function.BiFunction; + +import com.diffplug.common.base.Throwing; +import com.diffplug.gradle.spotless.FormatterStep.Strict; + +/** + * Standard implementation of FormatExtension which cleanly enforces + * separation of serializable configuration and a pure format function. + */ +abstract class FormatExtensionStandardImpl extends Strict { + private static final long serialVersionUID = 1L; + + /** Transient because only the state matters. */ + final transient String name; + /** Transient because only the state matters. */ + final transient BiFunction formatter; + + public FormatExtensionStandardImpl(String name, BiFunction formatter) { + this.name = Objects.requireNonNull(name); + this.formatter = Objects.requireNonNull(formatter); + } + + @Override + public String getName() { + return name; + } + + @Override + protected String format(Key key, String rawUnix, File file) { + return formatter.apply(key, rawUnix); + } + + static class Eager extends FormatExtensionStandardImpl { + private static final long serialVersionUID = 1L; + + /** Transient because the key is persisted by the superclass. */ + final transient Key key; + + public Eager(String name, Key key, BiFunction formatter) { + super(name, formatter); + this.key = Objects.requireNonNull(key); + } + + @Override + protected Key calculateKey() throws Exception { + return key; + } + } + + static class Lazy extends FormatExtensionStandardImpl { + private static final long serialVersionUID = 1L; + + /** Transient because the key is persisted by the superclass. */ + final transient Throwing.Specific.Supplier keySupplier; + + public Lazy(String name, Throwing.Specific.Supplier keySupplier, BiFunction formatter) { + super(name, formatter); + this.keySupplier = Objects.requireNonNull(keySupplier); + } + + @Override + protected Key calculateKey() throws Exception { + return keySupplier.get(); + } + } +} diff --git a/src/main/java/com/diffplug/gradle/spotless/FormatterStep.java b/src/main/java/com/diffplug/gradle/spotless/FormatterStep.java index 1404425433..6c8df6813b 100644 --- a/src/main/java/com/diffplug/gradle/spotless/FormatterStep.java +++ b/src/main/java/com/diffplug/gradle/spotless/FormatterStep.java @@ -39,7 +39,7 @@ public interface FormatterStep extends Serializable { /** * Returns a formatted version of the given content. * - * @param raw + * @param rawUnix * File's content, guaranteed to have unix-style newlines ('\n') * @param file * the File which is being formatted @@ -92,24 +92,7 @@ public static FormatterStep createLazy( String name, Throwing.Specific.Supplier keySupplier, BiFunction formatter) { - return new Strict() { - private static final long serialVersionUID = 1L; - - @Override - public String getName() { - return name; - } - - @Override - protected Key calculateKey() throws Exception { - return keySupplier.get(); - } - - @Override - protected String format(Key key, String rawUnix, File file) { - return formatter.apply(key, rawUnix); - } - }; + return new FormatExtensionStandardImpl.Lazy<>(name, keySupplier, formatter); } /** @@ -126,24 +109,7 @@ public static FormatterStep create( String name, Key key, BiFunction formatter) { - return new Strict() { - private static final long serialVersionUID = 1L; - - @Override - public String getName() { - return name; - } - - @Override - protected Key calculateKey() throws Exception { - return key; - } - - @Override - protected String format(Key key, String rawUnix, File file) { - return formatter.apply(key, rawUnix); - } - }; + return new FormatExtensionStandardImpl.Eager<>(name, key, formatter); } /** A FormatterStep which doesn't depend on the input file. */