markap14 commented on a change in pull request #3351: NIFI-2933 Remote 
input/output ports at any PG
URL: https://github.com/apache/nifi/pull/3351#discussion_r283448315
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/util/NumberUtil.java
 ##########
 @@ -0,0 +1,38 @@
+/*
+ * 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.nifi.web.api.dto.util;
+
+/**
+ * Utility class for numbers.
+ */
+public class NumberUtil {
+
+    /**
+     * Calculate sum of Integers those can be null.
+     * This method can be used to avoid getting NullPointerException when a 
null Integer being auto-boxed into an int.
+     * @param values Integers to add
+     * @return the sum of given values or null if the sum is 0
+     */
+    public static Integer sumNullableIntegers(Integer ... values) {
+        int y = 0;
+        for (Integer value : values) {
+            y += value == null ? 0 : value;
+        }
+        return y == 0 ? null : y;
 
 Review comment:
   @ijokarumawak this seems odd to me. If I call 
`NumberUtil.sumNullableIntegers(0, 0, 0, 0, 0);` or even 
`NumberUtil.sumNullableIntegers(8, -1, -5, -3, 1)` then I will get back `null` 
- but my expectation as a caller would generally be to expect `0`. It would 
make sense to me to return `null` if all values were `null` but if I pass in at 
least one non-null value, I would expect a non-null result. Perhaps something 
like:
   ```
   public static Integer sumNullableIntegers(Integer... values) {
     int sum = 0;
     int count = 0;
     for (Integer value : values) {
       if (value == null) {
           continue;
       }
       sum += value;
       count++;
     }
   
     return count == 0 ? null : sum;
   }
   ```
   
   Note that I didn't actually test this code out, just typed it up quickly 
inline :)

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to