Skip to content

Commit f78e292

Browse files
committed
Fix sonar warnings.
1 parent 6a9d052 commit f78e292

File tree

2 files changed

+18
-1
lines changed
  • src
    • main/java/com/github/underscore/lodash
    • test/java/com/github/underscore/lodash

2 files changed

+18
-1
lines changed

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,11 +955,18 @@ public static <T extends Number> Double average(final Iterable<T> iterable) {
955955

956956
public static <E, F extends Number> Double average(final Iterable<E> iterable, final Function<E, F> func) {
957957
F sum = sum(iterable, func);
958+
if (sum == null) {
959+
return null;
960+
}
958961
return sum.doubleValue() / size(iterable);
959962
}
960963

961964
public static <N extends Number> Double average(N[] array) {
962-
return sum(array).doubleValue() / array.length;
965+
N sum = sum(array);
966+
if (sum == null) {
967+
return null;
968+
}
969+
return sum.doubleValue() / array.length;
963970
}
964971

965972
public static Double average(java.math.BigDecimal first, java.math.BigDecimal second) {

src/test/java/com/github/underscore/lodash/MathTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public Byte apply(final Byte item) {
5050
}
5151
});
5252
assertEquals("4.0", resultFunc.toString());
53+
final Double resultFunc2 = U.average(asList((Byte) null), new Function<Byte, Byte>() {
54+
public Byte apply(final Byte item) {
55+
return item;
56+
}
57+
});
58+
assertNull(resultFunc2);
5359
assertEquals("2.0", result.toString());
5460
final Double result2 = U.average(asList((double) 1, (double) 2, (double) 3));
5561
assertEquals("2.0", result2.toString());
@@ -133,6 +139,10 @@ public Byte apply(final Byte item) {
133139
assertNull(result40);
134140
final Double result41 = U.average(Long.valueOf(2), Long.valueOf(4));
135141
assertEquals("3.0", result41.toString());
142+
final Double result42 = U.average(Long.valueOf(2), Long.valueOf(4));
143+
assertEquals("3.0", result41.toString());
144+
final Double result43 = U.average(new Integer[] {(Integer) null});
145+
assertNull(result43);
136146
}
137147

138148
/*

0 commit comments

Comments
 (0)