LangOps { public static <T> BigDecimal sumBigDecimal(List<T> datas, Function<T, BigDecimal> mapper){ if(datas==null) return BigDecimal.valueOf(0.0); return datas.stream().map(mapper).reduce((n1, n2)->{ return n1.add(n2); }) .orElse(BigDecimal.valueOf(0.0)); } private  LangOps(); static BinaryOperator<T> throwingMerger(); @SuppressWarnings("unchecked") static Map<K, V> arrayToMap(Object... arrays); @SuppressWarnings("unchecked") static Map<K, V> kvArrayToMap(K[] keys, V[] values); static Map<K, V> toMap(List<V> data, Function<V, K> keyExtractor); static Object[] toArray(Map<?, ?> map); @SuppressWarnings("unchecked") static R[] toArray(List<T> list, Function<? super T, ? extends R> mapper); static Map<Long, R> generateMap(Long count, Function<Long, ? extends R> mapper); static List<R> generateList(int count, Function<Integer, ? extends R> mapper); static List<R> generateList(Long count, Function<Long, ? extends R> mapper); static void splitTaskAndConsume(List<T> taskList, int taskSize, Consumer<List<T>> subTaskDataConsumer, boolean parallel); static void splitTaskAndConsume(List<T> taskList, int taskSize, BiConsumer<List<T>, Integer> subTaskDataConsumer); static void splitTaskAndConsume(List<T> taskList, int taskSize, BiConsumer<List<T>, Integer> subTaskDataConsumer, boolean parallel); static void timeIt(String tag, Integer times, Closure closure); static void ntimesRun(Integer times, Closure closure); static void ntimesRun(Integer times, Consumer<Integer> consumer); static void ntimesRun(String printTimeTag, Integer times, Closure closure); static void ntimesRun(String printTimeTag, Integer times, Consumer<Integer> consumer); static List<R> ntimesMap(Integer times, Function<Integer, R> mapper); static BigDecimal sumBigDecimal(List<T> datas, Function<T, BigDecimal> mapper); @SuppressWarnings("unchecked") static Map<K, List<T>> groupByProperty(List<T> datas, String propName); @SuppressWarnings("unchecked") static Map<K, List<V>> groupByProperty(List<T> datas, String keyProperty, String valueProperty); static Map<K, List<T>> groupBy(List<T> datas, Function<? super T, ? extends K> keyer); static Map<K, List<V>> groupBy(List<T> datas, Function<? super T, ? extends K> keyer, Function<? super T, ? extends V> valuer); static int parseSize(String size); static int parseSize(String size, Integer def); static long timeToSeconds(String time, long def); static long timeToMills(String time, long def); static long timeToMinutes(String time, long def); static long parseTime(String time, long def, BiFunction<Integer, TimeUnit, Long> convert); static Pair<Integer, TimeUnit> parseTimeUnit(String time); static Color parseColor(String color); @SafeVarargs static List<T> asList(T...values);  }
@Test public void testSum(){ BigDecimal total = LangOps.sumBigDecimal(null, BigDecimalBox::getValue); Assert.assertEquals(BigDecimal.valueOf(0.0), total); List<BigDecimalBox> datas = Lists.newArrayList(); total = LangOps.sumBigDecimal(datas, BigDecimalBox::getValue); Assert.assertEquals(BigDecimal.valueOf(0.0), total); datas.add(new BigDecimalBox(1.5)); datas.add(new BigDecimalBox(3.5)); datas.add(new BigDecimalBox(5.0)); datas.add(new BigDecimalBox(5.0)); total = LangOps.sumBigDecimal(datas, BigDecimalBox::getValue); Assert.assertEquals(BigDecimal.valueOf(15.0), total); }