|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.Serializable; |
| 20 | +import java.util.Arrays; |
| 21 | + |
| 22 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 23 | + |
| 24 | +/** |
| 25 | + * Standard implementation of FormatterStep which cleanly enforces |
| 26 | + * separation of a lazily computed "state" object whose serialized form |
| 27 | + * is used as the basis for equality and hashCode, which is separate |
| 28 | + * from the serialized form of the step itself, which can include absolute paths |
| 29 | + * and such without interfering with buildcache keys. |
| 30 | + */ |
| 31 | +@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") |
| 32 | +public abstract class FormatterStepEqualityOnStateSerialization<State extends Serializable> implements FormatterStep, Serializable { |
| 33 | + private static final long serialVersionUID = 1L; |
| 34 | + |
| 35 | + protected abstract State stateSupplier() throws Exception; |
| 36 | + |
| 37 | + protected abstract FormatterFunc stateToFormatter(State state) throws Exception; |
| 38 | + |
| 39 | + private transient FormatterFunc formatter; |
| 40 | + private transient State stateInternal; |
| 41 | + private transient byte[] serializedStateInternal; |
| 42 | + |
| 43 | + @Override |
| 44 | + public String format(String rawUnix, File file) throws Exception { |
| 45 | + if (formatter == null) { |
| 46 | + formatter = stateToFormatter(state()); |
| 47 | + } |
| 48 | + return formatter.apply(rawUnix, file); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean equals(Object o) { |
| 53 | + if (o == null) { |
| 54 | + return false; |
| 55 | + } else if (getClass() != o.getClass()) { |
| 56 | + return false; |
| 57 | + } else { |
| 58 | + return Arrays.equals(serializedState(), ((FormatterStepEqualityOnStateSerialization<?>) o).serializedState()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public int hashCode() { |
| 64 | + return Arrays.hashCode(serializedState()); |
| 65 | + } |
| 66 | + |
| 67 | + void cleanupFormatterFunc() { |
| 68 | + if (formatter instanceof FormatterFunc.Closeable) { |
| 69 | + ((FormatterFunc.Closeable) formatter).close(); |
| 70 | + formatter = null; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private State state() throws Exception { |
| 75 | + if (stateInternal == null) { |
| 76 | + stateInternal = stateSupplier(); |
| 77 | + } |
| 78 | + return stateInternal; |
| 79 | + } |
| 80 | + |
| 81 | + private byte[] serializedState() { |
| 82 | + if (serializedStateInternal == null) { |
| 83 | + try { |
| 84 | + serializedStateInternal = LazyForwardingEquality.toBytes(state()); |
| 85 | + } catch (Exception e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + return serializedStateInternal; |
| 90 | + } |
| 91 | +} |
0 commit comments