Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalise GroupItem member calculations to use the GroupItem's Unit #4563

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.ArrayList;
import java.util.List;

import javax.measure.Quantity;
import javax.measure.Unit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -48,13 +48,12 @@ public class GroupFunctionHelper {
* arithmetic group function will take unit conversion into account.
*
* @param function the {@link GroupFunctionDTO} describing the group function.
* @param baseItem an optional {@link Item} defining the dimension for unit conversion.
* @param baseItem an optional {@link Item} defining the dimension/unit for unit conversion.
* @return a {@link GroupFunction} according to the given parameters.
*/
public GroupFunction createGroupFunction(GroupFunctionDTO function, @Nullable Item baseItem) {
Class<? extends Quantity<?>> dimension = getDimension(baseItem);
if (dimension != null) {
return createDimensionGroupFunction(function, baseItem, dimension);
if (baseItem instanceof NumberItem baseNumberItem && baseNumberItem.getDimension() != null) {
return createDimensionGroupFunction(function, baseNumberItem);
}
return createDefaultGroupFunction(function, baseItem);
}
Expand All @@ -78,30 +77,26 @@ private List<State> parseStates(@Nullable Item baseItem, String @Nullable [] par
return states;
}

private @Nullable Class<? extends Quantity<?>> getDimension(@Nullable Item baseItem) {
if (baseItem instanceof NumberItem numberItem) {
return numberItem.getDimension();
}
return null;
}

private GroupFunction createDimensionGroupFunction(GroupFunctionDTO function, @Nullable Item baseItem,
Class<? extends Quantity<?>> dimension) {
private GroupFunction createDimensionGroupFunction(GroupFunctionDTO function, NumberItem baseNumberItem) {
final String functionName = function.name;
switch (functionName.toUpperCase()) {
case "AVG":
return new QuantityTypeArithmeticGroupFunction.Avg(dimension);
case "MEDIAN":
return new QuantityTypeArithmeticGroupFunction.Median(dimension, baseItem);
case "SUM":
return new QuantityTypeArithmeticGroupFunction.Sum(dimension);
case "MIN":
return new QuantityTypeArithmeticGroupFunction.Min(dimension);
case "MAX":
return new QuantityTypeArithmeticGroupFunction.Max(dimension);
default:
return createDefaultGroupFunction(function, baseItem);
Unit<?> targetUnit = baseNumberItem.getUnit();
if (targetUnit != null) {
targetUnit = targetUnit.getSystemUnit(); // use system unit so 'Sum' becomes zero based (absolute)
switch (functionName.toUpperCase()) {
case "AVG":
return new QuantityTypeArithmeticGroupFunction.Avg(targetUnit);
case "MEDIAN":
return new QuantityTypeArithmeticGroupFunction.Median(targetUnit);
case "SUM":
return new QuantityTypeArithmeticGroupFunction.Sum(targetUnit);
case "MIN":
return new QuantityTypeArithmeticGroupFunction.Min(targetUnit);
case "MAX":
return new QuantityTypeArithmeticGroupFunction.Max(targetUnit);
default:
}
}
return createDefaultGroupFunction(function, baseNumberItem);
}

private GroupFunction createDefaultGroupFunction(GroupFunctionDTO function, @Nullable Item baseItem) {
Expand Down
Loading
Loading