Deprecate ByIdArray and pull in trove for better NPC storage

This commit is contained in:
fullwall 2013-11-12 12:01:42 +08:00
parent a80f9c6cf4
commit c145917194
4 changed files with 11 additions and 94 deletions

View File

@ -63,6 +63,12 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>

View File

@ -1,5 +1,7 @@
package net.citizensnpcs.npc;
import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.Iterator;
import net.citizensnpcs.api.event.DespawnReason;
@ -9,7 +11,6 @@ import net.citizensnpcs.api.npc.NPCDataStore;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit;
@ -20,7 +21,7 @@ import org.bukkit.entity.LivingEntity;
import com.google.common.base.Preconditions;
public class CitizensNPCRegistry implements NPCRegistry {
private final ByIdArray<NPC> npcs = new ByIdArray<NPC>();
private final TIntObjectHashMap<NPC> npcs = new TIntObjectHashMap<NPC>();
private final NPCDataStore saves;
public CitizensNPCRegistry(NPCDataStore store) {
@ -103,6 +104,6 @@ public class CitizensNPCRegistry implements NPCRegistry {
@Override
public Iterator<NPC> iterator() {
return npcs.iterator();
return npcs.valueCollection().iterator();
}
}

View File

@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
@Deprecated
public class ByIdArray<T> implements Iterable<T> {
private final int capacity;
private Object[] elementData;

View File

@ -1,91 +0,0 @@
package net.citizensnpcs.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Iterator;
import org.junit.Test;
import com.google.common.collect.Iterables;
public class ByIdArrayTest {
private void assertSize(ByIdArray<?> array, int size) {
assertThat(array.size(), is(size));
assertThat(Iterables.size(array), is(size));
}
@Test
public void testBeyondCapacity() {
ByIdArray<Integer> array = ByIdArray.create();
array.put(1000, 1);
assertThat(array.contains(1000), is(true));
assertSize(array, 1);
}
@Test
public void testClear() {
ByIdArray<Integer> array = ByIdArray.create();
array.put(0, 1);
array.put(1, 2);
array.clear();
assertSize(array, 0);
}
@Test
public void testContains() {
ByIdArray<Integer> array = ByIdArray.create();
array.put(1, 1);
assertThat(array.contains(1), is(true));
}
@Test
public void testInsertion() {
ByIdArray<Integer> array = ByIdArray.create();
array.add(1);
array.add(2);
assertSize(array, 2);
}
@Test
public void testIteratorRemove() {
ByIdArray<Integer> array = ByIdArray.create();
array.put(10, 1);
array.put(20, 2);
array.put(30, 3);
Iterator<Integer> itr = array.iterator();
itr.next();
itr.remove();
itr.next();
assertSize(array, 2);
assertThat(array.contains(10), is(false));
assertThat(array.get(20), is(2));
assertThat(array.get(30), is(3));
itr = array.iterator();
while (itr.hasNext()) {
itr.next();
itr.remove();
}
assertSize(array, 0);
}
@Test
public void testPut() {
ByIdArray<Integer> array = ByIdArray.create();
array.put(50, 1);
array.put(20, 2);
assertSize(array, 2);
assertThat(array.get(20), is(2));
assertThat(array.get(50), is(1));
}
@Test
public void testRemoval() {
ByIdArray<Integer> array = ByIdArray.create();
array.put(0, 1);
array.put(1, 2);
array.remove(1);
assertSize(array, 1);
}
}