public class TimeMachineData extends Model {
+ /**
+ * We use strings here in order to support measures with string value.
+ */
private Map<Date, List<String>> data = new HashMap<Date, List<String>>();
public Map<Date, List<String>> getData() {
return this;
}
+ public Double getValueAsDouble(Date date, int index) {
+ if (data.containsKey(date)) {
+ String valueStr = data.get(date).get(index);
+ try {
+ return valueStr == null ? null : Double.valueOf(valueStr);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+ return null;
+ }
+
}
--- /dev/null
+package org.sonar.wsclient.services;
+
+import org.junit.Test;
+
+import java.util.*;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class TimeMachineDataTest {
+
+ @Test
+ public void valueAsDouble() {
+ Map<Date, List<String>> map = new HashMap<Date, List<String>>();
+ Date date = new Date();
+ map.put(date, Arrays.asList(null, "20.3", "hello"));
+ TimeMachineData data = new TimeMachineData().setData(map);
+
+ assertThat(data.getValueAsDouble(date, 0), nullValue());
+ assertThat(data.getValueAsDouble(date, 1), is(20.3));
+ assertThat(data.getValueAsDouble(date, 2), nullValue());
+ }
+
+}