Skip to content

Commit 2b01ea7

Browse files
authored
chore(EntityPool): workaround "unchecked generics array creation" for getEntitiesWith (#5052)
1 parent fbc40c3 commit 2b01ea7

File tree

1 file changed

+23
-1
lines changed
  • engine/src/main/java/org/terasology/engine/entitySystem/entity

1 file changed

+23
-1
lines changed

engine/src/main/java/org/terasology/engine/entitySystem/entity/EntityPool.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Terasology Foundation
1+
// Copyright 2022 The Terasology Foundation
22
// SPDX-License-Identifier: Apache-2.0
33
package org.terasology.engine.entitySystem.entity;
44

@@ -130,10 +130,32 @@ public interface EntityPool {
130130
Iterable<EntityRef> getAllEntities();
131131

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

149+
@SuppressWarnings("unchecked")
150+
default Iterable<EntityRef> getEntitiesWith(Class<? extends Component> componentClass) {
151+
return getEntitiesWith(new Class[] {componentClass});
152+
}
153+
154+
@SuppressWarnings("unchecked")
155+
default Iterable<EntityRef> getEntitiesWith(Class<? extends Component> componentClass, Class<? extends Component> componentClass2) {
156+
return getEntitiesWith(new Class[] {componentClass, componentClass2});
157+
}
158+
137159
/**
138160
* @return A count of entities with the provided component types
139161
*/

0 commit comments

Comments
 (0)