private <T extends Metric> MetricImpl toImpl(String name, T metric) { // Find subtype of Metric, needed for user-defined metrics Class<?> clazz = metric.getClass(); do { Optional<Class<?>> optionalClass = Arrays.stream(clazz.getInterfaces()) .filter(c -> Metric.class.isAssignableFrom(c)) .findFirst(); if (optionalClass.isPresent()) { clazz = optionalClass.get(); break; } clazz = clazz.getSuperclass(); } while (clazz != null); return toImpl(new Metadata(name, MetricType.from(clazz == null ? metric.getClass() : clazz)), metric); }
/** * Sets the metric type using a String representation of {@link MetricType}. * @param type the new metric type * @throws IllegalArgumentException if the String is not a valid {@link MetricType} */ public void setType(String type) throws IllegalArgumentException { this.type = MetricType.from(type); }
@Override public <T extends Metric> T register(String name, T t) throws IllegalArgumentException { return register(new Metadata(name, MetricType.from(t.getClass())), t); }