diff --git a/src/main/java/org/hisp/dhis/model/analytics/AnalyticsIndicatorType.java b/src/main/java/org/hisp/dhis/model/analytics/AnalyticsIndicatorType.java index 7c005563..f896e6ff 100644 --- a/src/main/java/org/hisp/dhis/model/analytics/AnalyticsIndicatorType.java +++ b/src/main/java/org/hisp/dhis/model/analytics/AnalyticsIndicatorType.java @@ -29,6 +29,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.io.Serializable; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -38,6 +39,7 @@ @Setter @ToString @NoArgsConstructor +@AllArgsConstructor public class AnalyticsIndicatorType implements Serializable { @JsonProperty private String name; diff --git a/src/main/java/org/hisp/dhis/model/analytics/MetaDataItem.java b/src/main/java/org/hisp/dhis/model/analytics/MetaDataItem.java index acaeeaf8..f0cab912 100644 --- a/src/main/java/org/hisp/dhis/model/analytics/MetaDataItem.java +++ b/src/main/java/org/hisp/dhis/model/analytics/MetaDataItem.java @@ -27,6 +27,7 @@ */ package org.hisp.dhis.model.analytics; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import java.io.Serializable; import lombok.Getter; @@ -104,7 +105,18 @@ public MetaDataItem(String uid, String name, DimensionItemType dimensionItemType * @param dimensionItemType the {@link DimensionItemType}. * @return true if the dimension item is of the given type. */ + @JsonIgnore public boolean isDimensionItemType(DimensionItemType dimensionItemType) { return this.dimensionItemType == dimensionItemType; } + + /** + * Indicates whether an {@link AnalyticsIndicatorType} exists. + * + * @return true if an {@link AnalyticsIndicatorType} exists. + */ + @JsonIgnore + public boolean hasIndicatorType() { + return indicatorType != null; + } } diff --git a/src/main/java/org/hisp/dhis/util/NumberUtils.java b/src/main/java/org/hisp/dhis/util/NumberUtils.java index 255f921a..5764bae2 100644 --- a/src/main/java/org/hisp/dhis/util/NumberUtils.java +++ b/src/main/java/org/hisp/dhis/util/NumberUtils.java @@ -29,6 +29,9 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.commons.lang3.StringUtils; @@ -119,22 +122,35 @@ public static Double toDouble(String string) { /** * Formats the double value so that if the value has no decimals, or has a single zero decimal as - * in {@code .0}, the formatted string excludes the decimal. Otherwise uses two decimals for the - * formatted string. + * in {@code .0}, the formatted string excludes any decimal. Otherwise uses one decimal place for + * the formatted string. Uses {@code ,} (comma) as the thousands separator. * * @param value the double value to format. * @return the value formatted as a string. */ public static String formatDouble(Double value) { if (value == null) { - return ""; + return StringUtils.EMPTY; } + + // Define custom symbols to force comma as grouping separator and period as decimal separator + DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); + symbols.setGroupingSeparator(','); + + // Define format pattern, '#' is "digit, zero as absent", '0' is "digit, zero as 0" + String pattern = "#,##0.#"; + + // Create the DecimalFormat formatter + DecimalFormat formatter = new DecimalFormat(pattern, symbols); + + // Set max number of fraction digits, max 1 ensures only one decimal place + formatter.setMaximumFractionDigits(1); + + // Check if the value is a whole number (or .0), and force max 0 fraction digits if (value % 1 == 0) { - return String.format("%.0f", value); - } else if (value * 10 % 1 == 0) { - return String.format("%.1f", value); - } else { - return String.format("%.2f", value); + formatter.setMaximumFractionDigits(0); } + + return formatter.format(value); } } diff --git a/src/test/java/org/hisp/dhis/util/NumberUtilsTest.java b/src/test/java/org/hisp/dhis/util/NumberUtilsTest.java index 02dc6a7c..125edf97 100644 --- a/src/test/java/org/hisp/dhis/util/NumberUtilsTest.java +++ b/src/test/java/org/hisp/dhis/util/NumberUtilsTest.java @@ -99,12 +99,14 @@ void testIsInteger() { @Test void testFormatDouble() { - assertEquals("3851", NumberUtils.formatDouble(3851D)); - assertEquals("1472", NumberUtils.formatDouble(1472.0)); - assertEquals("3851.2", NumberUtils.formatDouble(3851.2)); - assertEquals("1472.5", NumberUtils.formatDouble(1472.50)); - assertEquals("5249.39", NumberUtils.formatDouble(5249.387)); - assertEquals("54.25", NumberUtils.formatDouble(54.2485)); + assertEquals("351", NumberUtils.formatDouble(351D)); + assertEquals("172", NumberUtils.formatDouble(172.0)); + assertEquals("431.5", NumberUtils.formatDouble(431.5)); + assertEquals("3,851.2", NumberUtils.formatDouble(3851.2)); + assertEquals("1,472.5", NumberUtils.formatDouble(1472.50)); + assertEquals("5,249.4", NumberUtils.formatDouble(5249.387)); + assertEquals("1,874,642.6", NumberUtils.formatDouble(1874642.591)); + assertEquals("54.3", NumberUtils.formatDouble(54.2685)); assertEquals("", NumberUtils.formatDouble(null)); } }