ImmutableAddressMetric implements AddressMetric { @Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { if (measurements.isEmpty()) { return JsonFactory.nullObject(); } else { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder(); jsonObjectBuilder.set(JsonFields.SCHEMA_VERSION, schemaVersion.toInt(), predicate); final List<Measurement> sortedMeasurements = new ArrayList<>(measurements); sortedMeasurements.sort(getMeasurementComparator()); for (final Measurement measurement : sortedMeasurements) { final JsonPointer pointer = JsonFactory.newPointer( JsonFactory.newKey(measurement.getMetricType().getName()), measurement.isSuccess() ? SUCCESS_KEY : FAILURE_KEY); jsonObjectBuilder.set(pointer, measurement.toJson().getValue(pointer).orElse(JsonFactory.newObject())); } return jsonObjectBuilder.build(); } } private  ImmutableAddressMetric(final Set<Measurement> measurements); static ImmutableAddressMetric of(final Set<Measurement> measurements); @Override Set<Measurement> getMeasurements(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); static AddressMetric fromJson(final JsonObject jsonObject); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void toJsonReturnsExpected() { final JsonObject actual = ADDRESS_METRIC.toJson(); assertThat(actual).isEqualTo(METRIC_JSON); }