/** * Applies a transformation on the provided {@link BaseValue} such that * the return value of {@link Function#apply(Object)} will become the end * resulting value set into this {@link CompositeValueStore}. It is not * necessary that the input is actually present, in which case the * {@link Key}ed data is compatible, but not necessarily present. Writing * a {@link Function} to properly handle the potential for a null input * is required for this method to execute without exception. * * @param key The key linked to * @param function The function to manipulate the value * @param <E> The type of value * @return The end resulting value */ default <E> DataTransactionResult transform(Key<? extends BaseValue<E>> key, Function<E, E> function) { if (supports(key)) { return offer(key, checkNotNull(function.apply(get(key).orElse(null)))); } return DataTransactionResult.failNoData(); }
/** * Applies a transformation on the pre-existing value of the data keyed by * the provided {@link Key} and returns a {@link DataTransactionResult} of * said transformation. * * @param x The X position * @param y The Y position * @param z The Z position * @param key The key to the data * @param function The function applying the transformation * @param <E> The type of data * @return The transaction result */ default <E> DataTransactionResult transform(int x, int y, int z, Key<? extends BaseValue<E>> key, Function<E, E> function) { if (supports(x, y, z, key)) { final Optional<E> optional = get(x, y, z, key); if (optional.isPresent()) { return offer(x, y, z, key, function.apply(optional.get())); } } return DataTransactionResult.failNoData(); }
@Test public void testValueChangeEvent() { DataTransactionResult original = DataTransactionResult.failNoData(); DataTransactionResult modified = DataTransactionResult.successNoData(); ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange( Cause.of(EventContext.empty(), "none"), original, mockParam(DataHolder.class)); assertThat(event.getOriginalChanges(), is(equalTo(original))); event.proposeChanges(modified); assertThat(event.getEndResult(), is(equalTo(modified))); }
/** * Applies a transformation on the provided {@link BaseValue} such that * the return value of {@link Function#apply(Object)} will become the end * resulting value set into this {@link CompositeValueStore}. It is not * necessary that the input is actually present, in which case the * {@link Key}ed data is compatible, but not necessarily present. Writing * a {@link Function} to properly handle the potential for a null input * is required for this method to execute without exception. * * @param key The key linked to * @param function The function to manipulate the value * @param <E> The type of value * @return The end resulting value */ default <E> DataTransactionResult transform(Key<? extends BaseValue<E>> key, Function<E, E> function) { if (supports(key)) { return offer(key, checkNotNull(function.apply(get(key).orElse(null)))); } return DataTransactionResult.failNoData(); }
/** * Applies a transformation on the pre-existing value of the data keyed by * the provided {@link Key} and returns a {@link DataTransactionResult} of * said transformation. * * @param x The X position * @param y The Y position * @param z The Z position * @param key The key to the data * @param function The function applying the transformation * @param <E> The type of data * @return The transaction result */ default <E> DataTransactionResult transform(int x, int y, int z, Key<? extends BaseValue<E>> key, Function<E, E> function) { if (supports(x, y, z, key)) { final Optional<E> optional = get(x, y, z, key); if (optional.isPresent()) { return offer(x, y, z, key, function.apply(optional.get())); } } return DataTransactionResult.failNoData(); }