Skip to content

Commit

Permalink
First cut at ForwardingEquality.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Nov 1, 2016
1 parent cec7db3 commit 7b6aea7
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}:+" }
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/diffplug/gradle/spotless/ForwardingEquality.java
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.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<T extends Serializable> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String> {
Str(String key) {
super(key);
}
}

static Int i(int key) {
return new Int(key);
}

@SuppressWarnings("serial")
static class Int extends ForwardingEquality<Integer> {
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));
}
}

0 comments on commit 7b6aea7

Please sign in to comment.