import eu.etaxonomy.cdm.model.description.TextData;
import eu.etaxonomy.cdm.model.description.TextFormat;
+/**
+ * @author m.venin
+ *
+ */
public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
+
+ String space = " ";
@Override
protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
// booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
- boolean average = false;
- float averagevalue = new Float(0);
- boolean sd = false;
- float sdvalue = new Float(0);
- boolean min = false;
- float minvalue = new Float(0);
- boolean max = false;
- float maxvalue = new Float(0);
- boolean lowerb = false;
- float lowerbvalue = new Float(0);
- boolean upperb = false;
- float upperbvalue = new Float(0);
- String unit = "(unknown unit)";
+ String unit = "";
if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
unit = mUnit.getLabel();
}
String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
- String space = " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
+
// the booleans and floats are updated according to the presence or absence of values
- if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
- average = true;
- averagevalue = measures.get(StatisticalMeasure.AVERAGE());
- }
- if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
- sd = true;
- sdvalue = measures.get(StatisticalMeasure.STANDARD_DEVIATION());
- }
- if (measures.containsKey(StatisticalMeasure.MIN())) {
- min = true;
- minvalue = measures.get(StatisticalMeasure.MIN());
- }
- if (measures.containsKey(StatisticalMeasure.MAX())) {
- max = true;
- maxvalue = measures.get(StatisticalMeasure.MAX());
- }
- if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
- lowerb = true;
- lowerbvalue = measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
- }
- if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
- upperb = true;
- upperbvalue = measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
- }
-
-
+
+ Boolean max, min, upperb, lowerb, average, sd;
+
+ String averagevalue = getValue(measures,StatisticalMeasure.AVERAGE());
+ if (averagevalue!=null) average=true; else average=false;
+ String sdvalue = getValue(measures,StatisticalMeasure.STANDARD_DEVIATION());
+ if (sdvalue!=null) sd=true; else sd=false;
+ String minvalue = getValue(measures,StatisticalMeasure.MIN());
+ if (minvalue!=null) min=true; else min=false;
+ String maxvalue = getValue(measures,StatisticalMeasure.MAX());
+ if (maxvalue!=null) max=true; else max=false;
+ String lowerbvalue = getValue(measures,StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
+ if (lowerbvalue!=null) lowerb=true; else lowerb=false;
+ String upperbvalue = getValue(measures,StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
+ if (upperbvalue!=null) upperb=true; else upperb=false;
+
+
// depending on the different associations of values, a sentence is built
if (max && min) {
QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
}
if ((max||min)&&(lowerb||upperb)) {
- QuantitativeDescription.append(","); // merge with below ?
+ QuantitativeDescription.append(separator); // merge with below ?
}
if ((lowerb||upperb)&&(min||max)) {
QuantitativeDescription.append(space + most_Frequently);
QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
}
if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
- QuantitativeDescription.append(",");
+ QuantitativeDescription.append(separator);
}
if (average) {
QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
}
}
- textData.putText(QuantitativeDescription.toString(), languages.get(0)); // which language should be put here ?
- textData.setFormat(TextFormat.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
+ textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
+ textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
return textData;
}
- protected String buildFeature(Feature feature, boolean doItBetter){
- if (feature==null || feature.getLabel()==null) return "";
- else {
- if (doItBetter) { // remove the text between brackets
- String str= feature.getLabel();
- StringBuilder strbuilder = new StringBuilder();
- do {
- strbuilder.append(StringUtils.substringBefore(str, "<"));
- }
- while (!(str=StringUtils.substringAfter(str, ">")).equals(""));
- return StringUtils.substringBeforeLast(strbuilder.toString()," ");
- }
- else{
- String betterString = StringUtils.replaceChars(feature.getLabel(), "<>",""); // only remove the brackets
- return StringUtils.substringBeforeLast(betterString," ");
- }
+
+
+ /**
+ * Returns the value of a given type of measure as a String. If the value is an integer it is printed
+ * as an integer instead of a float.
+ * If no value of this type is present, returns null.
+ *
+ * @param measures the map with the values
+ * @param key the desired measure
+ * @return
+ */
+ private String getValue(Map<StatisticalMeasure,Float> measures, Object key) {
+ Float floatValue;
+ Integer intValue;
+ if(measures.containsKey(key)) {
+ floatValue = measures.get(key);
+ intValue=floatValue.intValue();
+ if (floatValue.equals(intValue.floatValue())) return intValue.toString();
+ else return floatValue.toString();
}
+ else return null;
}
-
}