Cousnouf commented on code in PR #1114: URL: https://github.com/apache/commons-lang/pull/1114#discussion_r1338563661
########## src/main/java/org/apache/commons/lang3/builder/TypedEqualsBuilder.java: ########## @@ -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) { Review Comment: If the function throws a checked exception it can use `FailableFunction`, yes. I'll catch the Exception class (Throwable are meant to not be caught) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
