This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new aee3a52ae85 [Fix](fe-common) Fix the Pair.java code about the hidden 
danger of NullPointException (#31371)
aee3a52ae85 is described below

commit aee3a52ae854fb04e76f676cb8103cdb8ea8eacd
Author: ZhongJinHacker <jiangzhong...@hotmail.com>
AuthorDate: Mon Feb 26 08:30:46 2024 +0800

    [Fix](fe-common) Fix the Pair.java code about the hidden danger of 
NullPointException (#31371)
    
    * 修复Pair类 first 或 second 为null时,调用equals和toString 抛NullPointException问题
    
    * add license
---
 .../main/java/org/apache/doris/common/Pair.java    | 14 ++++--
 .../java/org/apache/doris/common/PairTest.java     | 55 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/Pair.java 
b/fe/fe-common/src/main/java/org/apache/doris/common/Pair.java
index 7d1736a0754..a6992846761 100644
--- a/fe/fe-common/src/main/java/org/apache/doris/common/Pair.java
+++ b/fe/fe-common/src/main/java/org/apache/doris/common/Pair.java
@@ -60,10 +60,16 @@ public class Pair<F, S> {
      */
     @Override
     public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+
         if (o instanceof Pair) {
             Pair<F, S> other = (Pair<F, S>) o;
-            return first.equals(other.first)
-                    && second.equals(other.second);
+
+            boolean firstEqual = Objects.isNull(first) ? null == other.first : 
first.equals(other.first);
+            boolean secondEqual = Objects.isNull(second) ? null == 
other.second : second.equals(other.second);
+            return firstEqual && secondEqual;
         }
         return false;
     }
@@ -75,7 +81,9 @@ public class Pair<F, S> {
 
     @Override
     public String toString() {
-        return first.toString() + ":" + second.toString();
+        String firstStr = Objects.nonNull(first) ? first.toString() : "";
+        String secondStr = Objects.nonNull(second) ? second.toString() : "";
+        return firstStr + ":" + secondStr;
     }
 
     public static class PairComparator<T extends Pair<?, ? extends 
Comparable>> implements Comparator<T> {
diff --git a/fe/fe-common/src/test/java/org/apache/doris/common/PairTest.java 
b/fe/fe-common/src/test/java/org/apache/doris/common/PairTest.java
new file mode 100644
index 00000000000..11daa2075de
--- /dev/null
+++ b/fe/fe-common/src/test/java/org/apache/doris/common/PairTest.java
@@ -0,0 +1,55 @@
+// 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.doris.common;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PairTest {
+
+    @Test
+    public void testToString() {
+        Pair<String, Object> pairFirstNull = Pair.of(null, "world");
+        Assert.assertEquals(":world", pairFirstNull.toString());
+
+        Pair<String, Object> pairSecondNull = Pair.of("hello", null);
+        Assert.assertEquals("hello:", pairSecondNull.toString());
+    }
+
+    @Test
+    public void testEquals() {
+        Pair<String, Object> firstPair = Pair.of(null, "world");
+        Pair<String, Object> secondPair = null;
+
+        Assert.assertTrue(firstPair.equals(firstPair));
+        Assert.assertFalse(firstPair.equals(secondPair));
+
+        secondPair = Pair.of(null, "world");
+        Assert.assertTrue(firstPair.equals(secondPair));
+
+        secondPair = Pair.of("hello", null);
+        Assert.assertFalse(firstPair.equals(secondPair));
+
+        firstPair = Pair.of("hello", "world");
+        secondPair = Pair.of("hello", "world");
+        Assert.assertTrue(firstPair.equals(secondPair));
+
+        secondPair = Pair.of("world", "hello");
+        Assert.assertFalse(firstPair.equals(secondPair));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to