forked from apache/incubator-kie-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KOGITO-8447:[Dashbuilder]Support MEDIAN aggregation function (apache#…
…1406) * KOGITO-8447:[Dashbuilder]Support MEDIAN aggregation function * Fixing copyright header
- Loading branch information
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...-dataset-shared/src/main/java/org/dashbuilder/dataset/engine/function/MedianFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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.dashbuilder.dataset.engine.function; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.dashbuilder.dataset.group.AggregateFunctionType; | ||
|
||
/** | ||
* It calculates the average value of a set of numbers. | ||
*/ | ||
public class MedianFunction extends AbstractFunction { | ||
|
||
public MedianFunction() { | ||
super(); | ||
} | ||
|
||
public AggregateFunctionType getType() { | ||
return AggregateFunctionType.MEDIAN; | ||
} | ||
|
||
public Object aggregate(List values) { | ||
if (values == null || values.isEmpty()) { | ||
return 0d; | ||
} | ||
|
||
var n = values.size(); | ||
|
||
if (n == 1) { | ||
return ((Number) values.get(0)).doubleValue(); | ||
} | ||
|
||
var sortedValues = values.stream().mapToDouble(v -> ((Number) v).doubleValue()).sorted().toArray(); | ||
|
||
if (n % 2 == 1) { | ||
return sortedValues[n / 2]; | ||
} | ||
var middle = n / 2; | ||
var ii = n == 2 ? 0 : middle - 1; | ||
var is = n == 2 ? 1 : middle + 1; | ||
var v = (sortedValues[ii] + sortedValues[is]) / 2d; | ||
return round(v, precission); | ||
} | ||
|
||
public Object aggregate(List values, List<Integer> rows) { | ||
if (rows == null) { | ||
return aggregate(values); | ||
} | ||
if (rows.isEmpty()) { | ||
return 0d; | ||
} | ||
if (values == null || values.isEmpty()) { | ||
return 0d; | ||
} | ||
var _values = rows.stream() | ||
.map(values::get) | ||
.collect(Collectors.toList()); | ||
return this.aggregate(_values); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters