Author: tchemit Date: 2013-12-09 22:07:13 +0100 (Mon, 09 Dec 2013) New Revision: 1444 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1444 Log: fixes #3949: Add useful methods on ApplicationDataUtil class Modified: application/trunk/application/src/main/java/fr/ifremer/shared/application/ApplicationDataUtil.java Modified: application/trunk/application/src/main/java/fr/ifremer/shared/application/ApplicationDataUtil.java =================================================================== --- application/trunk/application/src/main/java/fr/ifremer/shared/application/ApplicationDataUtil.java 2013-12-09 21:06:07 UTC (rev 1443) +++ application/trunk/application/src/main/java/fr/ifremer/shared/application/ApplicationDataUtil.java 2013-12-09 21:07:13 UTC (rev 1444) @@ -26,9 +26,13 @@ import com.google.common.base.Preconditions; import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang3.time.DurationFormatUtils; +import java.math.BigDecimal; +import java.math.MathContext; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.util.Date; import static org.nuiton.i18n.I18n._; @@ -46,6 +50,8 @@ public static final double EARTH_RADIUS = 6378288.0; + private static final MathContext MATH_CONTEXT_4_DIGIT = new MathContext(4); + private static DecimalFormatSymbols symbols; private static DecimalFormat decimalFormat; @@ -135,4 +141,45 @@ } return textValue; } + + public static <N extends Number> N getValueOrComputedValue(N value, N computedValue) { + return value == null ? computedValue : value; + } + + public static <N extends Number> Boolean getValueOrComputedValueComputed(N value, N computedValue) { + Boolean result; + if (value == null) { + + result = computedValue == null ? null : true; + } else { + result = false; + } + return result; + } + + /** + * Round the given value to max 4 digits. + * + * @param value the float to round. + * @return the rounded value + * @since 1.0.1 + */ + public static float roundKiloGram(float value) { + BigDecimal sumB = new BigDecimal(value); + float result = sumB.abs(MATH_CONTEXT_4_DIGIT).floatValue(); + return result; + } + + public static String getDuration(Date startDate, + Date endDate, + String format) { + String duration = ""; + if (startDate != null && endDate != null && !startDate.after(endDate)) { + duration = DurationFormatUtils.formatPeriod( + startDate.getTime(), + endDate.getTime(), + format); + } + return duration; + } }