Skip to content

Commit e34f949

Browse files
committed
Enhance DataTransactionResult with ifSuccessful and ifNotSuccessful. Closes #1429
Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
1 parent faba8ee commit e34f949

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/main/java/org/spongepowered/api/data/DataTransactionResult.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@
3636
import org.spongepowered.api.data.value.immutable.ImmutableValue;
3737
import org.spongepowered.api.data.value.mutable.CompositeValueStore;
3838
import org.spongepowered.api.data.value.mutable.Value;
39+
import org.spongepowered.api.util.PEBKACException;
3940
import org.spongepowered.api.util.ResettableBuilder;
4041

4142
import java.util.ArrayList;
4243
import java.util.Collection;
4344
import java.util.List;
45+
import java.util.concurrent.Callable;
46+
import java.util.function.Consumer;
47+
import java.util.function.Supplier;
4448

4549
/**
4650
* Represents a transaction taking place where a {@link DataHolder} is
@@ -319,6 +323,39 @@ public List<ImmutableValue<?>> getReplacedData() {
319323
return this.replaced;
320324
}
321325

326+
/**
327+
* If this result of {@link #isSuccessful()} returns {@code true},
328+
* the provided {@link Consumer} is called provided a list of all
329+
* "successful" data as retrieved from {@link #getSuccessfulData()}.
330+
*
331+
* @param consumer The consumer to call
332+
*/
333+
public void ifSucessful(Consumer<List<ImmutableValue<?>>> consumer) {
334+
if (isSuccessful()) {
335+
try {
336+
consumer.accept(this.success);
337+
} catch (Exception e) {
338+
// Because Callable throws exception in the signature.....
339+
new RuntimeException("Something went wrong trying to call a callable", e).printStackTrace();
340+
}
341+
}
342+
}
343+
344+
/**
345+
* Used to call a {@link Supplier} for an {@link Exception} of type
346+
* {@code E} such that if this transaction's {@link #isSuccessful()}
347+
* returns {@code false}, the supplier's exception is thrown.
348+
*
349+
* @param supplier The supplier of the exception to throw
350+
* @param <E> The type of exception
351+
* @throws E The exception to throw if this transaction is not successful
352+
*/
353+
public <E extends Exception> void ifNotSuccessful(Supplier<E> supplier) throws E {
354+
if (!isSuccessful()) {
355+
throw supplier.get();
356+
}
357+
}
358+
322359
@Override
323360
public String toString() {
324361
return Objects.toStringHelper(this)
@@ -329,6 +366,26 @@ public String toString() {
329366
.toString();
330367
}
331368

369+
@Override
370+
public boolean equals(Object o) {
371+
if (this == o) {
372+
return true;
373+
}
374+
if (o == null || getClass() != o.getClass()) {
375+
return false;
376+
}
377+
DataTransactionResult that = (DataTransactionResult) o;
378+
return type == that.type &&
379+
Objects.equal(rejected, that.rejected) &&
380+
Objects.equal(replaced, that.replaced) &&
381+
Objects.equal(success, that.success);
382+
}
383+
384+
@Override
385+
public int hashCode() {
386+
return Objects.hashCode(type, rejected, replaced, success);
387+
}
388+
332389
/**
333390
* A type of builder for building {@link DataTransactionResult}s. The common
334391
* use is for both implementations of {@link DataHolder}s, and various

0 commit comments

Comments
 (0)