Handle nulls if they happen

This commit is contained in:
tastybento 2024-08-03 09:03:19 -07:00
parent 0766f2967d
commit 2f055b2a8e
2 changed files with 20 additions and 1 deletions

View File

@ -538,7 +538,9 @@ public class IslandCache {
* @return list of islands * @return list of islands
*/ */
public @NonNull List<Island> getIslands(UUID uniqueId) { public @NonNull List<Island> getIslands(UUID uniqueId) {
return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById).toList(); return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById)
.filter(Objects::nonNull) // Filter out null values
.toList();
} }
/** /**

View File

@ -470,6 +470,23 @@ public class IslandCacheTest extends AbstractCommonSetup {
assertTrue(ic.getIslands(owner).isEmpty()); assertTrue(ic.getIslands(owner).isEmpty());
} }
/**
* Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#getIslands(java.util.UUID)}.
* @throws IntrospectionException
* @throws NoSuchMethodException
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@Test
public void testGetIslandsUUIDNoIslands() throws InstantiationException, IllegalAccessException,
InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException {
// Test is WIP.
when(handler.loadObject(anyString())).thenReturn(null);
assertTrue(ic.getIslands(owner).isEmpty());
}
/** /**
* Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#addIsland(world.bentobox.bentobox.database.objects.Island)}. * Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#addIsland(world.bentobox.bentobox.database.objects.Island)}.
*/ */