diff --git a/build.gradle b/build.gradle index 98cb03ecfd..5d5d533ffb 100644 --- a/build.gradle +++ b/build.gradle @@ -82,6 +82,7 @@ dependencies { compile "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}" testCompile "junit:junit:${VER_JUNIT}" testCompile "org.assertj:assertj-core:${VER_ASSERTJ}" + testCompile "com.diffplug.durian:durian-testlib:${VER_DURIAN}" // add the eclipse jars to the embedded configuration eclipseDeps.each { embeddedJars "p2:${it}:+" } diff --git a/src/main/java/com/diffplug/gradle/spotless/ForwardingEquality.java b/src/main/java/com/diffplug/gradle/spotless/ForwardingEquality.java new file mode 100644 index 0000000000..b067060e89 --- /dev/null +++ b/src/main/java/com/diffplug/gradle/spotless/ForwardingEquality.java @@ -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.gradle.spotless; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.ObjectStreamException; +import java.io.Serializable; +import java.util.Objects; + +/** + * Implements equality, hashcode, and serialization entirely in terms + * of the given key. It is appropriate for subclasses of this class + * to use `@SuppressWarnings("serial")`. + */ +@SuppressWarnings("serial") +public abstract class ForwardingEquality implements Serializable { + private T key; + + ForwardingEquality(T key) { + this.key = Objects.requireNonNull(key); + } + + protected T key() { + return key; + } + + private void writeObject(ObjectOutputStream out) throws IOException { + out.writeObject(key); + } + + @SuppressWarnings("unchecked") + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + key = (T) Objects.requireNonNull(in.readObject()); + } + + @SuppressWarnings("unused") + private void readObjectNoData() throws ObjectStreamException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } else if (getClass().equals(other.getClass())) { + return key.equals(((ForwardingEquality) other).key); + } else { + return false; + } + } + + @Override + public int hashCode() { + return key.hashCode(); + } +} diff --git a/src/test/java/com/diffplug/gradle/spotless/ForwardingEqualityTest.java b/src/test/java/com/diffplug/gradle/spotless/ForwardingEqualityTest.java new file mode 100644 index 0000000000..3c053e9d03 --- /dev/null +++ b/src/test/java/com/diffplug/gradle/spotless/ForwardingEqualityTest.java @@ -0,0 +1,63 @@ +/* + * 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 org.junit.Test; + +import com.diffplug.common.testing.EqualsTester; +import com.diffplug.common.testing.SerializableTester; + +public class ForwardingEqualityTest { + static Str s(String key) { + return new Str(key); + } + + @SuppressWarnings("serial") + static class Str extends ForwardingEquality { + Str(String key) { + super(key); + } + } + + static Int i(int key) { + return new Int(key); + } + + @SuppressWarnings("serial") + static class Int extends ForwardingEquality { + Int(int key) { + super(key); + } + } + + @Test + public void testEquality() { + new EqualsTester() + .addEqualityGroup(s("hello"), s("hello"), s("h" + "ello")) + .addEqualityGroup(s("world"), s("world"), s("wor" + "ld")) + .addEqualityGroup(i(3), i(3), i(1 + 2)) + .addEqualityGroup(i(-6), i(-6), i(1 - 7)) + .testEquals(); + } + + @Test + public void testSerialization() { + SerializableTester.reserializeAndAssert(s("hello")); + SerializableTester.reserializeAndAssert(s("world")); + SerializableTester.reserializeAndAssert(i(4)); + SerializableTester.reserializeAndAssert(i(-6)); + } +}