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

chore(EntityPool): workaround "unchecked generics array creation" for getEntitiesWith #5052

Merged
merged 1 commit into from
Jun 26, 2022
Merged
Changes from all 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.entitySystem.entity;

Expand Down Expand Up @@ -130,10 +130,32 @@ public interface EntityPool {
Iterable<EntityRef> getAllEntities();

/**
* All entities containing every one of the given components.
* <p>
* Implementation note:
* <a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic5"
* title="Designing Generic Methods">Java generic types are a mess</a>, especially where
* varargs are involved. We can't use {@link SafeVarargs @SafeVarargs} on any interface methods
* because there is no way to know the <em>implementations</em> of the interface are safe.
* <p>
* In this case, in practice, there are few (if any) callers that pass more than two values.
* By adding methods that explicitly take one and two values, we can present an interface the
* compiler doesn't need to complain about at every call site.
*
* @return An iterable over all entities with the provided component types.
*/
Iterable<EntityRef> getEntitiesWith(Class<? extends Component>... componentClasses);

@SuppressWarnings("unchecked")
default Iterable<EntityRef> getEntitiesWith(Class<? extends Component> componentClass) {
return getEntitiesWith(new Class[] {componentClass});
}

@SuppressWarnings("unchecked")
default Iterable<EntityRef> getEntitiesWith(Class<? extends Component> componentClass, Class<? extends Component> componentClass2) {
return getEntitiesWith(new Class[] {componentClass, componentClass2});
}

/**
* @return A count of entities with the provided component types
*/
Expand Down