1 package eu
.etaxonomy
.cdm
.api
.service
;
6 import org
.apache
.commons
.lang
.StringUtils
;
8 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
9 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
10 import eu
.etaxonomy
.cdm
.model
.description
.MeasurementUnit
;
11 import eu
.etaxonomy
.cdm
.model
.description
.NaturalLanguageTerm
;
12 import eu
.etaxonomy
.cdm
.model
.description
.StatisticalMeasure
;
13 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
14 import eu
.etaxonomy
.cdm
.model
.description
.TextFormat
;
20 public class DefaultQuantitativeDescriptionBuilder
extends AbstractQuantitativeDescriptionBuilder
{
25 protected TextData
doBuild(Map
<StatisticalMeasure
,Float
> measures
, MeasurementUnit mUnit
, List
<Language
> languages
){
26 StringBuilder QuantitativeDescription
= new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
27 TextData textData
= TextData
.NewInstance(); // TextData that will contain the description and the language corresponding
28 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
31 if ((mUnit
!=null)&&(mUnit
.getLabel()!=null)){
32 unit
= mUnit
.getLabel();
35 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
36 NaturalLanguageTerm nltFrom
= NaturalLanguageTerm
.FROM();
37 String from
= nltFrom
.getPreferredRepresentation(languages
).getLabel();
38 NaturalLanguageTerm nltTo
= NaturalLanguageTerm
.TO();
39 String to
= nltTo
.getPreferredRepresentation(languages
).getLabel();
40 NaturalLanguageTerm nltUp_To
= NaturalLanguageTerm
.UP_TO();
41 String up_To
= nltUp_To
.getPreferredRepresentation(languages
).getLabel();
42 NaturalLanguageTerm nltMost_Frequently
= NaturalLanguageTerm
.MOST_FREQUENTLY();
43 String most_Frequently
= nltMost_Frequently
.getPreferredRepresentation(languages
).getLabel();
44 NaturalLanguageTerm nltOn_Average
= NaturalLanguageTerm
.ON_AVERAGE();
45 String on_Average
= nltOn_Average
.getPreferredRepresentation(languages
).getLabel();
46 NaturalLanguageTerm nltMore_Or_Less
= NaturalLanguageTerm
.MORE_OR_LESS();
47 String more_Or_Less
= nltMore_Or_Less
.getPreferredRepresentation(languages
).getLabel();
50 // the booleans and floats are updated according to the presence or absence of values
52 Boolean max
, min
, upperb
, lowerb
, average
, sd
;
54 String averagevalue
= getValue(measures
,StatisticalMeasure
.AVERAGE());
55 if (averagevalue
!=null) average
=true; else average
=false;
56 String sdvalue
= getValue(measures
,StatisticalMeasure
.STANDARD_DEVIATION());
57 if (sdvalue
!=null) sd
=true; else sd
=false;
58 String minvalue
= getValue(measures
,StatisticalMeasure
.MIN());
59 if (minvalue
!=null) min
=true; else min
=false;
60 String maxvalue
= getValue(measures
,StatisticalMeasure
.MAX());
61 if (maxvalue
!=null) max
=true; else max
=false;
62 String lowerbvalue
= getValue(measures
,StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY());
63 if (lowerbvalue
!=null) lowerb
=true; else lowerb
=false;
64 String upperbvalue
= getValue(measures
,StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY());
65 if (upperbvalue
!=null) upperb
=true; else upperb
=false;
68 // depending on the different associations of values, a sentence is built
70 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ to
+ space
+ maxvalue
+ space
+ unit
);
73 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ unit
);
76 QuantitativeDescription
.append(space
+ up_To
+ space
+ maxvalue
+ space
+ unit
);
78 if ((max
||min
)&&(lowerb
||upperb
)) {
79 QuantitativeDescription
.append(separator
); // merge with below ?
81 if ((lowerb
||upperb
)&&(min
||max
)) {
82 QuantitativeDescription
.append(space
+ most_Frequently
);
84 if (upperb
&& lowerb
) {
85 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ to
+ space
+ upperbvalue
+ space
+ unit
);
88 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ unit
);
91 QuantitativeDescription
.append(space
+ up_To
+ space
+ upperbvalue
+ space
+ unit
);
93 if (((max
||min
)&&(average
))||((lowerb
||upperb
)&&(average
))) {
94 QuantitativeDescription
.append(separator
);
97 QuantitativeDescription
.append(space
+ averagevalue
+ space
+ unit
+ space
+ on_Average
);
99 QuantitativeDescription
.append("("+ more_Or_Less
+ space
+ sdvalue
+ ")");
102 textData
.putText(languages
.get(0), QuantitativeDescription
.toString()); // which language should be put here ?
103 textData
.setFormat(TextFormat
.NewInstance(null, "Text",null ));
111 * Returns the value of a given type of measure as a String. If the value is an integer it is printed
112 * as an integer instead of a float.
113 * If no value of this type is present, returns null.
115 * @param measures the map with the values
116 * @param key the desired measure
119 private String
getValue(Map
<StatisticalMeasure
,Float
> measures
, Object key
) {
122 if(measures
.containsKey(key
)) {
123 floatValue
= measures
.get(key
);
124 intValue
=floatValue
.intValue();
125 if (floatValue
.equals(intValue
.floatValue())) return intValue
.toString();
126 else return floatValue
.toString();