public void print(PrintStream stream) {
    if (mSeries.isEmpty()) {
      return;
    }
    long start = mSeries.firstKey();
    stream.printf("Time series starts at %d with width %d.%n", start, mWidthNano);
    int bucketIndex = 0;
    Iterator<Map.Entry<Long, Integer>> it = mSeries.entrySet().iterator();

    Map.Entry<Long, Integer> current = it.next();
    while (current != null) {
      int numEvents = 0;
      if (bucketIndex * mWidthNano + start == current.getKey()) {
        numEvents = current.getValue();
        current = null;
        if (it.hasNext()) {
          current = it.next();
        }
      }
      stream.printf("%d %d%n", bucketIndex, numEvents);
      bucketIndex++;
    }
  }
@Test
  public void printTest() {
    TimeSeries timeSeries = new TimeSeries();
    timeSeries.record(mBase);
    timeSeries.record(mBase + 8L * Constants.SECOND_NANO);
    timeSeries.record(mBase + 8L * Constants.SECOND_NANO + 1);
    timeSeries.record(mBase + 9L * Constants.SECOND_NANO + 1);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(outputStream);

    timeSeries.print(printStream);

    StringBuilder sb = new StringBuilder();
    sb.append(
        String.format("Time series starts at %d with width %d.%n", mBase, Constants.SECOND_NANO));
    sb.append(String.format("%d %d%n", 0, 1));
    for (int i = 1; i < 8; i++) {
      sb.append(String.format("%d %d%n", i, 0));
    }
    sb.append(String.format("%d %d%n", 8, 2));
    sb.append(String.format("%d %d%n", 9, 1));
    printStream.close();

    Assert.assertEquals(sb.toString(), outputStream.toString());
  }