View Javadoc
1   package fr.ifremer.tutti.util;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.entities.protocol.Rtp;
28  import fr.ifremer.tutti.type.WeightUnit;
29  
30  import java.util.Objects;
31  
32  /**
33   * Created on 8/26/14.
34   *
35   * @author Tony Chemit - chemit@codelutin.com
36   * @since 3.7
37   */
38  public class Weights {
39  
40  //    private static DecimalFormat decimalFormat;
41  
42  //    public static DecimalFormat getDecimalFormat(int minDecimal, int maxDecimal) {
43  //        if (decimalFormat == null) {
44  //            decimalFormat = new DecimalFormat();
45  //
46  //            DecimalFormatSymbols symbols = new DecimalFormatSymbols();
47  //            symbols.setDecimalSeparator('.');
48  //            symbols.setGroupingSeparator(' ');
49  //
50  //            decimalFormat.setDecimalFormatSymbols(symbols);
51  //            decimalFormat.setGroupingUsed(false);
52  //        }
53  //        decimalFormat.setMinimumFractionDigits(minDecimal);
54  //        decimalFormat.setMaximumFractionDigits(maxDecimal);
55  //        return decimalFormat;
56  //    }
57  
58  //    public static String getWeightStringValue(Float weight) {
59  //        String textValue;
60  //        if (weight != null) {
61  //            DecimalFormat weightDecimalFormat = getDecimalFormat(1, 3);
62  //            textValue = weightDecimalFormat.format(weight);
63  //
64  //        } else {
65  //            textValue = "";
66  //        }
67  //        return textValue;
68  //    }
69  
70      /**
71       * Calcul du poids <strong>(en gramme)</strong> à partir de la classe de taille et d'une définition de
72       * Relation Taille Poids.
73       *
74       * La formule est {@code Poids (gramme) = a Taille (cm) ^ b},
75       *
76       * @param rtp            la définition du RTP à appliquer
77       * @param lengthStep     la classe de taille à transformer
78       * @param lengthStepUnit l'unité de la classe de taille
79       * @return Le poids calculé en gramme à partir de la classe de taille et du RTP
80       * @since 4.5
81       */
82      public static float computeWithRtp(Rtp rtp, float lengthStep, String lengthStepUnit) {
83          lengthStep = Numbers.convertToCm(lengthStep, lengthStepUnit);
84          return (float) (rtp.getA() * Math.pow(lengthStep, rtp.getB()));
85      }
86  
87      /**
88       * Pour convertir d'une unité vers une autre.
89       *
90       * @param sourceWeight     le poids à convertir
91       * @param sourceWeightUnit l'unité de convertion
92       * @param targetWeightUnit l'unité de convertion
93       * @return le poids converti (et arrondi) dans l'unité cible
94       */
95      public static float convert(WeightUnit sourceWeightUnit, WeightUnit targetWeightUnit, float sourceWeight) {
96  
97          Objects.requireNonNull(sourceWeightUnit);
98          Objects.requireNonNull(targetWeightUnit);
99  
100         // on convertit le poids source en mode «entité»
101         float sourceWeightToEntity = sourceWeightUnit.toEntity(sourceWeight);
102 
103         // on convertit depuis le mode «entité» vers l'unité cible
104         float targetWeightFromEntity = targetWeightUnit.fromEntity(sourceWeightToEntity);
105 
106         // on arrondit selon l'unité cible
107         return targetWeightUnit.round(targetWeightFromEntity);
108 
109     }
110 
111     /**
112      * Pour convertir d'une unité vers une autre.
113      *
114      * @param sourceWeight     le poids à convertir
115      * @param sourceWeightUnit l'unité de convertion
116      * @param targetWeightUnit l'unité de convertion
117      * @return le poids converti (et arrondi) dans l'unité cible
118      */
119     public static Float convert(WeightUnit sourceWeightUnit, WeightUnit targetWeightUnit, Float sourceWeight) {
120 
121         Float targetWeight = sourceWeight;
122         if (sourceWeight != null) {
123             targetWeight = convert(sourceWeightUnit, targetWeightUnit, (float) sourceWeight);
124         }
125 
126         return targetWeight;
127 
128     }
129 }