Skip to content

Commit a41d791

Browse files
committed
Add DataTransactionResult#successfulValue
1 parent 9e9f8c2 commit a41d791

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collection;
3636
import java.util.List;
3737
import java.util.Objects;
38+
import java.util.Optional;
3839
import java.util.Set;
3940
import java.util.StringJoiner;
4041
import java.util.function.BiConsumer;
@@ -332,6 +333,24 @@ public List<Value.Immutable<?>> successfulData() {
332333
return this.success;
333334
}
334335

336+
/**
337+
* Gets the successfully applied {@link Value} based on the provided {@link Key}.
338+
*
339+
* @param key The key
340+
* @param <T> The data type
341+
* @param <V> The value type
342+
* @return The value, if available
343+
*/
344+
@SuppressWarnings("unchecked")
345+
public <T, V extends Value<T>> Optional<Value.Immutable<T>> successfulValue(final Key<V> key) {
346+
for (final Value.Immutable<?> value : this.successfulData()) {
347+
if (value.key() == key) {
348+
return Optional.of((Value.Immutable<T>) value);
349+
}
350+
}
351+
return Optional.empty();
352+
}
353+
335354
/**
336355
* If {@link Value.Mutable}s were supplied to the operation, this
337356
* collection will return any {@link Value.Immutable}s which were rejected
@@ -343,6 +362,24 @@ public List<Value.Immutable<?>> rejectedData() {
343362
return this.rejected;
344363
}
345364

365+
/**
366+
* Gets the rejected {@link Value} based on the provided {@link Key}.
367+
*
368+
* @param key The key
369+
* @param <T> The data type
370+
* @param <V> The value type
371+
* @return The value, if available
372+
*/
373+
@SuppressWarnings("unchecked")
374+
public <T, V extends Value<T>> Optional<Value.Immutable<T>> rejectedValue(final Key<V> key) {
375+
for (final Value.Immutable<?> value : this.rejectedData()) {
376+
if (value.key() == key) {
377+
return Optional.of((Value.Immutable<T>) value);
378+
}
379+
}
380+
return Optional.empty();
381+
}
382+
346383
/**
347384
* If the operation replaced any {@link Value.Mutable}s, this returns a collection
348385
* of the replaced {@link Value.Immutable}s.
@@ -353,6 +390,24 @@ public List<Value.Immutable<?>> replacedData() {
353390
return this.replaced;
354391
}
355392

393+
/**
394+
* Gets the replaced {@link Value} based on the provided {@link Key}.
395+
*
396+
* @param key The key
397+
* @param <T> The data type
398+
* @param <V> The value type
399+
* @return The value, if available
400+
*/
401+
@SuppressWarnings("unchecked")
402+
public <T, V extends Value<T>> Optional<Value.Immutable<T>> replacedValue(final Key<V> key) {
403+
for (final Value.Immutable<?> value : this.replacedData()) {
404+
if (value.key() == key) {
405+
return Optional.of((Value.Immutable<T>) value);
406+
}
407+
}
408+
return Optional.empty();
409+
}
410+
356411
/**
357412
* If this result of {@link #isSuccessful()} returns {@code true},
358413
* the provided {@link Consumer} is called provided a list of all

0 commit comments

Comments
 (0)