This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git commit 052b8db713f41679a76955c6cc2d5cc47cb81d8e Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Apr 14 08:37:53 2016 +0200 Revue de l'API des WeightUnits pour ne plus utiliser Weights (trop d'incertitude sur l'unité utilisée) --- .../java/fr/ifremer/tutti/type/WeightUnit.java | 77 ++++++++++++- .../main/java/fr/ifremer/tutti/util/Weights.java | 127 +++++---------------- 2 files changed, 101 insertions(+), 103 deletions(-) diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/type/WeightUnit.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/type/WeightUnit.java index 9849fdf..39cd373 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/type/WeightUnit.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/type/WeightUnit.java @@ -25,6 +25,8 @@ package fr.ifremer.tutti.type; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.util.Comparator; +import java.util.Objects; import static org.nuiton.i18n.I18n.n; import static org.nuiton.i18n.I18n.t; @@ -37,9 +39,9 @@ import static org.nuiton.i18n.I18n.t; * @author Tony Chemit - chemit@codelutin.com * @since 3.2 */ -public enum WeightUnit { +public enum WeightUnit implements Comparator<Float> { - G(n("application.common.unit.g"), n("application.common.unit.short.g"), 1, "\\d{0,8}(\\.\\d{0,1})?") { + G(n("application.common.unit.g"), n("application.common.unit.short.g"), 1, "\\d{0,8}(\\.\\d{0,1})?", 0.01f) { @Override public Float fromEntity(Float weight) { return weight == null ? null : weight * 1000; @@ -51,7 +53,7 @@ public enum WeightUnit { } }, - KG(n("application.common.unit.kg"), n("application.common.unit.short.kg"), 4, "\\d{0,6}(\\.\\d{0,4})?") { + KG(n("application.common.unit.kg"), n("application.common.unit.short.kg"), 4, "\\d{0,6}(\\.\\d{0,4})?", 0.00001f) { @Override public Float fromEntity(Float weight) { return weight; @@ -72,13 +74,16 @@ public enum WeightUnit { private final String numberEditorPattern; + private final float rawPrecision; + private final DecimalFormat decimalFormat; - WeightUnit(String i18nKey, String i18nShortKey, int numberDigits, String numberEditorPattern) { + WeightUnit(String i18nKey, String i18nShortKey, int numberDigits, String numberEditorPattern, float rawPrecision) { this.i18nKey = i18nKey; this.i18nShortKey = i18nShortKey; this.numberDigits = numberDigits; this.numberEditorPattern = numberEditorPattern; + this.rawPrecision = rawPrecision; DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); symbols.setGroupingSeparator(' '); @@ -89,6 +94,28 @@ public enum WeightUnit { this.decimalFormat.setMaximumFractionDigits(numberDigits); } + @Override + public int compare(Float v0, Float v1) { + + Objects.requireNonNull(v0, "can not compare null weights"); + Objects.requireNonNull(v1, "can not compare null weights"); + + float delta = round(v0) - round(v1); + int result; + if (delta > rawPrecision) { + // v0 > v1 + result = 1; + } else if (delta < -rawPrecision) { + // v0 < v1 + result = -1; + } else { + // v0 == v1 + result = 0; + } + return result; + + } + public String getLabel() { return t(i18nKey); } @@ -139,7 +166,7 @@ public enum WeightUnit { } else { BigDecimal sumB = new BigDecimal(String.valueOf(weight)) - .setScale(getNumberDigits()+1, BigDecimal.ROUND_HALF_UP) + .setScale(numberDigits, BigDecimal.ROUND_HALF_UP) .abs(); result = sumB.floatValue(); } @@ -150,8 +177,48 @@ public enum WeightUnit { return String.format("%s (%s)", label, getShortLabel()); } + public String renderFromEntityWithShortLabel(float o) { + return round(fromEntity(o)) + getShortLabel(); + } + public String decorateTip(String tip) { String unit = t("application.common.unit"); return String.format("%s (%s %s)", tip, unit, getLabel()); } + + public boolean isNullOrZero(Float o) { + return o == null || isZero(o); + } + + public boolean isNotNullNorZero(Float o) { + return o != null && !isZero(o); + } + + public boolean isEquals(float o1, float o2) { + return compare(o1, o2) == 0; + } + + public boolean isZero(float o) { + return isEquals(o, 0f); + } + + public boolean isNotEquals(float o1, float o2) { + return compare(o1, o2) != 0; + } + + public boolean isSmallerThan(float o1, float o2) { + return compare(o1, o2) < 0; + } + + public boolean isGreaterThan(float o1, float o2) { + return compare(o1, o2) > 0; + } + + public boolean isGreaterThanZero(float o) { + return isGreaterThan(o, 0f); + } + + public boolean isSmallerThanZero(float o) { + return isSmallerThan(o, 0f); + } } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/util/Weights.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/util/Weights.java index 5ec1a8d..95c3675 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/util/Weights.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/util/Weights.java @@ -27,8 +27,6 @@ package fr.ifremer.tutti.util; import fr.ifremer.tutti.persistence.entities.protocol.Rtp; import fr.ifremer.tutti.type.WeightUnit; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; import java.util.Objects; /** @@ -39,102 +37,35 @@ import java.util.Objects; */ public class Weights { - private static DecimalFormatSymbols symbols; - - private static DecimalFormat decimalFormat; - - public static DecimalFormatSymbols getDecimalFormatSymbols() { - if (symbols == null) { - symbols = new DecimalFormatSymbols(); - symbols.setDecimalSeparator('.'); - symbols.setGroupingSeparator(' '); - } - return symbols; - } - - public static DecimalFormat getDecimalFormat(int minDecimal, int maxDecimal) { - if (decimalFormat == null) { - decimalFormat = new DecimalFormat(); - decimalFormat.setDecimalFormatSymbols(getDecimalFormatSymbols()); - decimalFormat.setGroupingUsed(false); - } - decimalFormat.setMinimumFractionDigits(minDecimal); - decimalFormat.setMaximumFractionDigits(maxDecimal); - return decimalFormat; - } - - public static String getWeightStringValue(Float weight) { - String textValue; - if (weight != null) { - DecimalFormat weightDecimalFormat = getDecimalFormat(1, 3); - textValue = weightDecimalFormat.format(weight); - - } else { - textValue = ""; - } - return textValue; - } - - /** - * Compare two weights with rounding them to kilograms. - * - * @param v0 first weight to compare - * @param v1 second weight to compare - * @return 1 if v0 > v1, -1 if v0 < v1, 0 if v0 == v1 - */ - public static int compareWeights(float v0, float v1) { - v0 = WeightUnit.KG.round(v0); - v1 = WeightUnit.KG.round(v1); - return compareRawWeights(v0, v1); - } - - public static int compareRawWeights(float v0, float v1) { - float delta = v0 - v1; - int result; - if (delta > 0.00001f) { - // v0 > v1 - result = 1; - } else if (delta < -0.00001f) { - // v0 < v1 - result = -1; - } else { - // v0 == v1 - result = 0; - } - return result; - } - - public static boolean isSmallerWeight(float v0, float v1) { - return compareWeights(v0, v1) < 0; - } - - public static boolean isGreaterWeight(float v0, float v1) { - return compareWeights(v0, v1) > 0; - } - - public static boolean isPositive(float weight) { - return compareRawWeights(weight, 0.0f) >= 0; - } - - public static boolean isEqualWeight(float v0, float v1) { - return compareWeights(v0, v1) == 0; - } - - public static boolean isNotEqualWeight(float v0, float v1) { - return compareWeights(v0, v1) != 0; - } - - public static boolean isNullOrZero(Float weight) { - return weight == null || compareRawWeights(weight, 0.0f) == 0; - } - - public static boolean isNotZero(float weight) { - return compareRawWeights(weight, 0.0f) != 0; - } - - public static boolean isNotNullNorZero(Float number) { - return number != null && Weights.isNotZero(number); - } +// private static DecimalFormat decimalFormat; + +// public static DecimalFormat getDecimalFormat(int minDecimal, int maxDecimal) { +// if (decimalFormat == null) { +// decimalFormat = new DecimalFormat(); +// +// DecimalFormatSymbols symbols = new DecimalFormatSymbols(); +// symbols.setDecimalSeparator('.'); +// symbols.setGroupingSeparator(' '); +// +// decimalFormat.setDecimalFormatSymbols(symbols); +// decimalFormat.setGroupingUsed(false); +// } +// decimalFormat.setMinimumFractionDigits(minDecimal); +// decimalFormat.setMaximumFractionDigits(maxDecimal); +// return decimalFormat; +// } + +// public static String getWeightStringValue(Float weight) { +// String textValue; +// if (weight != null) { +// DecimalFormat weightDecimalFormat = getDecimalFormat(1, 3); +// textValue = weightDecimalFormat.format(weight); +// +// } else { +// textValue = ""; +// } +// return textValue; +// } /** * Calcul du poids <strong>(en gramme)</strong> à partir de la classe de taille et d'une définition de -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.