Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Context)!: correct signature of Context.get #5017

Merged
merged 4 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -82,7 +82,7 @@ private static class ContextImplementation implements Context {
private final Map<Class<?>, Object> map = Maps.newConcurrentMap();

@Override
public <T> T get(Class<? extends T> type) {
public <T> T get(Class<T> type) {
T result = type.cast(map.get(type));
if (result != null) {
return result;
Expand Down
31 changes: 28 additions & 3 deletions engine/src/main/java/org/terasology/engine/context/Context.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.context;

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.
*
Expand All @@ -21,9 +24,31 @@
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> T get(Class<T> 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> T getValue(Class<T> 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.
*/
<T> T get(Class<? extends T> type);
@SuppressWarnings("unused")
default <T> Optional<T> getMaybe(Class<T> type) {
return Optional.ofNullable(get(type));
}

/**
* Makes the object known in this context to be the object to work with for the given type.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,7 +13,7 @@
public class ContextImpl implements Context {
private final Context parent;

private final Map<Class<? extends Object>, Object> map = Maps.newConcurrentMap();
private final Map<Class<?>, Object> map = Maps.newConcurrentMap();


/**
Expand All @@ -30,7 +30,7 @@ public ContextImpl() {
}

@Override
public <T> T get(Class<? extends T> type) {
public <T> T get(Class<T> type) {
if (type == Context.class) {
return type.cast(this);
}
Expand All @@ -41,7 +41,7 @@ public <T> T get(Class<? extends T> type) {
if (parent != null) {
return parent.get(type);
}
return result;
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.context.internal;

import org.terasology.engine.context.Context;

public class MockContext implements Context {
@Override
public <T> T get(Class<? extends T> type) {
public <T> T get(Class<T> type) {
return null;
}

Expand Down