Browse Source

feat: add FieldsCompare

许家凯 3 years ago
parent
commit
8e92101c86
1 changed files with 40 additions and 0 deletions
  1. 40 0
      src/main/java/com/winhc/bigdata/udf/string/FieldsCompare.java

+ 40 - 0
src/main/java/com/winhc/bigdata/udf/string/FieldsCompare.java

@@ -0,0 +1,40 @@
+package com.winhc.bigdata.udf.string;
+
+import com.aliyun.odps.udf.UDF;
+import com.aliyun.odps.utils.StringUtils;
+
+/**
+ * @author: XuJiakai
+ * 2021/9/8 11:13
+ */
+public class FieldsCompare extends UDF {
+    public Boolean evaluate(String val, String... other) {
+        if ((other.length + 1) % 2 != 0) {
+            throw new RuntimeException("fields length not equ");
+        }
+        int len = (other.length + 1) / 2;
+
+        Boolean flag = true;
+        for (int i = 1; i <= len; i++) {
+            if (i == 1) {
+                flag = equ(val, other[i + len - 2]);
+            } else {
+                flag = equ(other[i - 2], other[i + len - 2]);
+            }
+            if (!flag) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static Boolean equ(String val1, String val2) {
+        if (val1 == null && val2 == null) {
+            return true;
+        }
+        if (StringUtils.isNotBlank(val1) && StringUtils.isNotBlank(val2)) {
+            return val1.equals(val2);
+        }
+        return false;
+    }
+}