]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-833: New Web Service to get history of measures (time machine)
authorGodin <mandrikov@gmail.com>
Mon, 13 Dec 2010 10:20:33 +0000 (10:20 +0000)
committerGodin <mandrikov@gmail.com>
Mon, 13 Dec 2010 10:20:33 +0000 (10:20 +0000)
sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java
sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineDataTest.java [new file with mode: 0644]

index e017253f783edaa123374439de2ec7e9d10dc15e..4c68d2c3ca10d1a43843a6d54c6c65bac72ebbd8 100644 (file)
@@ -7,6 +7,9 @@ import java.util.Map;
 
 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() {
@@ -18,4 +21,16 @@ public class TimeMachineData extends Model {
     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;
+  }
+
 }
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineDataTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/TimeMachineDataTest.java
new file mode 100644 (file)
index 0000000..cd7fec9
--- /dev/null
@@ -0,0 +1,25 @@
+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());
+  }
+
+}