You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/06/14 07:05:31 UTC

[GitHub] [iotdb] Plutooooooo commented on a diff in pull request #6268: [IOTDB-3299] Migrate udf api to a seperate module

Plutooooooo commented on code in PR #6268:
URL: https://github.com/apache/iotdb/pull/6268#discussion_r896449358


##########
udf-api/src/main/java/org/apache/iotdb/udf/api/type/Binary.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.iotdb.udf.api.type;
+
+import java.io.Serializable;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+
+/**
+ * Override compareTo() and equals() function to Binary class. This class is used to accept Java
+ * String type
+ */
+public class Binary implements Comparable<Binary>, Serializable {
+
+  private static final long serialVersionUID = 1250049718612917815L;
+  public static final String STRING_ENCODING = "UTF-8";
+  public static final Charset STRING_CHARSET = Charset.forName(STRING_ENCODING);
+
+  private byte[] values;
+
+  /** if the bytes v is modified, the modification is visible to this binary. */
+  public Binary(byte[] v) {
+    this.values = v;
+  }
+
+  public Binary(String s) {
+    this.values = (s == null) ? null : s.getBytes(STRING_CHARSET);
+  }
+
+  public static Binary valueOf(String value) {
+    return new Binary(stringToBytes(value));
+  }
+
+  @Override
+  public int compareTo(Binary other) {
+    if (other == null) {
+      if (this.values == null) {
+        return 0;
+      } else {
+        return 1;
+      }
+    }
+
+    int i = 0;
+    while (i < getLength() && i < other.getLength()) {
+      if (this.values[i] == other.values[i]) {
+        i++;
+        continue;
+      }
+      return this.values[i] - other.values[i];
+    }
+    return getLength() - other.getLength();
+  }
+
+  @Override
+  public boolean equals(Object other) {
+    if (this == other) {
+      return true;
+    }
+    if (other == null) {
+      return false;
+    }
+    if (getClass() != other.getClass()) {
+      return false;
+    }
+
+    return compareTo((Binary) other) == 0;
+  }
+
+  @Override
+  public int hashCode() {
+    return Arrays.hashCode(values);
+  }
+
+  /**
+   * get length.
+   *
+   * @return length
+   */
+  public int getLength() {
+    if (this.values == null) {
+      return -1;

Review Comment:
   In tsfile.util.Binary, this method returns -1 when values is null. But I think it's better to return 0, too.



-- 
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: reviews-unsubscribe@iotdb.apache.org

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