### Question:
InstrumentRegistry { @SuppressWarnings("unchecked") <I extends AbstractInstrument> I register(I instrument) { AbstractInstrument oldInstrument = registry.putIfAbsent(instrument.getDescriptor().getName().toLowerCase(), instrument); if (oldInstrument != null) { if (!instrument.getClass().isInstance(oldInstrument) || !instrument.equals(oldInstrument)) { throw new IllegalArgumentException( "Instrument with same name and different descriptor already created."); } return (I) oldInstrument; } return instrument; }    }### Answer:
@Test void register() { MeterSharedState meterSharedState = MeterSharedState.create(InstrumentationLibraryInfo.getEmpty()); TestInstrument testInstrument = new TestInstrument( INSTRUMENT_DESCRIPTOR, METER_PROVIDER_SHARED_STATE, meterSharedState, ACTIVE_BATCHER); assertThat(meterSharedState.getInstrumentRegistry().register(testInstrument)) .isSameAs(testInstrument); assertThat(meterSharedState.getInstrumentRegistry().register(testInstrument)) .isSameAs(testInstrument); assertThat( meterSharedState .getInstrumentRegistry() .register( new TestInstrument( INSTRUMENT_DESCRIPTOR, METER_PROVIDER_SHARED_STATE, meterSharedState, ACTIVE_BATCHER))) .isSameAs(testInstrument); }
@Test void register_OtherDescriptor() { MeterSharedState meterSharedState = MeterSharedState.create(getEmpty()); TestInstrument testInstrument = new TestInstrument( INSTRUMENT_DESCRIPTOR, METER_PROVIDER_SHARED_STATE, meterSharedState, ACTIVE_BATCHER); assertThat(meterSharedState.getInstrumentRegistry().register(testInstrument)) .isSameAs(testInstrument); assertThrows( IllegalArgumentException.class, () -> meterSharedState .getInstrumentRegistry() .register( new TestInstrument( OTHER_INSTRUMENT_DESCRIPTOR, METER_PROVIDER_SHARED_STATE, meterSharedState, ACTIVE_BATCHER)), "Instrument with same name and different descriptor already created."); }
@Test void register_OtherInstance() { MeterSharedState meterSharedState = MeterSharedState.create(getEmpty()); TestInstrument testInstrument = new TestInstrument( INSTRUMENT_DESCRIPTOR, METER_PROVIDER_SHARED_STATE, meterSharedState, ACTIVE_BATCHER); assertThat(meterSharedState.getInstrumentRegistry().register(testInstrument)) .isSameAs(testInstrument); assertThrows( IllegalArgumentException.class, () -> meterSharedState .getInstrumentRegistry() .register( new OtherTestInstrument( INSTRUMENT_DESCRIPTOR, METER_PROVIDER_SHARED_STATE, meterSharedState, ACTIVE_BATCHER)), "Instrument with same name and different descriptor already created."); }