/**
 * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.web.analytics.formatting;

import java.util.List;
import java.util.Map;

import org.threeten.bp.ZonedDateTime;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.opengamma.engine.value.ValueSpecification;
import com.opengamma.util.timeseries.localdate.LocalDateDoubleTimeSeries;
import com.opengamma.util.timeseries.zoneddatetime.ZonedDateTimeDoubleTimeSeries;

/**
 *
 */
/* package */ class LocalDateDoubleTimeSeriesFormatter extends AbstractFormatter<LocalDateDoubleTimeSeries> {

  /* package */ LocalDateDoubleTimeSeriesFormatter() {
    super(LocalDateDoubleTimeSeries.class);
    addFormatter(new Formatter<LocalDateDoubleTimeSeries>(Format.EXPANDED) {
      @Override
      Object format(LocalDateDoubleTimeSeries value, ValueSpecification valueSpec) {
        return formatExpanded(value);
      }
    });

  }

  @Override
  public String formatCell(LocalDateDoubleTimeSeries timeSeries, ValueSpecification valueSpec) {
    return "Time-series (" + timeSeries.getEarliestTime() + " to " + timeSeries.getLatestTime() + ")";
  }

  private Map<String, Object> formatExpanded(LocalDateDoubleTimeSeries value) {
    ZonedDateTimeDoubleTimeSeries series = value.toZonedDateTimeDoubleTimeSeries();
    List<Object[]> data = Lists.newArrayListWithCapacity(series.size());
    for (Map.Entry<ZonedDateTime, Double> entry : series) {
      long timeMillis = entry.getKey().toInstant().toEpochMilli();
      Double vol = entry.getValue();
      data.add(new Object[]{timeMillis, vol});
    }
    Map<String, String> templateData = ImmutableMap.of("data_field", "Historical Time Series",
                                                       "observation_time", "Historical Time Series");
    Map<String, Object> timeSeries = ImmutableMap.of("fieldLabels", new String[]{"Time", "Value"},
                                                     "data", data);
    return ImmutableMap.<String, Object>of("template_data", templateData, "timeseries", timeSeries);
  }

  @Override
  public DataType getDataType() {
    return DataType.TIME_SERIES;
  }
}

@@ -1135,22 +1135,29 @@
 ) %7B%0A    
-return
+String text =
  %22Time-s
@@ -1162,16 +1162,67 @@
 -series 
+%22;%0A    text += timeSeries.isEmpty() ? %22(empty)%22 : %22
 (%22 + tim
@@ -1292,16 +1292,33 @@
  + %22)%22;%0A
+    return text;%0A
   %7D%0A%0A  p

Fix bug where an empty time-series would cause exception to be thrown