bziobrowski commented on code in PR #14337:
URL: https://github.com/apache/pinot/pull/14337#discussion_r1837700145


##########
pinot-core/src/main/java/org/apache/pinot/core/util/NumberUtils.java:
##########
@@ -0,0 +1,224 @@
+/**
+ * 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.pinot.core.util;
+
+/**
+ * Utility class with various number related methods.
+ */
+public class NumberUtils {
+
+  public static final NumberFormatException NULL_EXCEPTION = new 
NumberFormatException("Can't parse null string");
+  public static final NumberFormatException EXP_EXCEPTION = new 
NumberFormatException("Wrong exponent");
+  public static final NumberFormatException EXCEPTION = new 
NumberFormatException("Can't parse value");
+
+  private static final long[] POWERS_OF_10 = new long[]{
+      1L,
+      10L,
+      100L,
+      1000L,
+      10000L,
+      100000L,
+      1000000L,
+      10000000L,
+      100000000L,
+      1000000000L,
+      10000000000L,
+      100000000000L,
+      1000000000000L,
+      10000000000000L,
+      100000000000000L,
+      1000000000000000L,
+      10000000000000000L,
+      100000000000000000L,
+      1000000000000000000L,
+  };
+
+  private NumberUtils() {
+  }
+
+  /**
+   * Parses whole input char sequence.
+   * Throws static, pre-allocated NumberFormatException.
+   * If proper stack trace is required, caller has to catch it and throw 
another exception.
+   * @param cs char sequence to parse
+   * @return parsed long value
+   */
+  public static long parseLong(CharSequence cs) {
+    if (cs == null) {
+      throw NULL_EXCEPTION;
+    }
+    return parseLong(cs, 0, cs.length());
+  }
+
+  /**
+   * Parses input char sequence between given indices.
+   * Throws static, pre-allocated NumberFormatException.
+   * If proper stack trace is required, caller has to catch it and throw 
another exception.
+   * @param cs char sequence to parse
+   * @param start start index (inclusive)
+   * @param end end index (exclusive)
+   * @return parsed long value
+   */
+  public static long parseLong(CharSequence cs, int start, int end) {
+    if (cs == null) {
+      throw NULL_EXCEPTION;
+    }

Review Comment:
   The default exception thrown by Long.parseLong() doesn't have useful context 
information, only the value. A regular user won't follow the stack trace to 
understand what went wrong so the really useful context (e.g. name of function 
parameter that is being parsed) must be provided by the call site (making the 
original exception's message and stack trace unnecessary) . In addition to that 
the approach makes it possible to ignore the exception (e.g. when it's ok to 
evaluate it to null) without having to pay for exception instantiation. For 
other cases  I'd recommend `Long.parseLong()`.
    



-- 
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: commits-unsubscr...@pinot.apache.org

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


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

Reply via email to