ImmutableMeasurement implements Measurement { @Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final JsonObject counts = values.entrySet() .stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(e -> JsonFactory.newField(JsonFactory.newKey(e.getKey().toString()), JsonValue.of(e.getValue()))) .collect(JsonCollectors.fieldsToObject()); return JsonFactory.newObjectBuilder() .set(metricType.getName(), JsonFactory.newObjectBuilder() .set(getSuccessFieldName(success), JsonFactory.newObjectBuilder(counts) .set(JsonFields.LAST_MESSAGE_AT, getLastMessageAt() .map(Object::toString).orElse(null) ).build() ).build() ).build(); } ImmutableMeasurement(final MetricType metricType, final boolean success,
            final Map<Duration, Long> values, @Nullable final Instant lastMessageAt); @Override MetricType getMetricType(); @Override boolean isSuccess(); @Override Map<Duration, Long> getCounts(); @Override Optional<Instant> getLastMessageAt(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); static Measurement fromJson(final JsonObject jsonObject); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void toJsonReturnsExpected() { final JsonObject actual = MEASUREMENT.toJson(); assertThat(actual).isEqualTo(MEASUREMENT_JSON); }