Преглед на файлове

BETA: Minor tweak of methods in `NpcData` to use Lambdas.
* Reviewed by: Nos, Zoey76

Rumen Nikiforov преди 11 години
родител
ревизия
0e3a5c3999

+ 26 - 50
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/NpcData.java

@@ -31,6 +31,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Level;
+import java.util.stream.Collectors;
 
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -50,6 +51,7 @@ import com.l2jserver.gameserver.model.drops.IDropItem;
 import com.l2jserver.gameserver.model.effects.L2EffectType;
 import com.l2jserver.gameserver.model.holders.SkillHolder;
 import com.l2jserver.gameserver.model.skills.Skill;
+import com.l2jserver.gameserver.util.Util;
 
 /**
  * NPC data parser.
@@ -717,18 +719,11 @@ public class NpcData extends DocumentParser
 	 */
 	public List<L2NpcTemplate> getAllOfLevel(int... lvls)
 	{
-		final List<L2NpcTemplate> list = new ArrayList<>();
-		for (int lvl : lvls)
-		{
-			for (L2NpcTemplate t : _npcs.values())
-			{
-				if (t.getLevel() == lvl)
-				{
-					list.add(t);
-				}
-			}
-		}
-		return list;
+		//@formatter:off
+		return _npcs.values().stream()
+			.filter(template -> Util.contains(lvls, template.getLevel()))
+			.collect(Collectors.toList());
+		//@formatter:on
 	}
 	
 	/**
@@ -738,18 +733,12 @@ public class NpcData extends DocumentParser
 	 */
 	public List<L2NpcTemplate> getAllMonstersOfLevel(int... lvls)
 	{
-		final List<L2NpcTemplate> list = new ArrayList<>();
-		for (int lvl : lvls)
-		{
-			for (L2NpcTemplate t : _npcs.values())
-			{
-				if ((t.getLevel() == lvl) && t.isType("L2Monster"))
-				{
-					list.add(t);
-				}
-			}
-		}
-		return list;
+		//@formatter:off
+		return _npcs.values().stream()
+			.filter(template -> Util.contains(lvls, template.getLevel()))
+			.filter(template -> template.isType("L2Monster"))
+			.collect(Collectors.toList());
+		//@formatter:on
 	}
 	
 	/**
@@ -759,18 +748,12 @@ public class NpcData extends DocumentParser
 	 */
 	public List<L2NpcTemplate> getAllNpcStartingWith(String... letters)
 	{
-		final List<L2NpcTemplate> list = new ArrayList<>();
-		for (String letter : letters)
-		{
-			for (L2NpcTemplate t : _npcs.values())
-			{
-				if (t.getName().startsWith(letter) && t.isType("L2Npc"))
-				{
-					list.add(t);
-				}
-			}
-		}
-		return list;
+		//@formatter:off
+		return _npcs.values().stream()
+			.filter(template -> Util.startsWith(letters, template.getName()))
+			.filter(template -> template.isType("L2Npc"))
+			.collect(Collectors.toList());
+		//@formatter:on
 	}
 	
 	/**
@@ -780,30 +763,23 @@ public class NpcData extends DocumentParser
 	 */
 	public List<L2NpcTemplate> getAllNpcOfClassType(String... classTypes)
 	{
-		final List<L2NpcTemplate> list = new ArrayList<>();
-		for (String classType : classTypes)
-		{
-			for (L2NpcTemplate t : _npcs.values())
-			{
-				if (t.isType(classType))
-				{
-					list.add(t);
-				}
-			}
-		}
-		return list;
+		//@formatter:off
+		return _npcs.values().stream()
+			.filter(template -> Util.contains(classTypes, template.getType(), true))
+			.collect(Collectors.toList());
+		//@formatter:on
 	}
 	
 	public void loadNpcsSkillLearn()
 	{
-		for (L2NpcTemplate template : _npcs.values())
+		_npcs.values().forEach(template ->
 		{
 			final List<ClassId> teachInfo = SkillLearnData.getInstance().getSkillLearnData(template.getId());
 			if (teachInfo != null)
 			{
 				template.addTeachInfo(teachInfo);
 			}
-		}
+		});
 	}
 	
 	public void loadMinions()

+ 35 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/util/Util.java

@@ -968,4 +968,39 @@ public final class Util
 	{
 		return (input < min) ? min : (input > max) ? max : input;
 	}
+	
+	/**
+	 * @param array - the array to look into
+	 * @param obj - the object to search for
+	 * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise.
+	 */
+	public static boolean startsWith(String[] array, String obj)
+	{
+		for (String element : array)
+		{
+			if (element.startsWith(obj))
+			{
+				return true;
+			}
+		}
+		return false;
+	}
+	
+	/**
+	 * @param array - the array to look into
+	 * @param obj - the object to search for
+	 * @param ignoreCase
+	 * @return {@code true} if the {@code array} contains the {@code obj}, {@code false} otherwise.
+	 */
+	public static boolean contains(String[] array, String obj, boolean ignoreCase)
+	{
+		for (String element : array)
+		{
+			if (element.equals(obj) || (ignoreCase && element.equalsIgnoreCase(obj)))
+			{
+				return true;
+			}
+		}
+		return false;
+	}
 }