Skip to content

Commit

Permalink
Morphed ForwardingEquality into LazyForwardingEquality.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Nov 1, 2016
1 parent 7b6aea7 commit 6113458
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,42 @@
import java.io.Serializable;
import java.util.Objects;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Implements equality, hashcode, and serialization entirely in terms
* of the given key. It is appropriate for subclasses of this class
* to use `@SuppressWarnings("serial")`.
* of a lazily-computed key.
*/
@SuppressWarnings("serial")
public abstract class ForwardingEquality<T extends Serializable> implements Serializable {
private T key;
public abstract class LazyForwardingEquality<T extends Serializable> implements Serializable {
/** Null indicates that the key has not yet been set. */
@Nullable
private transient volatile T key;

ForwardingEquality(T key) {
this.key = Objects.requireNonNull(key);
}
/**
* This function is guaranteed to be called at most once.
* If the key is never required, then it will never be called at all.
*/
@Nonnull
protected abstract T calculateKey();

protected T key() {
/** Returns the underlying key, possibly triggering a call to {{@link #calculateKey()}. */
@Nonnull
protected final T key() {
// double-checked locking for lazy evaluation of calculateKey
if (key == null) {
synchronized (this) {
if (key == null) {
key = calculateKey();
}
}
}
return key;
}

private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(key);
out.writeObject(key());
}

@SuppressWarnings("unchecked")
Expand All @@ -54,18 +71,18 @@ private void readObjectNoData() throws ObjectStreamException {
}

@Override
public boolean equals(Object other) {
public final boolean equals(Object other) {
if (other == null) {
return false;
} else if (getClass().equals(other.getClass())) {
return key.equals(((ForwardingEquality<?>) other).key);
return key().equals(((LazyForwardingEquality<?>) other).key());
} else {
return false;
}
}

@Override
public int hashCode() {
return key.hashCode();
public final int hashCode() {
return key().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,48 @@
*/
package com.diffplug.gradle.spotless;

import static com.diffplug.common.testing.SerializableTester.*;

import org.junit.Test;

import com.diffplug.common.testing.EqualsTester;
import com.diffplug.common.testing.SerializableTester;

public class ForwardingEqualityTest {
@SuppressWarnings("serial")
public class LazyForwardingEqualityTest {
static Str s(String key) {
return new Str(key);
}

@SuppressWarnings("serial")
static class Str extends ForwardingEquality<String> {
static Other o(String key) {
return new Other(key);
}

static class Str extends LazyForwardingEquality<String> {
private String key;

Str(String key) {
super(key);
this.key = key;
}
}

static Int i(int key) {
return new Int(key);
@Override
protected String calculateKey() {
return key;
}
}

@SuppressWarnings("serial")
static class Int extends ForwardingEquality<Integer> {
Int(int key) {
static class Other extends Str {
Other(String 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))
.addEqualityGroup(s("hello"), reserializeAndAssert(s("hello")))
.addEqualityGroup(s("world"), reserializeAndAssert(s("world")))
.addEqualityGroup(o("hello"), reserializeAndAssert(o("hello")))
.addEqualityGroup(o("world"), reserializeAndAssert(o("world")))
.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 6113458

Please sign in to comment.