Skip to content

Commit

Permalink
[type-safe] Rename getOrDefault to getOrElseGet to align with Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Boynes committed Jun 12, 2018
1 parent fdeb0ad commit a02ecc8
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class ThreadLocalMetrics {
* @return the MetricContext associated with the current Thread
*/
public static MetricContext current() {
return ThreadContext.current().getOrDefault(METRIC_CONTEXT, NullContext::empty);
return ThreadContext.current().getOrElseGet(METRIC_CONTEXT, NullContext::empty);
}

// Suppress construction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public <V> V get(Key<V> key) {
* @param <V> the type of value
* @return the key's value, or the default from the supplier
*/
public <V> V getOrDefault(Key<V> key, Supplier<V> supplier) {
return values.getOrDefault(key.key, supplier);
public <V> V getOrElseGet(Key<V> key, Supplier<V> supplier) {
return values.getOrElseGet(key.key, supplier);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void contextReturnsNullForMissingKey() {
@Test
public void defaultIsSuppliedForMissingKey() {
ThreadContext context = ThreadContext.emptyContext();
assertEquals("hello", context.getOrDefault(REQUEST_ID, () -> "hello"));
assertEquals("hello", context.getOrElseGet(REQUEST_ID, () -> "hello"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public default <T> Optional<T> getOptional(Key<T> key) {
* @param <T> the data type
* @return the data stored for the key, or the default value from the supplier
*/
default <T> T getOrDefault(Key<T> key, Supplier<T> supplier) {
default <T> T getOrElseGet(Key<T> key, Supplier<T> supplier) {
T value = get(key);
if (value == null) {
return supplier.get();
Expand Down

0 comments on commit a02ecc8

Please sign in to comment.