ValidatorRegistrationState { static ValidatorRegistrationState from(Stream<Particle> store, RadixAddress address) { Objects.requireNonNull(store, "store"); Objects.requireNonNull(address, "address"); return store .map(ValidatorRegistrationState::from) .filter(state -> state.getAddress().equals(address)) .max(Comparator.comparing(ValidatorRegistrationState::getNonce)) .orElseGet(() -> ValidatorRegistrationState.initial(address)); } private  ValidatorRegistrationState(Particle particle, RadixAddress address, boolean registered, long nonce); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void when_creating_validator_registration_state_with_null__then_exception_is_thrown() { Assertions.assertThatThrownBy(() -> ValidatorRegistrationState.from(null)) .isInstanceOf(NullPointerException.class); Assertions.assertThatThrownBy(() -> ValidatorRegistrationState.from(null, mock(RadixAddress.class))) .isInstanceOf(NullPointerException.class); Assertions.assertThatThrownBy(() -> ValidatorRegistrationState.from(Stream.empty(), null)) .isInstanceOf(NullPointerException.class); }
@Test public void when_extracting_from_stream_with_single_address__then_latest_correct_state_is_returned() { int numStateChanges = 10; List<Particle> particles = new ArrayList<>(numStateChanges); ValidatorRegistrationState currentState = populateStateChanges(ADDRESS_1, numStateChanges, particles); ValidatorRegistrationState extractedState = ValidatorRegistrationState.from(particles.stream(), ADDRESS_1); assertThat(extractedState).isEqualTo(currentState); }
@Test public void when_extracting_from_stream_with_multiple_addresses__then_latest_correct_state_is_returned_for_each() { List<Particle> particles = new ArrayList<>(); ValidatorRegistrationState currentState1 = populateStateChanges(ADDRESS_1, 5, particles); ValidatorRegistrationState currentState2 = populateStateChanges(ADDRESS_2, 6, particles); ValidatorRegistrationState extractedState1 = ValidatorRegistrationState.from(particles.stream(), ADDRESS_1); assertThat(extractedState1).isEqualTo(currentState1); ValidatorRegistrationState extractedState2 = ValidatorRegistrationState.from(particles.stream(), ADDRESS_2); assertThat(extractedState2).isEqualTo(currentState2); }
@Test public void when_converting_from_invalid_particle__then_exception_is_thrown() { Assertions.assertThatThrownBy(() -> ValidatorRegistrationState.from(mock(Particle.class))) .isInstanceOf(IllegalArgumentException.class); }