MicrometerUtil { public static <T> T register(MeterRegistry registry, MeterIdPrefix idPrefix, Class<T> type, BiFunction<MeterRegistry, MeterIdPrefix, T> factory) { requireNonNull(registry, "registry"); requireNonNull(idPrefix, "idPrefix"); requireNonNull(type, "type"); requireNonNull(factory, "factory"); final ConcurrentMap<MeterIdPrefix, Object> objects = map.computeIfAbsent(registry, unused -> new ConcurrentHashMap<>()); final RegistrationState registrationState = MicrometerUtil.registrationState.get(); if (registrationState.isRegistering) { throw new IllegalStateException("nested registration prohibited"); } final Object object = register(objects, registrationState, registry, idPrefix, type, factory); handlePendingRegistrations(objects, registrationState); @SuppressWarnings("unchecked") final T cast = (T) object; return cast; } private  MicrometerUtil(); static T register(MeterRegistry registry, MeterIdPrefix idPrefix, Class<T> type,
                                 BiFunction<MeterRegistry, MeterIdPrefix, T> factory); static void registerLater(MeterRegistry registry, MeterIdPrefix idPrefix, Class<T> type,
                                         BiFunction<MeterRegistry, MeterIdPrefix, T> factory);  }
@Test void getOrCreateGroup() { final Integer a = MicrometerUtil.register(metrics, ID_PREFIX_A, Integer.class, (parent, id) -> 42); assertThat(MicrometerUtil.register(metrics, ID_PREFIX_A, Integer.class, (parent, id) -> 0)).isSameAs(a); assertThatThrownBy(() -> MicrometerUtil.register(metrics, ID_PREFIX_A, String.class, (parent, id) -> "foo")) .isInstanceOf(IllegalStateException.class); }