/** * Creates a new {@link DataTransactionResult} that ends in failure. The * provided {@link ImmutableValue}s are considered "rejected" and were not * successfully added. * * @param values The values that were rejected * @return The new data transaction result */ public static DataTransactionResult failResult(final Iterable<ImmutableValue<?>> values) { return builder().reject(values).result(Type.FAILURE).build(); }
/** * 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(); }
this.resultType = result.getType(); } else { if (this.resultType.compareTo(result.getType()) < 0) { this.resultType = result.getType(); dance: for (final ImmutableValue<?> value : this.successful) { for (final ImmutableValue<?> rejected : result.getRejectedData()) { if (value.getKey().equals(rejected.getKey())) { newRejected.add(rejected); for (final ImmutableValue<?> replaced : result.getReplacedData()) { if (value.getKey().equals(replaced.getKey())) { newReplaced.add(value); for (final ImmutableValue<?> successful : result.getSuccessfulData()) { if (value.getKey().equals(successful.getKey())) { newSuccessful.add(successful); dance: for (final ImmutableValue<?> value : this.replaced) { for (final ImmutableValue<?> rejected : result.getRejectedData()) { if (value.getKey().equals(rejected.getKey())) { newRejected.add(rejected); for (final ImmutableValue<?> replaced : result.getReplacedData()) { if (value.getKey().equals(replaced.getKey())) { newReplaced.add(value);
@Override public Builder from(DataTransactionResult value) { this.resultType = value.type; this.rejected = new ArrayList<>(value.getRejectedData()); this.replaced = new ArrayList<>(value.getReplacedData()); this.successful = new ArrayList<>(value.getSuccessfulData()); return this; }
/** * If this result of {@link #isSuccessful()} returns {@code true}, * the provided {@link Consumer} is called provided a list of all * "successful" data as retrieved from {@link #getSuccessfulData()}. * * @param consumer The consumer to call */ public void ifSuccessful(Consumer<List<ImmutableValue<?>>> consumer) { if (isSuccessful()) { consumer.accept(this.success); } }
@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))); }
/** * Gets whether this {@link DataTransactionResult} was successful or not. * * @return True if this result was successful */ public boolean isSuccessful() { return getType() == Type.SUCCESS; }
/** * Builds a new {@link DataTransactionResult} with the providing * {@link List}s of {@link ImmutableValue}s that are successfully * offered, {@link ImmutableValue}s that were replaced, and * {@link ImmutableValue}s that were rejected. * * @return The newly created transaction result */ public DataTransactionResult build() { checkState(this.resultType != null); return new DataTransactionResult(this); }
/** * If this result of {@link #isSuccessful()} returns {@code true}, * the provided {@link Consumer} is called provided a list of all * "successful" data as retrieved from {@link #getSuccessfulData()}. * * @param consumer The consumer to call * @deprecated Use {@link #ifSuccessful(Consumer)} instead */ @Deprecated public void ifSucessful(Consumer<List<ImmutableValue<?>>> consumer) { ifSuccessful(consumer); }
/** * Used to call a {@link Supplier} for an {@link Exception} of type * {@code E} such that if this transaction's {@link #isSuccessful()} * returns {@code false}, the supplier's exception is thrown. * * @param supplier The supplier of the exception to throw * @param <E> The type of exception * @throws E The exception to throw if this transaction is not successful */ public <E extends Exception> void ifNotSuccessful(Supplier<E> supplier) throws E { if (!isSuccessful()) { throw supplier.get(); } }
@Override public Builder from(DataTransactionResult value) { this.resultType = value.type; this.rejected = new ArrayList<>(value.getRejectedData()); this.replaced = new ArrayList<>(value.getReplacedData()); this.successful = new ArrayList<>(value.getSuccessfulData()); return this; }
/** * Gets whether this {@link DataTransactionResult} was successful or not. * * @return True if this result was successful */ public boolean isSuccessful() { return getType() == Type.SUCCESS; }
/** * Builds a new {@link DataTransactionResult} with the providing * {@link List}s of {@link ImmutableValue}s that are successfully * offered, {@link ImmutableValue}s that were replaced, and * {@link ImmutableValue}s that were rejected. * * @return The newly created transaction result */ public DataTransactionResult build() { checkState(this.resultType != null); return new DataTransactionResult(this); }
/** * Offers the given {@link ValueContainer} such that all of the available * {@link BaseValue}s from the given {@link ValueContainer} are offered * to this {@link CompositeValueStore}. Intentionally, however, this differs * from {@link #offer(ValueContainer)} as it will intentionally throw an * exception if the result was a failure. * * @param valueContainer The value to set * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default DataTransactionResult tryOffer(H valueContainer) { final DataTransactionResult result = offer(valueContainer, MergeFunction.IGNORE_ALL); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Creates a new {@link DataTransactionResult} that ends in failure. The * provided {@link ImmutableValue} is considered "incompatible" and was not * successfully added. * * @param value The value that was incompatible or errored * @return The new data transaction result */ public static DataTransactionResult errorResult(final ImmutableValue<?> value) { return builder().result(Type.ERROR).reject(value).build(); }
this.resultType = result.getType(); } else { if (this.resultType.compareTo(result.getType()) < 0) { this.resultType = result.getType(); dance: for (final ImmutableValue<?> value : this.successful) { for (final ImmutableValue<?> rejected : result.getRejectedData()) { if (value.getKey().equals(rejected.getKey())) { newRejected.add(rejected); for (final ImmutableValue<?> replaced : result.getReplacedData()) { if (value.getKey().equals(replaced.getKey())) { newReplaced.add(value); for (final ImmutableValue<?> successful : result.getSuccessfulData()) { if (value.getKey().equals(successful.getKey())) { newSuccessful.add(successful); dance: for (final ImmutableValue<?> value : this.replaced) { for (final ImmutableValue<?> rejected : result.getRejectedData()) { if (value.getKey().equals(rejected.getKey())) { newRejected.add(rejected); for (final ImmutableValue<?> replaced : result.getReplacedData()) { if (value.getKey().equals(replaced.getKey())) { newReplaced.add(value);
/** * 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(); }
/** * Offers the given {@link ValueContainer} such that all of the available * {@link BaseValue}s from the given {@link ValueContainer} are offered * to this {@link CompositeValueStore}. Any overlaps of data are merged via * the {@link MergeFunction}. Intentionally, however, this differs * from {@link #offer(ValueContainer)} as it will intentionally throw an * exception if the result was a failure. * * @param valueContainer The value to set * @param function The merge function * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default DataTransactionResult tryOffer(H valueContainer, MergeFunction function) throws IllegalArgumentException { final DataTransactionResult result = offer(valueContainer, function); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Creates a new {@link DataTransactionResult} that ends in failure. The * provided {@link ImmutableValue} is considered "rejected" and was not * successfully added. * * @param value The value that was rejected * @return The new data transaction result */ public static DataTransactionResult failResult(final ImmutableValue<?> value) { return builder().reject(value).result(Type.FAILURE).build(); }