diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 5130c12bc2..d4bd628f85 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2333,7 +2334,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 0078239bed..8e9640061c 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_01.0_Ertheia/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 223825662d..08a0e50181 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2339,7 +2340,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 1774d79dfb..eae45b1f82 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_02.5_Underground/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 223825662d..08a0e50181 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2339,7 +2340,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 1774d79dfb..eae45b1f82 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_03.0_Helios/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index bdddd8ed76..3b210ff8b1 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2339,7 +2340,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 1774d79dfb..eae45b1f82 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_04.0_GrandCrusade/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 3ce1069a80..59ca4b3226 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2418,7 +2419,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index f6b56b0190..3ecaf76f2e 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_05.0_Salvation/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 70f38fce29..8f29339dbd 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2611,7 +2612,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index f6b56b0190..3ecaf76f2e 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_05.5_EtinasFate/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 70f38fce29..8f29339dbd 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2611,7 +2612,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index f6b56b0190..3ecaf76f2e 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_06.0_Fafurion/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 8af9ec44f3..72b796ae29 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2596,7 +2597,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index f6b56b0190..3ecaf76f2e 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_07.0_PreludeOfWar/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/World.java index d3e120747d..e0df75f9c3 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -403,11 +404,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -472,11 +477,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -532,11 +541,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -580,14 +593,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -614,11 +627,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -635,14 +652,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -669,11 +686,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index df69649cd7..078c12351f 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2566,7 +2567,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index aec33c3013..13fe891899 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -128,7 +128,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -164,7 +164,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -194,7 +194,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -213,7 +213,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -240,7 +240,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_08.2_Homunculus/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/EffectList.java index 4ef78e7ee9..5a46c1633a 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/World.java index d3e120747d..e0df75f9c3 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -403,11 +404,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -472,11 +477,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -532,11 +541,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -580,14 +593,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -614,11 +627,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -635,14 +652,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -669,11 +686,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index df69649cd7..078c12351f 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2566,7 +2567,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index aec33c3013..13fe891899 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -128,7 +128,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -164,7 +164,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -194,7 +194,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -213,7 +213,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -240,7 +240,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_09.2_ReturnOfTheQueenAnt/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/EffectList.java index 4ef78e7ee9..5a46c1633a 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/World.java index d3e120747d..e0df75f9c3 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -403,11 +404,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -472,11 +477,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -532,11 +541,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -580,14 +593,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -614,11 +627,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -635,14 +652,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -669,11 +686,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index aad54d95b0..50fb82f05c 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2566,7 +2567,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index aec33c3013..13fe891899 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -128,7 +128,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -164,7 +164,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -194,7 +194,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -213,7 +213,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -240,7 +240,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_10.0_MasterClass/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/World.java index b6737f51f7..34f13ded04 100644 --- a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/World.java @@ -19,6 +19,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -414,15 +415,14 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - // Remove the WorldObject from the WorldObjectHashSet(WorldObject) _knownObjects of the surrounding WorldRegion Creatures // If object is a Player, remove the WorldObject from the WorldObjectHashSet(Player) _knownPlayer of the surrounding WorldRegion Creatures // If object is targeted by one of the surrounding WorldRegion Creatures, cancel ATTACK and cast @@ -488,21 +488,20 @@ public class World } // Create a list in order to contain all visible WorldObject - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); // Go through the list of region final WorldRegion[] surroundingRegions = region.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - if (wo.equals(object)) { continue; // skip our own character @@ -554,22 +553,21 @@ public class World final int sqRadius = radius * radius; // Create a list in order to contain all visible WorldObject - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); // Go through the list of region final WorldRegion[] surroundingRegions = region.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { // Go through visible objects of the selected region - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - if (wo.equals(object)) { continue; // skip our own character @@ -619,21 +617,20 @@ public class World final int sqRadius = radius * radius; // Create a list in order to contain all visible WorldObject - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); // Go through visible object of the selected region final WorldRegion[] surroundingRegions = object.getWorldRegion().getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - if (wo.equals(object)) { continue; // skip our own character diff --git a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/WorldRegion.java index e7a098f77d..0b2df94baa 100644 --- a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,8 +17,11 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; @@ -39,19 +42,18 @@ import org.l2jmobius.gameserver.model.zone.ZoneManager; import org.l2jmobius.gameserver.model.zone.ZoneType; import org.l2jmobius.gameserver.model.zone.type.PeaceZone; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { private static final Logger LOGGER = Logger.getLogger(WorldRegion.class.getName()); - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); private final List _doors = new ArrayList<>(1); private final List _fences = new ArrayList<>(1); private WorldRegion[] _surroundingRegions; private final int _regionX; private final int _regionY; - private Boolean _active = Config.GRIDS_ALWAYS_ON; + private boolean _active = Config.GRIDS_ALWAYS_ON; private ScheduledFuture _neighborsTask = null; private final AtomicInteger _activeNeighbors = new AtomicInteger(); private ZoneManager _zoneManager; @@ -125,18 +127,17 @@ public class WorldRegion _zoneManager.onRevive(creature); } - private void switchAI(Boolean isOn) + private void switchAI(boolean isOn) { + if (_visibleObjects.isEmpty()) + { + return; + } + if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo instanceof Attackable) { final Attackable mob = (Attackable) wo; @@ -182,14 +183,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo instanceof Attackable) { // Start HP/MP/CP Regeneration task @@ -231,17 +226,22 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) { - final WorldObject wo = regionObjects.get(j); - if ((wo != null) && wo.isPlayable()) + continue; + } + + for (WorldObject wo : regionObjects) + { + if (wo.isPlayable()) { return false; } } } } + return true; } @@ -347,7 +347,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -410,7 +410,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } @@ -478,14 +478,8 @@ public class WorldRegion public synchronized void deleteVisibleNpcSpawns() { LOGGER.info("Deleting all visible NPCs in Region: " + getName()); - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo instanceof Npc) { final Npc target = (Npc) wo; diff --git a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java index aaf829e25f..0c0821018f 100644 --- a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java +++ b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java @@ -16,8 +16,8 @@ */ package org.l2jmobius.gameserver.model.itemcontainer; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import org.l2jmobius.gameserver.model.actor.Player; @@ -81,7 +81,7 @@ public class PlayerFreight extends ItemContainer @Override public Collection getItems() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item.getEquipSlot() == 0) || (item.getEquipSlot() == _activeLocationId)) diff --git a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 312cb449e9..1d29200845 100644 --- a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,18 +19,18 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import org.l2jmobius.commons.database.DatabaseFactory; import org.l2jmobius.gameserver.model.TradeList; import org.l2jmobius.gameserver.model.TradeList.TradeItem; +import org.l2jmobius.gameserver.model.WorldObject; import org.l2jmobius.gameserver.model.actor.Player; import org.l2jmobius.gameserver.model.item.ItemTemplate; import org.l2jmobius.gameserver.model.item.instance.Item; import org.l2jmobius.gameserver.model.item.instance.Item.ItemLocation; import org.l2jmobius.gameserver.model.item.type.EtcItemType; -import org.l2jmobius.gameserver.model.WorldObject; public class PlayerInventory extends Inventory { @@ -99,7 +99,7 @@ public class PlayerInventory extends Inventory public List getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable, boolean allowEquipped) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getItemId() == 57)) @@ -143,7 +143,7 @@ public class PlayerInventory extends Inventory public List getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable, boolean allowEquipped) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getItemId() == 57)) @@ -181,7 +181,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (item.getItemId() == itemId) @@ -200,7 +200,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId, int enchantment) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item.getItemId() == itemId) && (item.getEnchantLevel() == enchantment)) @@ -218,7 +218,7 @@ public class PlayerInventory extends Inventory */ public List getAvailableItems(boolean allowAdena) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, allowAdena, false)) @@ -236,7 +236,7 @@ public class PlayerInventory extends Inventory */ public List getAvailableItems(TradeList tradeList) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 631fce719f..5c5f3c8602 100644 --- a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -17,9 +17,11 @@ package org.l2jmobius.gameserver.taskmanager; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; +import java.util.LinkedList; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; @@ -28,7 +30,6 @@ import org.l2jmobius.commons.util.Chronos; import org.l2jmobius.gameserver.ai.CtrlEvent; import org.l2jmobius.gameserver.instancemanager.DayNightSpawnManager; import org.l2jmobius.gameserver.model.actor.Creature; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -45,7 +46,7 @@ public class GameTimeTaskManager protected static long _gameStartTime; protected static boolean _isNight = false; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); protected static TimerThread _timer; private final ScheduledFuture _timerWatcher; @@ -102,7 +103,7 @@ public class GameTimeTaskManager return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -119,30 +120,20 @@ public class GameTimeTaskManager */ protected void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) + final List finished = new LinkedList<>(); + for (Creature creature : _movingObjects) { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - if (creature.updatePosition(_gameTicks)) { - if (finished == null) - { - finished = new ArrayList<>(); - } finished.add(creature); } } - if (finished != null) + if (!finished.isEmpty()) { - for (int i = 0; i < finished.size(); i++) + for (Creature creature : finished) { - _movingObjects.remove(finished.get(i)); + _movingObjects.remove(creature); } ThreadPool.execute(new MovingObjectArrived(finished)); } diff --git a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_C4_ScionsOfDestiny/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/World.java index b6737f51f7..34f13ded04 100644 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/World.java @@ -19,6 +19,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -414,15 +415,14 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - // Remove the WorldObject from the WorldObjectHashSet(WorldObject) _knownObjects of the surrounding WorldRegion Creatures // If object is a Player, remove the WorldObject from the WorldObjectHashSet(Player) _knownPlayer of the surrounding WorldRegion Creatures // If object is targeted by one of the surrounding WorldRegion Creatures, cancel ATTACK and cast @@ -488,21 +488,20 @@ public class World } // Create a list in order to contain all visible WorldObject - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); // Go through the list of region final WorldRegion[] surroundingRegions = region.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - if (wo.equals(object)) { continue; // skip our own character @@ -554,22 +553,21 @@ public class World final int sqRadius = radius * radius; // Create a list in order to contain all visible WorldObject - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); // Go through the list of region final WorldRegion[] surroundingRegions = region.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { // Go through visible objects of the selected region - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - if (wo.equals(object)) { continue; // skip our own character @@ -619,21 +617,20 @@ public class World final int sqRadius = radius * radius; // Create a list in order to contain all visible WorldObject - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); // Go through visible object of the selected region final WorldRegion[] surroundingRegions = object.getWorldRegion().getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : visibleObjects) { - final WorldObject wo = visibleObjects.get(j); - if (wo == null) - { - continue; - } - if (wo.equals(object)) { continue; // skip our own character diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java index e7a098f77d..0b2df94baa 100644 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,8 +17,11 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; @@ -39,19 +42,18 @@ import org.l2jmobius.gameserver.model.zone.ZoneManager; import org.l2jmobius.gameserver.model.zone.ZoneType; import org.l2jmobius.gameserver.model.zone.type.PeaceZone; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { private static final Logger LOGGER = Logger.getLogger(WorldRegion.class.getName()); - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); private final List _doors = new ArrayList<>(1); private final List _fences = new ArrayList<>(1); private WorldRegion[] _surroundingRegions; private final int _regionX; private final int _regionY; - private Boolean _active = Config.GRIDS_ALWAYS_ON; + private boolean _active = Config.GRIDS_ALWAYS_ON; private ScheduledFuture _neighborsTask = null; private final AtomicInteger _activeNeighbors = new AtomicInteger(); private ZoneManager _zoneManager; @@ -125,18 +127,17 @@ public class WorldRegion _zoneManager.onRevive(creature); } - private void switchAI(Boolean isOn) + private void switchAI(boolean isOn) { + if (_visibleObjects.isEmpty()) + { + return; + } + if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo instanceof Attackable) { final Attackable mob = (Attackable) wo; @@ -182,14 +183,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo instanceof Attackable) { // Start HP/MP/CP Regeneration task @@ -231,17 +226,22 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) { - final WorldObject wo = regionObjects.get(j); - if ((wo != null) && wo.isPlayable()) + continue; + } + + for (WorldObject wo : regionObjects) + { + if (wo.isPlayable()) { return false; } } } } + return true; } @@ -347,7 +347,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -410,7 +410,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } @@ -478,14 +478,8 @@ public class WorldRegion public synchronized void deleteVisibleNpcSpawns() { LOGGER.info("Deleting all visible NPCs in Region: " + getName()); - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo instanceof Npc) { final Npc target = (Npc) wo; diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java index aaf829e25f..0c0821018f 100644 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java +++ b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerFreight.java @@ -16,8 +16,8 @@ */ package org.l2jmobius.gameserver.model.itemcontainer; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import org.l2jmobius.gameserver.model.actor.Player; @@ -81,7 +81,7 @@ public class PlayerFreight extends ItemContainer @Override public Collection getItems() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item.getEquipSlot() == 0) || (item.getEquipSlot() == _activeLocationId)) diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 42116361e0..b9820ba13d 100644 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,18 +19,18 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import org.l2jmobius.commons.database.DatabaseFactory; import org.l2jmobius.gameserver.model.TradeList; import org.l2jmobius.gameserver.model.TradeList.TradeItem; +import org.l2jmobius.gameserver.model.WorldObject; import org.l2jmobius.gameserver.model.actor.Player; import org.l2jmobius.gameserver.model.item.ItemTemplate; import org.l2jmobius.gameserver.model.item.instance.Item; import org.l2jmobius.gameserver.model.item.instance.Item.ItemLocation; import org.l2jmobius.gameserver.model.item.type.EtcItemType; -import org.l2jmobius.gameserver.model.WorldObject; public class PlayerInventory extends Inventory { @@ -99,7 +99,7 @@ public class PlayerInventory extends Inventory public List getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable, boolean allowEquipped) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getItemId() == 57)) @@ -143,7 +143,7 @@ public class PlayerInventory extends Inventory public List getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable, boolean allowEquipped) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getItemId() == 57)) @@ -181,7 +181,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (item.getItemId() == itemId) @@ -200,7 +200,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId, int enchantment) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item.getItemId() == itemId) && (item.getEnchantLevel() == enchantment)) @@ -218,7 +218,7 @@ public class PlayerInventory extends Inventory */ public List getAvailableItems(boolean allowAdena) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, allowAdena, false)) @@ -235,7 +235,7 @@ public class PlayerInventory extends Inventory */ public List getAugmentedItems() { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAugmented()) @@ -253,7 +253,7 @@ public class PlayerInventory extends Inventory */ public List getAvailableItems(TradeList tradeList) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 631fce719f..5c5f3c8602 100644 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -17,9 +17,11 @@ package org.l2jmobius.gameserver.taskmanager; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; +import java.util.LinkedList; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; @@ -28,7 +30,6 @@ import org.l2jmobius.commons.util.Chronos; import org.l2jmobius.gameserver.ai.CtrlEvent; import org.l2jmobius.gameserver.instancemanager.DayNightSpawnManager; import org.l2jmobius.gameserver.model.actor.Creature; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -45,7 +46,7 @@ public class GameTimeTaskManager protected static long _gameStartTime; protected static boolean _isNight = false; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); protected static TimerThread _timer; private final ScheduledFuture _timerWatcher; @@ -102,7 +103,7 @@ public class GameTimeTaskManager return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -119,30 +120,20 @@ public class GameTimeTaskManager */ protected void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) + final List finished = new LinkedList<>(); + for (Creature creature : _movingObjects) { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - if (creature.updatePosition(_gameTicks)) { - if (finished == null) - { - finished = new ArrayList<>(); - } finished.add(creature); } } - if (finished != null) + if (!finished.isEmpty()) { - for (int i = 0; i < finished.size(); i++) + for (Creature creature : finished) { - _movingObjects.remove(finished.get(i)); + _movingObjects.remove(creature); } ThreadPool.execute(new MovingObjectArrived(finished)); } diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/EffectList.java index 367dd05eed..0fa026f393 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,8 +16,8 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; @@ -159,7 +159,7 @@ public class EffectList return Collections. emptyList(); } - final List buffs = new ArrayList<>(); + final List buffs = new LinkedList<>(); if (hasBuffs()) { buffs.addAll(_buffs); @@ -179,6 +179,7 @@ public class EffectList { buffs.addAll(_debuffs); } + return buffs; } diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/World.java index f7a55b4bc2..c67bffa142 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -394,11 +395,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -463,11 +468,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -523,11 +532,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceId() != object.getInstanceId())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceId() != object.getInstanceId())) { continue; } @@ -571,14 +584,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -605,11 +618,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -626,14 +643,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -660,11 +677,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 835d5a6a9b..a528887210 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -121,7 +121,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 417f44ab88..b3e249a59f 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -124,7 +124,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -165,7 +165,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item == null) || (!allowAdena && (item.getId() == ADENA_ID)) || (!allowAncientAdena && (item.getId() == ANCIENT_ADENA_ID))) @@ -199,7 +199,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -229,7 +229,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -248,7 +248,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -274,7 +274,7 @@ public class PlayerInventory extends Inventory */ public Collection getAugmentedItems() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (item.isAugmented()) @@ -291,7 +291,7 @@ public class PlayerInventory extends Inventory */ public Collection getElementItems() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (item.getElementals() != null) @@ -309,7 +309,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) @@ -942,8 +942,8 @@ public class PlayerInventory extends Inventory } /** - * Return TIntArrayList with blocked item ids - * @return TIntArrayList + * Return int[] array with blocked item ids + * @return int[] array */ public int[] getBlockItems() { diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 000776b4aa..90455cd4eb 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,9 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -26,7 +26,6 @@ import org.l2jmobius.commons.threads.ThreadPool; import org.l2jmobius.commons.util.Chronos; import org.l2jmobius.gameserver.instancemanager.DayNightSpawnManager; import org.l2jmobius.gameserver.model.actor.Creature; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -43,7 +42,7 @@ public class GameTimeTaskManager extends Thread public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000; public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private final long _referenceTime; protected GameTimeTaskManager() @@ -102,7 +101,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -121,32 +120,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/EffectList.java index 0bcb5e2323..3d1be23aa6 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,8 +16,8 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; @@ -171,7 +171,7 @@ public class EffectList return Collections. emptyList(); } - final List buffs = new ArrayList<>(); + final List buffs = new LinkedList<>(); if (hasBuffs()) { buffs.addAll(_buffs); @@ -196,6 +196,7 @@ public class EffectList { buffs.addAll(_debuffs); } + return buffs; } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/World.java index f7a55b4bc2..c67bffa142 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -394,11 +395,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -463,11 +468,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -523,11 +532,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceId() != object.getInstanceId())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceId() != object.getInstanceId())) { continue; } @@ -571,14 +584,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -605,11 +618,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -626,14 +643,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -660,11 +677,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 835d5a6a9b..a528887210 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -121,7 +121,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 417f44ab88..b3e249a59f 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -124,7 +124,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -165,7 +165,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItemsByEnchantLevel(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if ((item == null) || (!allowAdena && (item.getId() == ADENA_ID)) || (!allowAncientAdena && (item.getId() == ANCIENT_ADENA_ID))) @@ -199,7 +199,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -229,7 +229,7 @@ public class PlayerInventory extends Inventory */ public List getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -248,7 +248,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -274,7 +274,7 @@ public class PlayerInventory extends Inventory */ public Collection getAugmentedItems() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (item.isAugmented()) @@ -291,7 +291,7 @@ public class PlayerInventory extends Inventory */ public Collection getElementItems() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (item.getElementals() != null) @@ -309,7 +309,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) @@ -942,8 +942,8 @@ public class PlayerInventory extends Inventory } /** - * Return TIntArrayList with blocked item ids - * @return TIntArrayList + * Return int[] array with blocked item ids + * @return int[] array */ public int[] getBlockItems() { diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 000776b4aa..90455cd4eb 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,9 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -26,7 +26,6 @@ import org.l2jmobius.commons.threads.ThreadPool; import org.l2jmobius.commons.util.Chronos; import org.l2jmobius.gameserver.instancemanager.DayNightSpawnManager; import org.l2jmobius.gameserver.model.actor.Creature; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -43,7 +42,7 @@ public class GameTimeTaskManager extends Thread public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000; public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private final long _referenceTime; protected GameTimeTaskManager() @@ -102,7 +101,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -121,32 +120,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 0eb0c1e02b..e96e3132d3 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2341,7 +2342,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 8251a576c8..255cc0dfce 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -139,7 +139,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -175,7 +175,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -205,7 +205,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -224,7 +224,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -251,7 +251,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 0eb0c1e02b..e96e3132d3 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2341,7 +2342,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 8251a576c8..255cc0dfce 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -139,7 +139,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -175,7 +175,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -205,7 +205,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -224,7 +224,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -251,7 +251,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index d42fb37214..f3c8527f4b 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2426,7 +2427,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index c8e13e1e2c..c627cb2c06 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -139,7 +139,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -175,7 +175,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -205,7 +205,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -224,7 +224,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -251,7 +251,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index c964594bbb..bb672eb27c 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2613,7 +2614,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index c8e13e1e2c..c627cb2c06 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -139,7 +139,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -175,7 +175,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -205,7 +205,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -224,7 +224,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -251,7 +251,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index c964594bbb..bb672eb27c 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2613,7 +2614,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index c8e13e1e2c..c627cb2c06 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -139,7 +139,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -175,7 +175,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -205,7 +205,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -224,7 +224,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -251,7 +251,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 1175df4778..b2795b68e6 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2598,7 +2599,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index c8e13e1e2c..c627cb2c06 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -139,7 +139,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -175,7 +175,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -205,7 +205,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -224,7 +224,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -251,7 +251,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 0535e0017a..439e935d24 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2333,7 +2334,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 0078239bed..8e9640061c 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -127,7 +127,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -163,7 +163,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -193,7 +193,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -212,7 +212,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -239,7 +239,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/EffectList.java index bcdf8375f2..acff7e5344 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 1bea8f90b8..5bb0dde7a5 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2694,7 +2695,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 0a83925e3c..e83ab3bbfe 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -129,7 +129,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -165,7 +165,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -195,7 +195,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -214,7 +214,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -241,7 +241,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Essence_4.2_DwellingOfSpirits/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/EffectList.java index 4ef78e7ee9..5a46c1633a 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 882255f6db..11a760b7c6 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2724,7 +2725,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 0a83925e3c..e83ab3bbfe 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -129,7 +129,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -165,7 +165,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -195,7 +195,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -214,7 +214,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -241,7 +241,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Essence_5.2_FrostLord/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -} diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/EffectList.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/EffectList.java index 4ef78e7ee9..5a46c1633a 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/EffectList.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/EffectList.java @@ -16,11 +16,11 @@ */ package org.l2jmobius.gameserver.model; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Queue; @@ -136,7 +136,7 @@ public class EffectList */ public List getBuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isBuff()) @@ -153,7 +153,7 @@ public class EffectList */ public List getDances() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDance()) @@ -170,7 +170,7 @@ public class EffectList */ public List getDebuffs() { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (BuffInfo info : _actives) { if (info.getSkill().getBuffType().isDebuff()) diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/World.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/World.java index 55a0f02229..ade7f57579 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/World.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/World.java @@ -18,6 +18,7 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -398,11 +399,15 @@ public class World final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -467,11 +472,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if (wo == object) { continue; } @@ -527,11 +536,15 @@ public class World continue; } - final List visibleObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = worldRegion.getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || (wo.getInstanceWorld() != object.getInstanceWorld())) { continue; } @@ -575,14 +588,14 @@ public class World public List getVisibleObjects(WorldObject object, Class clazz) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, result::add); return result; } public List getVisibleObjects(WorldObject object, Class clazz, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObject(object, clazz, o -> { if (predicate.test(o)) @@ -609,11 +622,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } @@ -630,14 +647,14 @@ public class World public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, result::add); return result; } public List getVisibleObjectsInRange(WorldObject object, Class clazz, int range, Predicate predicate) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); forEachVisibleObjectInRange(object, clazz, range, o -> { if (predicate.test(o)) @@ -664,11 +681,15 @@ public class World final WorldRegion[] surroundingRegions = worldRegion.getSurroundingRegions(); for (int i = 0; i < surroundingRegions.length; i++) { - final List visibleObjects = surroundingRegions[i].getVisibleObjects(); - for (int j = 0; j < visibleObjects.size(); j++) + final Collection visibleObjects = surroundingRegions[i].getVisibleObjects(); + if (visibleObjects.isEmpty()) { - final WorldObject wo = visibleObjects.get(j); - if ((wo == null) || (wo == object) || !clazz.isInstance(wo)) + continue; + } + + for (WorldObject wo : visibleObjects) + { + if ((wo == object) || !clazz.isInstance(wo)) { continue; } diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/WorldRegion.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/WorldRegion.java index c8cdd06dc0..e79930703e 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/WorldRegion.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/WorldRegion.java @@ -17,7 +17,10 @@ package org.l2jmobius.gameserver.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.atomic.AtomicInteger; @@ -28,12 +31,11 @@ import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.Door; import org.l2jmobius.gameserver.model.actor.instance.Fence; import org.l2jmobius.gameserver.taskmanager.RandomAnimationTaskManager; -import org.l2jmobius.gameserver.util.UnboundArrayList; public class WorldRegion { - /** List containing visible objects in this world region. */ - private final UnboundArrayList _visibleObjects = new UnboundArrayList<>(); + /** Set containing visible objects in this world region. */ + private final Set _visibleObjects = ConcurrentHashMap.newKeySet(); /** List containing doors in this world region. */ private final List _doors = new ArrayList<>(1); /** List containing fences in this world region. */ @@ -61,14 +63,8 @@ public class WorldRegion if (!isOn) { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { final Attackable mob = (Attackable) wo; @@ -102,14 +98,8 @@ public class WorldRegion } else { - for (int i = 0; i < _visibleObjects.size(); i++) + for (WorldObject wo : _visibleObjects) { - final WorldObject wo = _visibleObjects.get(i); - if (wo == null) - { - continue; - } - if (wo.isAttackable()) { // Start HP/MP/CP regeneration task. @@ -151,10 +141,14 @@ public class WorldRegion final WorldRegion worldRegion = _surroundingRegions[i]; if (worldRegion.isActive()) { - final List regionObjects = worldRegion.getVisibleObjects(); - for (int j = 0; j < regionObjects.size(); j++) + final Collection regionObjects = worldRegion.getVisibleObjects(); + if (regionObjects.isEmpty()) + { + continue; + } + + for (WorldObject wo : regionObjects) { - final WorldObject wo = regionObjects.get(j); if ((wo != null) && wo.isPlayable()) { return false; @@ -267,7 +261,7 @@ public class WorldRegion return; } - _visibleObjects.addIfAbsent(object); + _visibleObjects.add(object); if (object.isDoor()) { @@ -330,7 +324,7 @@ public class WorldRegion } } - public List getVisibleObjects() + public Collection getVisibleObjects() { return _visibleObjects; } diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java index 882255f6db..11a760b7c6 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/Inventory.java @@ -22,6 +22,7 @@ import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -2724,7 +2725,7 @@ public abstract class Inventory extends ItemContainer filter = filter.and(additionalFilter); } - final List items = new ArrayList<>(_paperdoll.length / (filters.length + 1)); + final List items = new LinkedList<>(); for (Item item : _paperdoll) { if (filter.test(item)) diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java index 2d1a00f461..6b78509b47 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/ItemContainer.java @@ -19,8 +19,8 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -106,7 +106,7 @@ public abstract class ItemContainer */ public Collection getAllItemsByItemId(int itemId) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (itemId == item.getId()) diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java index 0a83925e3c..e83ab3bbfe 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/model/itemcontainer/PlayerInventory.java @@ -19,9 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -129,7 +129,7 @@ public class PlayerInventory extends Inventory public Collection getUniqueItems(boolean allowAdena, boolean allowAncientAdena, boolean onlyAvailable) { - final List list = new ArrayList<>(); + final List list = new LinkedList<>(); for (Item item : _items) { if (!allowAdena && (item.getId() == ADENA_ID)) @@ -165,7 +165,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped())) @@ -195,7 +195,7 @@ public class PlayerInventory extends Inventory */ public Collection getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped())) @@ -214,7 +214,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId())) @@ -241,7 +241,7 @@ public class PlayerInventory extends Inventory */ public Collection getAvailableItems(TradeList tradeList) { - final List result = new ArrayList<>(); + final List result = new LinkedList<>(); for (Item item : _items) { if ((item != null) && item.isAvailable(_owner, false, false)) diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java index 99e3760779..6abf61187a 100644 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java +++ b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/taskmanager/GameTimeTaskManager.java @@ -16,9 +16,7 @@ */ package org.l2jmobius.gameserver.taskmanager; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -30,7 +28,6 @@ import org.l2jmobius.gameserver.model.events.EventDispatcher; import org.l2jmobius.gameserver.model.events.impl.OnDayNightChange; import org.l2jmobius.gameserver.network.SystemMessageId; import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; -import org.l2jmobius.gameserver.util.UnboundArrayList; /** * Game Time task manager class. @@ -48,7 +45,7 @@ public class GameTimeTaskManager extends Thread public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND; private static final int SHADOW_SENSE_ID = 294; - private static final UnboundArrayList _movingObjects = new UnboundArrayList<>(); + private static final Set _movingObjects = ConcurrentHashMap.newKeySet(); private static final Set _shadowSenseCharacters = ConcurrentHashMap.newKeySet(); private final long _referenceTime; @@ -108,7 +105,7 @@ public class GameTimeTaskManager extends Thread return; } - _movingObjects.addIfAbsent(creature); + _movingObjects.add(creature); } /** @@ -127,32 +124,7 @@ public class GameTimeTaskManager extends Thread */ private void moveObjects() { - List finished = null; - for (int i = 0; i < _movingObjects.size(); i++) - { - final Creature creature = _movingObjects.get(i); - if (creature == null) - { - continue; - } - - if (creature.updatePosition()) - { - if (finished == null) - { - finished = new ArrayList<>(); - } - finished.add(creature); - } - } - - if (finished != null) - { - for (int i = 0; i < finished.size(); i++) - { - _movingObjects.remove(finished.get(i)); - } - } + _movingObjects.removeIf(Creature::updatePosition); } public void stopTimer() diff --git a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/util/UnboundArrayList.java b/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/util/UnboundArrayList.java deleted file mode 100644 index d40c10778a..0000000000 --- a/L2J_Mobius_Essence_6.0_BattleChronicle/java/org/l2jmobius/gameserver/util/UnboundArrayList.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (c) 2020 Pantelis Andrianakis - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.l2jmobius.gameserver.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Consumer; - -/** - * Unbound and synchronized implementation of the {@code ArrayList} class.
- * In addition to implementing the {@code ArrayList} class,
- * this class provides synchronization on adding and removing elements.
- * Further more, get() does not throw IndexOutOfBoundsException, a {@code null} element is returned instead.
- * An equivalent Iterator constructor is introduced under the same logic. - * @param the type of elements in this list - * @version September 4th 2020 - * @author Pantelis Andrianakis - */ -public class UnboundArrayListextends ArrayList -{ - /** - * Returns the element at the specified position in this list. - * @param index index of the element to return - * @return the element at the specified position in this list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E get(int index) - { - E element = null; - - try - { - // The try performance impact itself is insignificant. - // Benchmark test on an i7-2600 machine with a list of 100.000.000 random elements. - // get: Execution time was 116 milliseconds. - // get with try: Execution time was 124 milliseconds. - element = super.get(index); - } - catch (Exception e) - { - // Continue with execution. - // This impacts performance only if the Exception is created. - // Benchmark test on an i7-2600 machine resulted with a 187 millisecond total delay, - // in a list of 10.000 elements, with IndexOutOfBoundsException thrown on 5.000 of them. - } - - return element; - } - - /** - * Replaces the element at the specified position in this list with the specified element. - * @param index index of the element to replace - * @param element element to be stored at the specified position - * @return the element previously at the specified position,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E set(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.set(index, element); - } - return null; - } - } - - /** - * Appends the specified element to the end of this list. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - @Override - public boolean add(E e) - { - synchronized (this) - { - return super.add(e); - } - } - - /** - * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). - * @param index index at which the specified element is to be inserted - * @param element element to be inserted - */ - @Override - public void add(int index, E element) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - super.add(index, element); - } - else - { - super.add(element); - } - } - } - - /** - * Appends the specified element to the end of this list if this list does not contain the specified element. - * @param e element to be appended to this list - * @return {@code true} (as specified by {@link Collection#add}) - */ - public boolean addIfAbsent(E e) - { - synchronized (this) - { - if (!contains(e)) - { - return super.add(e); - } - return false; - } - } - - /** - * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that {@code Objects.equals(o, get(i))} (if such an element exists). - * Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call). - * @param o element to be removed from this list, if present - * @return {@code true} if this list contained the specified element - */ - @Override - public boolean remove(Object o) - { - synchronized (this) - { - return super.remove(o); - } - } - - /** - * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). - * @param index the index of the element to be removed - * @return the element that was removed from the list,
- * or {@code null} if this list does not have this specified position. - */ - @Override - public E remove(int index) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.remove(index); - } - return null; - } - } - - /** - * Removes all of the elements from this list.
- * The list will be empty after this call returns. - */ - @Override - public void clear() - { - synchronized (this) - { - super.clear(); - } - } - - /** - * Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies - * that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.) - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(Collection c) - { - synchronized (this) - { - return super.addAll(c); - } - } - - /** - * Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that - * they are returned by the specified collection's iterator. - * @param index index at which to insert the first element from the specified collection - * @param c collection containing elements to be added to this list - * @return {@code true} if this list changed as a result of the call - * @throws NullPointerException if the specified collection is null - */ - @Override - public boolean addAll(int index, Collection c) - { - synchronized (this) - { - if ((index >= 0) && (index < size())) - { - return super.addAll(index, c); - } - return super.addAll(c); - } - } - - /** - * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by {@code (toIndex - fromIndex)} elements. - */ - @Override - protected void removeRange(int fromIndex, int toIndex) - { - synchronized (this) - { - if (fromIndex > size()) - { - return; - } - super.removeRange(fromIndex < 0 ? 0 : fromIndex, size() < toIndex ? size() - 1 : toIndex); - } - } - - /** - * Removes from this list all of its elements that are contained in the specified collection. - * @param c collection containing elements to be removed from this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean removeAll(Collection c) - { - synchronized (this) - { - return super.removeAll(c); - } - } - - /** - * Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection. - * @param c collection containing elements to be retained in this list - * @return {@code true} if this list changed as a result of the call - * @throws ClassCastException if the class of an element of this list is incompatible with the specified collection (optional) - * @throws NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null - * @see Collection#contains(Object) - */ - @Override - public boolean retainAll(Collection c) - { - synchronized (this) - { - return super.retainAll(c); - } - } - - /** - * Returns an iterator over the elements in this list in proper sequence. - * @return an iterator over the elements in this list in proper sequence.
- * Non existing elements are returned as null. - */ - @Override - public Iterator iterator() - { - return new Itr(); - } - - /** - * An optimized version of AbstractList.Itr - */ - private class Itr implements Iterator - { - private int _cursor = 0; // index of next element to return - - // prevent creating a synthetic constructor - Itr() - { - } - - @Override - public boolean hasNext() - { - return _cursor < size(); - } - - @Override - public E next() - { - return get(_cursor++); - } - - @Override - public void remove() - { - UnboundArrayList.this.remove(get(_cursor - 1)); - } - - @Override - public void forEachRemaining(Consumer action) - { - if (action == null) - { - return; - } - for (int i = _cursor; i < size(); i++) - { - final E next = get(i); - if (next != null) - { - action.accept(next); - } - _cursor++; - } - } - } -}