Skip to content

Commit 7b6aea7

Browse files
committed
First cut at ForwardingEquality.
1 parent cec7db3 commit 7b6aea7

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ dependencies {
8282
compile "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
8383
testCompile "junit:junit:${VER_JUNIT}"
8484
testCompile "org.assertj:assertj-core:${VER_ASSERTJ}"
85+
testCompile "com.diffplug.durian:durian-testlib:${VER_DURIAN}"
8586

8687
// add the eclipse jars to the embedded configuration
8788
eclipseDeps.each { embeddedJars "p2:${it}:+" }
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2016 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.gradle.spotless;
17+
18+
import java.io.IOException;
19+
import java.io.ObjectInputStream;
20+
import java.io.ObjectOutputStream;
21+
import java.io.ObjectStreamException;
22+
import java.io.Serializable;
23+
import java.util.Objects;
24+
25+
/**
26+
* Implements equality, hashcode, and serialization entirely in terms
27+
* of the given key. It is appropriate for subclasses of this class
28+
* to use `@SuppressWarnings("serial")`.
29+
*/
30+
@SuppressWarnings("serial")
31+
public abstract class ForwardingEquality<T extends Serializable> implements Serializable {
32+
private T key;
33+
34+
ForwardingEquality(T key) {
35+
this.key = Objects.requireNonNull(key);
36+
}
37+
38+
protected T key() {
39+
return key;
40+
}
41+
42+
private void writeObject(ObjectOutputStream out) throws IOException {
43+
out.writeObject(key);
44+
}
45+
46+
@SuppressWarnings("unchecked")
47+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
48+
key = (T) Objects.requireNonNull(in.readObject());
49+
}
50+
51+
@SuppressWarnings("unused")
52+
private void readObjectNoData() throws ObjectStreamException {
53+
throw new UnsupportedOperationException();
54+
}
55+
56+
@Override
57+
public boolean equals(Object other) {
58+
if (other == null) {
59+
return false;
60+
} else if (getClass().equals(other.getClass())) {
61+
return key.equals(((ForwardingEquality<?>) other).key);
62+
} else {
63+
return false;
64+
}
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return key.hashCode();
70+
}
71+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2016 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.gradle.spotless;
17+
18+
import org.junit.Test;
19+
20+
import com.diffplug.common.testing.EqualsTester;
21+
import com.diffplug.common.testing.SerializableTester;
22+
23+
public class ForwardingEqualityTest {
24+
static Str s(String key) {
25+
return new Str(key);
26+
}
27+
28+
@SuppressWarnings("serial")
29+
static class Str extends ForwardingEquality<String> {
30+
Str(String key) {
31+
super(key);
32+
}
33+
}
34+
35+
static Int i(int key) {
36+
return new Int(key);
37+
}
38+
39+
@SuppressWarnings("serial")
40+
static class Int extends ForwardingEquality<Integer> {
41+
Int(int key) {
42+
super(key);
43+
}
44+
}
45+
46+
@Test
47+
public void testEquality() {
48+
new EqualsTester()
49+
.addEqualityGroup(s("hello"), s("hello"), s("h" + "ello"))
50+
.addEqualityGroup(s("world"), s("world"), s("wor" + "ld"))
51+
.addEqualityGroup(i(3), i(3), i(1 + 2))
52+
.addEqualityGroup(i(-6), i(-6), i(1 - 7))
53+
.testEquals();
54+
}
55+
56+
@Test
57+
public void testSerialization() {
58+
SerializableTester.reserializeAndAssert(s("hello"));
59+
SerializableTester.reserializeAndAssert(s("world"));
60+
SerializableTester.reserializeAndAssert(i(4));
61+
SerializableTester.reserializeAndAssert(i(-6));
62+
}
63+
}

0 commit comments

Comments
 (0)