From 13d6bd6fe6c9cab3bbe8562c5b18970be27d921a Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Wed, 18 May 2022 12:34:56 -0700 Subject: [PATCH 1/3] fix(Context)! correct signature of Context.get T get(Class type) would mean that `type` could be a subclass of the return value. e.g. returning a Number when asked for a Float. That's the opposite of what we wanted! --- .../java/org/terasology/engine/registry/CoreRegistryTest.java | 4 ++-- .../src/main/java/org/terasology/engine/context/Context.java | 4 ++-- .../org/terasology/engine/context/internal/ContextImpl.java | 4 ++-- .../org/terasology/engine/context/internal/MockContext.java | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/engine-tests/src/test/java/org/terasology/engine/registry/CoreRegistryTest.java b/engine-tests/src/test/java/org/terasology/engine/registry/CoreRegistryTest.java index dad43023317..39f85c11bbe 100644 --- a/engine-tests/src/test/java/org/terasology/engine/registry/CoreRegistryTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/registry/CoreRegistryTest.java @@ -1,4 +1,4 @@ -// Copyright 2021 The Terasology Foundation +// Copyright 2022 The Terasology Foundation // SPDX-License-Identifier: Apache-2.0 package org.terasology.engine.registry; @@ -82,7 +82,7 @@ private static class ContextImplementation implements Context { private final Map, Object> map = Maps.newConcurrentMap(); @Override - public T get(Class type) { + public T get(Class type) { T result = type.cast(map.get(type)); if (result != null) { return result; diff --git a/engine/src/main/java/org/terasology/engine/context/Context.java b/engine/src/main/java/org/terasology/engine/context/Context.java index 2384ebd6c9c..ace2b46c7e8 100644 --- a/engine/src/main/java/org/terasology/engine/context/Context.java +++ b/engine/src/main/java/org/terasology/engine/context/Context.java @@ -1,4 +1,4 @@ -// Copyright 2021 The Terasology Foundation +// Copyright 2022 The Terasology Foundation // SPDX-License-Identifier: Apache-2.0 package org.terasology.engine.context; @@ -23,7 +23,7 @@ public interface Context { /** * @return the object that is known in this context for this type. */ - T get(Class type); + T get(Class type); /** * Makes the object known in this context to be the object to work with for the given type. diff --git a/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java b/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java index 28e94772338..0751221849f 100644 --- a/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java +++ b/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java @@ -1,4 +1,4 @@ -// Copyright 2021 The Terasology Foundation +// Copyright 2022 The Terasology Foundation // SPDX-License-Identifier: Apache-2.0 package org.terasology.engine.context.internal; @@ -30,7 +30,7 @@ public ContextImpl() { } @Override - public T get(Class type) { + public T get(Class type) { if (type == Context.class) { return type.cast(this); } diff --git a/engine/src/main/java/org/terasology/engine/context/internal/MockContext.java b/engine/src/main/java/org/terasology/engine/context/internal/MockContext.java index f5b5c7ed614..90e77ab705f 100644 --- a/engine/src/main/java/org/terasology/engine/context/internal/MockContext.java +++ b/engine/src/main/java/org/terasology/engine/context/internal/MockContext.java @@ -1,4 +1,4 @@ -// Copyright 2021 The Terasology Foundation +// Copyright 2022 The Terasology Foundation // SPDX-License-Identifier: Apache-2.0 package org.terasology.engine.context.internal; @@ -6,7 +6,7 @@ public class MockContext implements Context { @Override - public T get(Class type) { + public T get(Class type) { return null; } From 24d40e49517b63b16bd3942b7d6265d508107ac5 Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Wed, 18 May 2022 12:42:44 -0700 Subject: [PATCH 2/3] feat(Context): add non-null and Optional getters --- .../terasology/engine/context/Context.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/engine/src/main/java/org/terasology/engine/context/Context.java b/engine/src/main/java/org/terasology/engine/context/Context.java index ace2b46c7e8..74eed81db2d 100644 --- a/engine/src/main/java/org/terasology/engine/context/Context.java +++ b/engine/src/main/java/org/terasology/engine/context/Context.java @@ -4,6 +4,9 @@ import org.terasology.gestalt.module.sandbox.API; +import java.util.NoSuchElementException; +import java.util.Optional; + /** * Provides classes with the utility objects that belong to the context they are running in. * @@ -21,10 +24,32 @@ public interface Context { /** - * @return the object that is known in this context for this type. + * Get the object that is known in this context for this type. */ T get(Class type); + /** + * Get the object that is known in this context for this type. Never null. + * + * @throws NoSuchElementException No instance was registered with that type. + */ + @SuppressWarnings("unused") + default T getValue(Class type) { + T value = get(type); + if (value == null) { + throw new NoSuchElementException(type.toString()); + } + return value; + } + + /** + * Get the object that is known in this context for this type. + */ + @SuppressWarnings("unused") + default Optional getMaybe(Class type) { + return Optional.ofNullable(get(type)); + } + /** * Makes the object known in this context to be the object to work with for the given type. */ From 4d6b377a181f2ea359be2ef55629db66d455078d Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Wed, 18 May 2022 13:27:02 -0700 Subject: [PATCH 3/3] chore(Context): remove redundant type Object is automatically the lower bound of `?` --- .../org/terasology/engine/context/internal/ContextImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java b/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java index 0751221849f..33c13c977d7 100644 --- a/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java +++ b/engine/src/main/java/org/terasology/engine/context/internal/ContextImpl.java @@ -13,7 +13,7 @@ public class ContextImpl implements Context { private final Context parent; - private final Map, Object> map = Maps.newConcurrentMap(); + private final Map, Object> map = Maps.newConcurrentMap(); /** @@ -41,7 +41,7 @@ public T get(Class type) { if (parent != null) { return parent.get(type); } - return result; + return null; } @Override