-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypedEqualsBuilder class and test
- Loading branch information
Marc Cappelletti
committed
Sep 15, 2023
1 parent
14be5e5
commit daa8c6e
Showing
3 changed files
with
196 additions
and
21 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
77 changes: 77 additions & 0 deletions
77
src/main/java/org/apache/commons/lang3/builder/TypedEqualsBuilder.java
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,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.commons.lang3.builder; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* An extension of {@link EqualsBuilder} aimed to perform additionally the base objects and class equals checks. | ||
* | ||
* <p>It then offers the possibility to append field extractors.</p> | ||
* | ||
* <p>Typical use for the code is as follows:</p> | ||
* <pre> | ||
* public boolean equals(Object obj) { | ||
* return new TypedEqualsBuilder<>(this) | ||
* .appendBaseObject(obj) | ||
* .append(TestObject::getA) | ||
* .append(TestObject::getB) | ||
* .isEquals(); | ||
* } | ||
* </pre> | ||
* | ||
* @param <T> the type of the compared object. | ||
* | ||
* @since 3.14.0 | ||
*/ | ||
public class TypedEqualsBuilder<T> extends EqualsBuilder { | ||
|
||
private final T currentInstance; | ||
|
||
private boolean sameReference = false; | ||
private T other; | ||
|
||
@SuppressWarnings("unchecked") | ||
public TypedEqualsBuilder(T currentInstance, Object other) { | ||
Objects.requireNonNull(currentInstance); | ||
this.currentInstance = currentInstance; | ||
if (currentInstance == other) { | ||
sameReference = true; | ||
return; | ||
} | ||
Class<T> currentInstanceClass = (Class<T>) currentInstance.getClass(); | ||
if (other == null || currentInstanceClass != other.getClass()) { | ||
isEquals = false; | ||
return; | ||
} | ||
this.other = (T) other; | ||
} | ||
|
||
@Override | ||
boolean shouldLeaveEarly() { | ||
return sameReference || super.shouldLeaveEarly(); | ||
} | ||
|
||
public TypedEqualsBuilder<T> append(Function<T, ?> extractor) { | ||
if (shouldLeaveEarly()) { | ||
return this; | ||
} | ||
super.append(extractor.apply(currentInstance), extractor.apply(other)); | ||
return this; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/org/apache/commons/lang3/builder/TypedEqualsBuilderTest.java
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,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.commons.lang3.builder; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class TypedEqualsBuilderTest { | ||
|
||
@Test | ||
void test_complete_equals() { | ||
TestObject testObject1 = new TestObject(1, 2); | ||
TestObject testObject2 = new TestObject(1, 3); | ||
TestObject testObject3 = new TestObject(1, 2); | ||
|
||
assertEquals(testObject1, testObject1); | ||
assertNotEquals(testObject1, testObject2); | ||
assertNotEquals(testObject2, testObject1); | ||
|
||
assertEquals(Boolean.TRUE, | ||
new TypedEqualsBuilder<>(testObject1, testObject2) | ||
.append(TestObject::getA) | ||
.isEquals()); | ||
assertEquals(Boolean.TRUE, | ||
new TypedEqualsBuilder<>(testObject1, testObject3) | ||
.append(TestObject::getA) | ||
.append(TestObject::getB) | ||
.isEquals()); | ||
assertEquals(Boolean.FALSE, | ||
new TypedEqualsBuilder<>(testObject1, testObject2) | ||
.append(TestObject::getA) | ||
.append(TestObject::getB) | ||
.isEquals()); | ||
assertEquals(Boolean.FALSE, | ||
new TypedEqualsBuilder<>(testObject2, testObject1) | ||
.append(TestObject::getA) | ||
.append(TestObject::getB) | ||
.isEquals()); | ||
assertEquals(Boolean.FALSE, | ||
new TypedEqualsBuilder<>(testObject1, null) | ||
.append(TestObject::getA) | ||
.isEquals()); | ||
assertEquals(Boolean.FALSE, | ||
new TypedEqualsBuilder<>(testObject1, "") | ||
.append(TestObject::getA) | ||
.isEquals()); | ||
} | ||
|
||
static class TestObject { | ||
int a; | ||
int b; | ||
|
||
public TestObject(int a, int b) { | ||
this.a = a; | ||
this.b = b; | ||
} | ||
|
||
public int getA() { | ||
return a; | ||
} | ||
|
||
public int getB() { | ||
return b; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return new TypedEqualsBuilder<>(this, obj) | ||
.append(TestObject::getA) | ||
.append(TestObject::getB) | ||
.isEquals(); | ||
} | ||
} | ||
} |