有人可以帮我解决我使用枚举单例实例的问题吗?实例的列表被还原或清除,我不知道如何或为什么:
public enum ObjectMap {
INSTANCE;
ObjectMap() {
this.displayObjectList = new ArrayList<DisplayObject>();
}
private final List<DisplayObject> displayObjectList;
public boolean addObject(final DisplayObject paramDisplayObject) {
boolean success = false;
if (null != paramDisplayObject && !this.isTaken(paramDisplayObject.x, paramDisplayObject.y)) {
this.displayObjectList.add(paramDisplayObject);
success = true;
}
return success;
}
public boolean isTaken(final int paramX, final int paramY) {
for (final DisplayObject displayObject : this.displayObjectList) {
if (displayObject.x == paramX && displayObject.y == paramY) {
return true;
}
}
return false;
}
}
以下junit测试失败:
@Test
public void testBombEvent() {
Player player = new Player(PLAYER_X, PLAYER_Y);
// calls ObjectMap.INSTANCE.addObject(DisplayObject bomb), debugging this successfully adds the object to the list
player.placeBomb();
// AssertionError because ObjectMap.INSTANCE.displayObjectList is empty???
Assert.assertTrue(ObjectMap.INSTANCE.isTaken(PLAYER_X, PLAYER_Y));
final DisplayObject object = ObjectMap.INSTANCE.getObject(PLAYER_X, PLAYER_Y);
Assert.assertEquals(object.objectType, GameObjectType.BOMB);
try {
Thread.sleep(2010);
Assert.assertFalse(ObjectMap.INSTANCE.isTaken(PLAYER_X, PLAYER_Y));
} catch (final InterruptedException e) {
}
}