Skip to content

Commit

Permalink
Improve WrappedDataWatcher hasIndex performance
Browse files Browse the repository at this point in the history
Use the map to check for indices instead of getting the object

Addresses #850
  • Loading branch information
dmulloy2 committed May 28, 2020
1 parent e92abda commit 54c252a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public WrappedWatchableObject remove(int index) {
* @return True if it does, false if not
*/
public boolean hasIndex(int index) {
return getObject(index) != null;
return getMap().containsKey(index);
}

/**
Expand Down Expand Up @@ -388,7 +388,7 @@ public Object getObject(WrappedDataWatcherObject object) {
* @param value New value
* @param update Whether or not to inform the client
*
* @see {@link #setObject(WrappedDataWatcherObject, Object, boolean)}
* @see WrappedDataWatcher#setObject(WrappedDataWatcherObject, Object, boolean)
* @throws IllegalArgumentException in 1.9 and up if there isn't already an
* object at this index
*/
Expand All @@ -415,7 +415,7 @@ public void setObject(int index, Object value) {
* @param value New value
* @param update Whether or not to inform the client
*
* @see {@link #setObject(WrappedDataWatcherObject, Object)}
* @see WrappedDataWatcher#setObject(WrappedDataWatcherObject, Object)
*/
public void setObject(int index, Serializer serializer, Object value, boolean update) {
setObject(new WrappedDataWatcherObject(index, serializer), value, update);
Expand All @@ -435,7 +435,7 @@ public void setObject(int index, Serializer serializer, Object value) {
* @param value New value
* @param update Whether or not to inform the client
*
* @see {@link #setObject(int, Object, boolean)}
* @see WrappedDataWatcher#setObject(int, Object, boolean)
*/
public void setObject(int index, WrappedWatchableObject value, boolean update) {
setObject(index, value.getRawValue(), update);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WrappedDataWatcherTest {

@BeforeClass
public static void prepare() {
BukkitInitialization.initializePackage();
BukkitInitialization.initializeItemMeta();
}

@Test
Expand Down Expand Up @@ -94,4 +94,14 @@ public void testSerializers() {
// assertNull(Registry.get(ItemStack.class, false));
assertNotNull(Registry.get(UUID.class, true));
}

@Test
public void testHasIndex() {
WrappedDataWatcher watcher = new WrappedDataWatcher();
Serializer serializer = Registry.get(Integer.class);

assertFalse(watcher.hasIndex(0));
watcher.setObject(0, serializer, 1);
assertTrue(watcher.hasIndex(0));
}
}

0 comments on commit 54c252a

Please sign in to comment.