Cousnouf commented on code in PR #1114:
URL: https://github.com/apache/commons-lang/pull/1114#discussion_r1338546647


##########
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:
   It's better not to use the FailableFunction as it changes the signature of 
the method, hence forces you to surround with try/catch the build in your 
equals method.
   
![image](https://github.com/apache/commons-lang/assets/2562315/d0846d67-3a5b-433f-bfde-d8b4a17362f7)
   



-- 
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: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to