|
| 1 | +/* |
| 2 | + * Copyright 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.IOException; |
| 19 | +import java.io.ObjectStreamException; |
| 20 | +import java.io.Serializable; |
| 21 | + |
| 22 | +import edu.umd.cs.findbugs.annotations.Nullable; |
| 23 | + |
| 24 | +class FormatterStepSerializationRoundtrip<RoundtripState extends Serializable, EqualityState extends Serializable> extends FormatterStepEqualityOnStateSerialization<EqualityState> { |
| 25 | + private final String name; |
| 26 | + private final transient ThrowingEx.Supplier<RoundtripState> initializer; |
| 27 | + private @Nullable RoundtripState roundtripStateInternal; |
| 28 | + private final SerializedFunction<RoundtripState, EqualityState> equalityStateExtractor; |
| 29 | + private final SerializedFunction<EqualityState, FormatterFunc> equalityStateToFormatter; |
| 30 | + |
| 31 | + public FormatterStepSerializationRoundtrip(String name, ThrowingEx.Supplier<RoundtripState> initializer, SerializedFunction<RoundtripState, EqualityState> equalityStateExtractor, SerializedFunction<EqualityState, FormatterFunc> equalityStateToFormatter) { |
| 32 | + this.name = name; |
| 33 | + this.initializer = initializer; |
| 34 | + this.equalityStateExtractor = equalityStateExtractor; |
| 35 | + this.equalityStateToFormatter = equalityStateToFormatter; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getName() { |
| 40 | + return name; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected EqualityState stateSupplier() throws Exception { |
| 45 | + if (roundtripStateInternal != null) { |
| 46 | + roundtripStateInternal = initializer.get(); |
| 47 | + } |
| 48 | + return equalityStateExtractor.apply(roundtripStateInternal); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected FormatterFunc stateToFormatter(EqualityState equalityState) throws Exception { |
| 53 | + return equalityStateToFormatter.apply(equalityState); |
| 54 | + } |
| 55 | + |
| 56 | + // override serialize output |
| 57 | + private void writeObject(java.io.ObjectOutputStream out) throws IOException { |
| 58 | + if (roundtripStateInternal == null) { |
| 59 | + roundtripStateInternal = ThrowingEx.get(initializer::get); |
| 60 | + } |
| 61 | + out.defaultWriteObject(); |
| 62 | + } |
| 63 | + |
| 64 | + private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { |
| 65 | + in.defaultReadObject(); |
| 66 | + } |
| 67 | + |
| 68 | + private void readObjectNoData() throws ObjectStreamException { |
| 69 | + throw new UnsupportedOperationException(); |
| 70 | + } |
| 71 | +} |
0 commit comments