-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FormatExceptionPolicy implementations need to implement Serializable …
…and equals/hashCode with the same constraints as SerializableFileFilter. Refactored the `toBytes()` method into the interface `NoLambda`, and moved `SerializableFileFilter.EqualityBasedOnSerialization` to `NoLambda.EqualityBasedOnSerialization`. Technically a breaking change, but will affect exactly zero real-world client code.
- Loading branch information
Showing
5 changed files
with
75 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.spotless; | ||
|
||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Marker interface to prevent lambda implementations of | ||
* single-method interfaces that require serializability. | ||
* | ||
* In order for Spotless to support up-to-date checks, all | ||
* of its parameters must be {@link Serializable} so that | ||
* entries can be written to file, and they must implement | ||
* equals and hashCode correctly. | ||
* | ||
* This interface and its standard implementation, | ||
* {@link EqualityBasedOnSerialization}, are a quick way | ||
* to accomplish these goals. | ||
*/ | ||
public interface NoLambda extends Serializable { | ||
/** | ||
* Returns a byte array representation of everything inside this `SerializableFileFilter`. | ||
* | ||
* The main purpose of this method is to ensure one can't instantiate this class with lambda | ||
* expressions, which are notoriously difficult to serialize and deserialize properly. (See | ||
* `SerializableFileFilterImpl.SkipFilesNamed` for an example of how to make a serializable | ||
* subclass.) | ||
*/ | ||
public byte[] toBytes(); | ||
|
||
/** An implementation of NoLambda in which equality is based on the serialized representation of itself. */ | ||
public static abstract class EqualityBasedOnSerialization implements NoLambda { | ||
private static final long serialVersionUID = 1733798699224768949L; | ||
|
||
@Override | ||
public byte[] toBytes() { | ||
return LazyForwardingEquality.toBytes(this); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(toBytes()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object otherObj) { | ||
if (otherObj == null) { | ||
return false; | ||
} else if (otherObj.getClass().equals(this.getClass())) { | ||
EqualityBasedOnSerialization other = (EqualityBasedOnSerialization) otherObj; | ||
return Arrays.equals(toBytes(), other.toBytes()); | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters