diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/Game.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/Game.java index d97c50fef..32091f118 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/Game.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/Game.java @@ -42,7 +42,7 @@ import de.gurkenlabs.litiengine.sound.SoundEngine; import de.gurkenlabs.litiengine.sound.SoundPlayback; import de.gurkenlabs.litiengine.tweening.TweenEngine; -import de.gurkenlabs.litiengine.util.ArrayUtilities; + import de.gurkenlabs.litiengine.util.io.XmlUtilities; /*** @@ -723,11 +723,11 @@ private static void handleCommandLineArguments(String[] args) { return; } - if (ArrayUtilities.contains(args, COMMANDLINE_ARG_RELEASE, true)) { + if (Arrays.stream(args).anyMatch(arg -> COMMANDLINE_ARG_RELEASE.equalsIgnoreCase(arg))) { allowDebug(false); } - if (ArrayUtilities.contains(args, COMMANDLINE_ARG_NOGUI, true)) { + if (Arrays.stream(args).anyMatch(arg -> COMMANDLINE_ARG_NOGUI.equalsIgnoreCase(arg))) { hideGUI(true); } } diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/GameRandom.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/GameRandom.java index 4cacf204b..349cb9ac1 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/GameRandom.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/GameRandom.java @@ -2,7 +2,7 @@ import de.gurkenlabs.litiengine.entities.IEntity; import de.gurkenlabs.litiengine.environment.tilemap.IMap; -import de.gurkenlabs.litiengine.util.ArrayUtilities; + import de.gurkenlabs.litiengine.util.geom.GeometricUtilities; import java.awt.Color; import java.awt.geom.Ellipse2D; @@ -10,6 +10,7 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -47,7 +48,7 @@ public T[] sample(final T[] array, int amount, boolean replacement) { T[] sampled = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), amount); if (!replacement) { - T[] copiedArray = ArrayUtilities.arrayCopy(array); + T[] copiedArray = Arrays.copyOf(array, array.length); this.shuffle(copiedArray); // Use System and arraycopy to copy the array diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/EmitterMapObjectLoader.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/EmitterMapObjectLoader.java index 3468005fd..7480d23c0 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/EmitterMapObjectLoader.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/EmitterMapObjectLoader.java @@ -14,7 +14,7 @@ import de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData; import de.gurkenlabs.litiengine.graphics.emitters.xml.ParticleParameter; import de.gurkenlabs.litiengine.physics.Collision; -import de.gurkenlabs.litiengine.util.ArrayUtilities; + import de.gurkenlabs.litiengine.util.ColorHelper; import java.util.ArrayList; import java.util.Collection; @@ -120,7 +120,7 @@ public static void updateMapObject(EmitterData emitterData, IMapObject mo) { mo.setValue(MapObjectProperty.Emitter.ORIGIN_ALIGN, emitterData.getOriginAlign()); mo.setValue(MapObjectProperty.Emitter.ORIGIN_VALIGN, emitterData.getOriginValign()); - String commaSeperatedColors = ArrayUtilities.join(emitterData.getColors()); + String commaSeperatedColors = String.join(",", emitterData.getColors()); mo.setValue(MapObjectProperty.Emitter.COLORS, commaSeperatedColors); mo.setValue(MapObjectProperty.Particle.ACCELERATION_X_MAX, emitterData.getAccelerationX().getMaxValue()); @@ -154,7 +154,7 @@ public static void updateMapObject(EmitterData emitterData, IMapObject mo) { mo.setValue(MapObjectProperty.COLLISION_TYPE, emitterData.getCollision()); mo.setValue(MapObjectProperty.REQUIRED_QUALITY, emitterData.getRequiredQuality()); - String commaSeperatedTexts = ArrayUtilities.join(emitterData.getTexts()); + String commaSeperatedTexts = String.join(",", emitterData.getTexts()); mo.setValue(MapObjectProperty.Particle.TEXTS, commaSeperatedTexts); mo.setValue(MapObjectProperty.SPRITESHEETNAME, emitterData.getSpritesheet()); diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/MapObjectSerializer.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/MapObjectSerializer.java index 028da0735..4de86b03c 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/MapObjectSerializer.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/MapObjectSerializer.java @@ -7,11 +7,12 @@ import de.gurkenlabs.litiengine.environment.tilemap.TmxType; import de.gurkenlabs.litiengine.environment.tilemap.xml.DecimalFloatAdapter; import de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import java.lang.reflect.Field; +import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; public final class MapObjectSerializer { private static final Logger log = Logger.getLogger(MapObjectSerializer.class.getName()); @@ -35,7 +36,7 @@ public static MapObject serialize(IEntity entity) { serialize(entity.getClass(), entity, obj); if (!entity.getTags().isEmpty()) { - obj.setValue(MapObjectProperty.TAGS, ArrayUtilities.join(entity.getTags())); + obj.setValue(MapObjectProperty.TAGS, String.join(",", entity.getTags())); } return obj; @@ -78,46 +79,58 @@ private static void serialize(Field field, Object entity, IMapObject mapObject) } private static String getPropertyValue(Field field, Object value) { - if (field.getType().equals(Float.class) || field.getType().equals(Double.class)) { + Class type = field.getType(); + if (type.equals(Float.class) || type.equals(Double.class)) { try { return new DecimalFloatAdapter().marshal((Float) value); } catch (Exception e) { log.log(Level.SEVERE, e.getMessage(), e); } - } else if (field.getType().equals(Integer.class)) { - return Integer.toString((int) value); - } else if (field.getType().equals(short.class)) { - return Short.toString((short) value); - } else if (field.getType().equals(byte.class)) { - return Byte.toString((byte) value); - } else if (field.getType().equals(long.class)) { - return Long.toString((long) value); } - if (value instanceof List) { - return ArrayUtilities.join((List) value); - // special handling + if (value instanceof List list) { + return list.stream().map(String::valueOf).collect(Collectors.joining(",")); } if (value.getClass().isArray()) { - if (field.getType().getComponentType() == int.class) { - return ArrayUtilities.join((int[]) value); - } else if (field.getType().getComponentType() == double.class) { - return ArrayUtilities.join((double[]) value); - } else if (field.getType().getComponentType() == float.class) { - return ArrayUtilities.join((float[]) value); - } else if (field.getType().getComponentType() == short.class) { - return ArrayUtilities.join((short[]) value); - } else if (field.getType().getComponentType() == byte.class) { - return ArrayUtilities.join((byte[]) value); - } else if (field.getType().getComponentType() == long.class) { - return ArrayUtilities.join((long[]) value); - } else if (field.getType().getComponentType() == String.class) { - return ArrayUtilities.join((String[]) value); - } else if (field.getType().getComponentType() == boolean.class) { - return ArrayUtilities.join((boolean[]) value); + if (type.getComponentType() == int.class) { + return Arrays.stream((int[]) value).mapToObj(String::valueOf).collect(Collectors.joining(",")); + } else if (type.getComponentType() == double.class) { + return Arrays.stream((double[]) value).mapToObj(String::valueOf).collect(Collectors.joining(",")); + } else if (type.getComponentType() == float.class) { + String[] arr = new String[((float[]) value).length]; + float[] floatArr = (float[]) value; + for (int i = 0; i < arr.length; i++) { + arr[i] = String.valueOf(floatArr[i]); + } + return String.join(",", arr); + } else if (type.getComponentType() == short.class) { + String[] arr = new String[((short[]) value).length]; + short[] shortArr = (short[]) value; + for (int i = 0; i < arr.length; i++) { + arr[i] = String.valueOf(shortArr[i]); + } + return String.join(",", arr); + } else if (type.getComponentType() == byte.class) { + String[] arr = new String[((byte[]) value).length]; + byte[] byteArr = (byte[]) value; + for (int i = 0; i < arr.length; i++) { + arr[i] = String.valueOf(byteArr[i]); + } + return String.join(",", arr); + } else if (type.getComponentType() == long.class) { + return Arrays.stream((long[]) value).mapToObj(String::valueOf).collect(Collectors.joining(",")); + } else if (type.getComponentType() == String.class) { + return String.join(",", (String[]) value); + } else if (type.getComponentType() == boolean.class) { + String[] arr = new String[((boolean[]) value).length]; + boolean[] boolArr = (boolean[]) value; + for (int i = 0; i < arr.length; i++) { + arr[i] = String.valueOf(boolArr[i]); + } + return String.join(",", arr); } else { - return ArrayUtilities.join((Object[]) value); + return Arrays.stream((Object[]) value).map(String::valueOf).collect(Collectors.joining(",")); } } diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/TriggerMapObjectLoader.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/TriggerMapObjectLoader.java index 188b5951e..f3ce879e7 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/TriggerMapObjectLoader.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/TriggerMapObjectLoader.java @@ -1,6 +1,7 @@ package de.gurkenlabs.litiengine.environment; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import de.gurkenlabs.litiengine.entities.IEntity; @@ -9,7 +10,6 @@ import de.gurkenlabs.litiengine.environment.tilemap.IMapObject; import de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty; import de.gurkenlabs.litiengine.environment.tilemap.MapObjectType; -import de.gurkenlabs.litiengine.util.ArrayUtilities; public class TriggerMapObjectLoader extends MapObjectLoader { @@ -44,27 +44,19 @@ protected Trigger createTrigger(IMapObject mapObject, TriggerActivation act, Str protected void loadTargets(IMapObject mapObject, Trigger trigger) { final String targets = mapObject.getStringValue(MapObjectProperty.TRIGGER_TARGETS, null); - if (targets == null) { + if (targets == null || targets.isEmpty()) { return; } - for (final int target : ArrayUtilities.splitInt(targets)) { - if (target != 0) { - trigger.addTarget(target); - } - } + Arrays.stream(targets.split(",")).mapToInt(Integer::parseInt).filter(target -> target != 0).forEach(trigger::addTarget); } protected void loadActivators(IMapObject mapObject, Trigger trigger) { final String activators = mapObject.getStringValue(MapObjectProperty.TRIGGER_ACTIVATORS, null); - if (activators == null) { + if (activators == null || activators.isEmpty()) { return; } - for (final int activator : ArrayUtilities.splitInt(activators)) { - if (activator != 0) { - trigger.addActivator(activator); - } - } + Arrays.stream(activators.split(",")).mapToInt(Integer::parseInt).filter(activator -> activator != 0).forEach(trigger::addActivator); } } diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/IntegerArrayAdapter.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/IntegerArrayAdapter.java index b3f020ce0..14a91e806 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/IntegerArrayAdapter.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/IntegerArrayAdapter.java @@ -1,17 +1,20 @@ package de.gurkenlabs.litiengine.environment.tilemap.xml; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import jakarta.xml.bind.annotation.adapters.XmlAdapter; - +import java.util.Arrays; +import java.util.stream.Collectors; public class IntegerArrayAdapter extends XmlAdapter { @Override public int[] unmarshal(String v) throws Exception { - return ArrayUtilities.splitInt(v); + if (v == null || v.isEmpty()) { + return new int[0]; + } + return Arrays.stream(v.split(",")).mapToInt(Integer::parseInt).toArray(); } @Override public String marshal(int[] v) throws Exception { - return ArrayUtilities.join(v); + return Arrays.stream(v).mapToObj(String::valueOf).collect(Collectors.joining(",")); } } diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/TileData.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/TileData.java index bee8a92ad..be851c88d 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/TileData.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/environment/tilemap/xml/TileData.java @@ -1,6 +1,5 @@ package de.gurkenlabs.litiengine.environment.tilemap.xml; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import de.gurkenlabs.litiengine.util.io.Codec; import jakarta.xml.bind.DatatypeConverter; import jakarta.xml.bind.Unmarshaller; @@ -14,11 +13,13 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import java.util.zip.DeflaterOutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -638,7 +639,7 @@ private List parseChunkData() throws InvalidTileLayerException { } } - return ArrayUtilities.toList(tileArr); + return Arrays.stream(tileArr).flatMap(Arrays::stream).collect(Collectors.toList()); } /** diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/physics/PhysicsEngine.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/physics/PhysicsEngine.java index db4487c09..073e8cd38 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/physics/PhysicsEngine.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/physics/PhysicsEngine.java @@ -5,12 +5,12 @@ import de.gurkenlabs.litiengine.IUpdateable; import de.gurkenlabs.litiengine.entities.ICollisionEntity; import de.gurkenlabs.litiengine.entities.IMobileEntity; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import de.gurkenlabs.litiengine.util.geom.GeometricUtilities; import java.awt.Shape; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -745,7 +745,7 @@ private Intersection getIntersection(final ICollisionEntity entity, final Rectan result = new Intersection( intersection.createUnion(result), - ArrayUtilities.append(result.involvedEntities, otherEntity)); + appendToArray(result.involvedEntities, otherEntity)); } else { result = new Intersection(intersection, otherEntity); } @@ -896,7 +896,7 @@ private static void fireCollisionEvents( continue; } - involvedEntities = ArrayUtilities.distinct(involvedEntities, inter.involvedEntities); + involvedEntities = Stream.concat(Arrays.stream(involvedEntities), Arrays.stream(inter.involvedEntities)).distinct().toArray(ICollisionEntity[]::new); } // 1. fire collision event on the collider with all the involved entities @@ -923,4 +923,10 @@ public Intersection(Rectangle2D rect, ICollisionEntity... entities) { this.involvedEntities = entities; } } + + private static ICollisionEntity[] appendToArray(ICollisionEntity[] arr, ICollisionEntity element) { + ICollisionEntity[] newArray = Arrays.copyOf(arr, arr.length + 1); + newArray[arr.length] = element; + return newArray; + } } diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/SpritesheetResource.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/SpritesheetResource.java index a816bf375..ffed07eb8 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/SpritesheetResource.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/SpritesheetResource.java @@ -1,7 +1,6 @@ package de.gurkenlabs.litiengine.resources; import de.gurkenlabs.litiengine.graphics.Spritesheet; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import de.gurkenlabs.litiengine.util.io.Codec; import jakarta.xml.bind.Marshaller; import jakarta.xml.bind.annotation.XmlAttribute; @@ -10,6 +9,8 @@ import jakarta.xml.bind.annotation.XmlTransient; import java.awt.image.BufferedImage; import java.io.Serializable; +import java.util.Arrays; +import java.util.stream.Collectors; /** * Represents a resource for managing spritesheets. @@ -146,7 +147,7 @@ public int[] getKeyframes() { return new int[0]; } - return ArrayUtilities.splitInt(this.keyframes); + return Arrays.stream(this.keyframes.split(",")).mapToInt(Integer::parseInt).toArray(); } /** @@ -191,7 +192,7 @@ public void setImageFormat(final ImageFormat f) { * @param keyframes An array of keyframes to set. */ public void setKeyframes(int[] keyframes) { - this.keyframes = ArrayUtilities.join(keyframes); + this.keyframes = Arrays.stream(keyframes).mapToObj(String::valueOf).collect(Collectors.joining(",")); } /** diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/Spritesheets.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/Spritesheets.java index 50b5e2d7b..e05dc51d0 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/Spritesheets.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/resources/Spritesheets.java @@ -2,7 +2,7 @@ import de.gurkenlabs.litiengine.environment.tilemap.ITileset; import de.gurkenlabs.litiengine.graphics.Spritesheet; -import de.gurkenlabs.litiengine.util.ArrayUtilities; + import de.gurkenlabs.litiengine.util.io.Codec; import de.gurkenlabs.litiengine.util.io.FileUtilities; import java.awt.image.BufferedImage; @@ -23,6 +23,7 @@ import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import javax.imageio.ImageIO; /** @@ -274,7 +275,7 @@ public boolean saveTo(final String spriteInfoFile, boolean metadataOnly) { // print keyframes (if they exist) if (keyFrames.length > 0) { writer.write(";"); - writer.write(ArrayUtilities.join(keyFrames)); + writer.write(Arrays.stream(keyFrames).mapToObj(String::valueOf).collect(Collectors.joining(","))); } writer.write("\n"); diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ArrayUtilities.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ArrayUtilities.java deleted file mode 100644 index 5e8961159..000000000 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ArrayUtilities.java +++ /dev/null @@ -1,526 +0,0 @@ -package de.gurkenlabs.litiengine.util; - -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Utility class for array operations. - * - *

This class provides various static methods for manipulating arrays, such as concatenation, splitting, joining, and more.

- * - *

Note: This class cannot be instantiated.

- */ -public final class ArrayUtilities { - public static final String DEFAULT_STRING_DELIMITER = ","; - private static final Logger log = Logger.getLogger(ArrayUtilities.class.getName()); - - private ArrayUtilities() { - throw new UnsupportedOperationException(); - } - - /** - * Concatenates the two specified byte arrays to a new array. - * - * @param first The first array. - * @param second The second array. - * @return A new array with both specified arrays in sequence. - */ - public static byte[] concat(byte[] first, byte[] second) { - byte[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - /** - * Concatenates the two specified int arrays to a new array. - * - * @param first The first array. - * @param second The second array. - * @return A new array with both specified arrays in sequence. - */ - public static int[] concat(int[] first, int[] second) { - int[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - /** - * Concatenates the two specified long arrays to a new array. - * - * @param first The first array. - * @param second The second array. - * @return A new array with both specified arrays in sequence. - */ - public static long[] concat(long[] first, long[] second) { - long[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - /** - * Concatenates the two specified double arrays to a new array. - * - * @param first The first array. - * @param second The second array. - * @return A new array with both specified arrays in sequence. - */ - public static double[] concat(double[] first, double[] second) { - double[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - /** - * Concatenates the two specified double arrays to a new array. - * - * @param The type of the array elements. - * @param first The first array. - * @param second The second array. - * @return A new array with both specified arrays in sequence. - */ - public static T[] concat(T[] first, T[] second) { - T[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - /** - * Splits the specified string by the {@link #DEFAULT_STRING_DELIMITER} into an int array. - * - * @param delimiterSeparatedString The string to split. - * @return An int array with all separated elements of the specified string. - */ - public static int[] splitInt(String delimiterSeparatedString) { - return splitInt(delimiterSeparatedString, DEFAULT_STRING_DELIMITER); - } - - /** - * Splits the specified string by the defined delimiter into an int array. - * - * @param delimiterSeparatedString The string to split. - * @param delimiter The delimiter by which to split the elements. - * @return An int array with all separated elements of the specified string. - */ - public static int[] splitInt(String delimiterSeparatedString, String delimiter) { - if (delimiterSeparatedString == null || delimiterSeparatedString.isEmpty()) { - return new int[0]; - } - - final String[] split = delimiterSeparatedString.split(delimiter); - int[] integers = new int[split.length]; - if (integers.length == 0) { - return integers; - } - - for (int i = 0; i < split.length; i++) { - if (split[i] == null || split[i].isEmpty()) { - continue; - } - - try { - integers[i] = Integer.parseInt(split[i]); - } catch (final NumberFormatException e) { - log.log(Level.SEVERE, e.getMessage(), e); - } - } - - return integers; - } - - /** - * Splits the specified string by the {@link #DEFAULT_STRING_DELIMITER} into a double array. - * - * @param delimiterSeparatedString The string to split. - * @return An double array with all separated elements of the specified string. - */ - public static double[] splitDouble(String delimiterSeparatedString) { - return splitDouble(delimiterSeparatedString, DEFAULT_STRING_DELIMITER); - } - - /** - * Splits the specified string by the defined delimiter into a double array. - * - * @param delimiterSeparatedString The string to split. - * @param delimiter The delimiter by which to split the elements. - * @return An double array with all separated elements of the specified string. - */ - public static double[] splitDouble(String delimiterSeparatedString, String delimiter) { - if (delimiterSeparatedString == null || delimiterSeparatedString.isEmpty()) { - return new double[0]; - } - - final String[] split = delimiterSeparatedString.split(delimiter); - double[] doubles = new double[split.length]; - if (doubles.length == 0) { - return doubles; - } - - for (int i = 0; i < split.length; i++) { - if (split[i] == null || split[i].isEmpty()) { - continue; - } - - try { - doubles[i] = Double.parseDouble(split[i]); - } catch (final NumberFormatException e) { - log.log(Level.SEVERE, e.getMessage(), e); - } - } - - return doubles; - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(boolean[] arr) { - return joinArray(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(boolean[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(int[] arr) { - return joinArray(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(int[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(double[] arr) { - return joinArray(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(double[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(float[] arr) { - return joinArray(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(float[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(short[] arr) { - return joinArray(arr, DEFAULT_STRING_DELIMITER); - } - - public static String join(short[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(long[] arr) { - return join(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(long[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(byte[] arr) { - return join(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(byte[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - /** - * Joins the specified list with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param collection The list that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(Collection collection) { - return joinArray(collection.toArray(), DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified collection with the defined delimiter. - * - * @param collection The list that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(Collection collection, String delimiter) { - return joinArray(collection.toArray(), delimiter); - } - - /** - * Joins the specified array with the {@link #DEFAULT_STRING_DELIMITER}. - * - * @param arr The array that provides the elements to be joined. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(Object[] arr) { - return joinArray(arr, DEFAULT_STRING_DELIMITER); - } - - /** - * Joins the specified array with the defined delimiter. - * - * @param arr The array that provides the elements to be joined. - * @param delimiter The delimiter used to separate the elements with. - * @return A string with all joined elements, separated by the delimiter. - */ - public static String join(Object[] arr, String delimiter) { - return joinArray(arr, delimiter); - } - - public static List toList(T[][] arr) { - List list = new ArrayList<>(); - for (T[] rows : arr) { - list.addAll(Arrays.asList(rows)); - } - - return list; - } - - /** - * Return true if the array contains the specified value. - * - * @param arr The array that is tested for the existence of the element. - * @param value The element to check for in the array. - * @return True if the specified element is in the array; otherwise false. - */ - public static boolean contains(Object[] arr, Object value) { - for (Object v : arr) { - if (value == null && v == null) { - return true; - } - - if (v != null && v.equals(value)) { - return true; - } - } - - return false; - } - - /** - * Return true if the array contains the specified string argument. - * - * @param arr The array that is tested for the existence of the argument. - * @param argument The argument to check for in the array. - * @param ignoreCase A flag indicating whether the case should be ignored when checking for equality. - * @return True if the specified argument is in the array; otherwise false. - */ - public static boolean contains(String[] arr, String argument, boolean ignoreCase) { - if (arr == null) { - return false; - } - - for (String arg : arr) { - if (arg != null - && !arg.isEmpty() - && (ignoreCase && arg.equalsIgnoreCase(argument) - || !ignoreCase && arg.equals(argument))) { - return true; - } - } - - return false; - } - - /** - * Removes the specified deleteItem from the input array and returns a trimmed new array instance without null entries. The resulting array will - * have a length -1; - * - * @param The element type of the array. - * @param input The original array - * @param deleteItem The item to delete - * @return A new array with the length input.length - 1. - */ - @SuppressWarnings("unchecked") - public static T[] remove(T[] input, T deleteItem) { - List result = new ArrayList<>(); - - for (T item : input) { - if (!deleteItem.equals(item)) { - result.add(item); - } - } - - result.removeAll(Collections.singleton(null)); - return result.toArray( - (T[]) Array.newInstance(input.getClass().getComponentType(), result.size())); - } - - /** - * Adds the specified item to the input array and returns a new array instance with the length of the input array +1. - * - * @param The element type of the array. - * @param input The original array. - * @param addItem The item to add. - * @return A new array with the item to add appended at the end. - */ - @SuppressWarnings("unchecked") - public static T[] append(T[] input, T addItem) { - List result = new ArrayList<>(Arrays.asList(input)); - result.add(addItem); - - return result.toArray( - (T[]) Array.newInstance(input.getClass().getComponentType(), result.size())); - } - - /** - * Combines the two specified arrays by only keeping distinct values. - * - * @param The element type of the array. - * @param first The first array. - * @param second The second array. - * @return A new array with every distinct value of the specified arrays. - */ - @SuppressWarnings("unchecked") - public static T[] distinct(T[] first, T[] second) { - List firstList = Arrays.asList(first); - List secondList = Arrays.asList(second); - - HashSet hash = new HashSet<>(firstList); - hash.addAll(secondList); - - return hash.toArray((T[]) Array.newInstance(first.getClass().getComponentType(), hash.size())); - } - - /** - * Creates a copy of the specified array. - * - * @param the type of the array elements - * @param original the array to copy - * @return a new array that is a copy of the original array - */ - public static T[] arrayCopy(T[] original) { - return original.clone(); - } - - /** - * Converts a list of Integer objects to an array of primitive int values. - * - * @param intList the list of Integer objects to convert - * @return an array of primitive int values - */ - public static int[] toIntegerArray(List intList) { - Integer[] objArray = intList.toArray(new Integer[0]); - int[] intArray = new int[objArray.length]; - System.arraycopy(objArray, 0, intArray, 0, objArray.length); - return intArray; - } - - /** - * General method for joining an array. Encapsulated for type safety. - * - * @param arr The array to join. - * @param separator The separator to use between elements. - * @return A string with all joined elements, separated by the specified separator. - */ - private static String joinArray(Object arr, String separator) { - if (arr == null) { - return null; - } - - int len = Array.getLength(arr); - if (len == 0) { - return ""; - } - - StringBuilder sb = new StringBuilder(String.valueOf(Array.get(arr, 0))); - for (int i = 1; i < len; i++) { - sb.append(separator); - sb.append(Array.get(arr, i)); - } - - return sb.toString(); - } -} diff --git a/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ReflectionUtilities.java b/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ReflectionUtilities.java index 679d927eb..d048d533b 100644 --- a/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ReflectionUtilities.java +++ b/litiengine/src/main/java/de/gurkenlabs/litiengine/util/ReflectionUtilities.java @@ -259,9 +259,9 @@ public static boolean setFieldValue( } else if (field.getType().equals(String[].class)) { return setValue(cls, instance, fieldName, value.split(",")); } else if (field.getType().equals(int[].class)) { - return setValue(cls, instance, fieldName, ArrayUtilities.splitInt(value, ",")); + return setValue(cls, instance, fieldName, Arrays.stream(value.split(",")).mapToInt(Integer::parseInt).toArray()); } else if (field.getType().equals(double[].class)) { - return setValue(cls, instance, fieldName, ArrayUtilities.splitDouble(value, ",")); + return setValue(cls, instance, fieldName, Arrays.stream(value.split(",")).mapToDouble(Double::parseDouble).toArray()); } else if (field.getType().isEnum()) { return setEnumPropertyValue(cls, instance, field, fieldName, value); } else if (field.getType().equals(Material.class)) { diff --git a/litiengine/src/test/java/de/gurkenlabs/litiengine/util/ArrayUtilitiesTests.java b/litiengine/src/test/java/de/gurkenlabs/litiengine/util/ArrayUtilitiesTests.java deleted file mode 100644 index 7f9bd11d6..000000000 --- a/litiengine/src/test/java/de/gurkenlabs/litiengine/util/ArrayUtilitiesTests.java +++ /dev/null @@ -1,382 +0,0 @@ -package de.gurkenlabs.litiengine.util; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Arrays; -import java.util.List; -import java.util.logging.Logger; -import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -class ArrayUtilitiesTests { - - @BeforeEach - public void setup() { - Logger.getLogger(ArrayUtilities.class.getName()).setUseParentHandlers(false); - } - - @Test - void testByteArrayConcat() { - byte[] arr1 = new byte[] {1, 2, 3, 4, 5}; - byte[] arr2 = new byte[] {6, 7, 8, 9}; - - byte[] arr3 = ArrayUtilities.concat(arr1, arr2); - - assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr3); - } - - @Test - void testIntArrayConcat() { - int[] arr1 = new int[] {1, 2, 3, 4, 5}; - int[] arr2 = new int[] {6, 7, 8, 9}; - - int[] arr3 = ArrayUtilities.concat(arr1, arr2); - - assertArrayEquals(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr3); - } - - @Test - void testLongArrayConcat() { - long[] arr1 = new long[] {1, 2, 3, 4, 5}; - long[] arr2 = new long[] {6, 7, 8, 9}; - - long[] arr3 = ArrayUtilities.concat(arr1, arr2); - - assertArrayEquals(new long[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr3); - } - - @Test - void testDoubleArrayConcat() { - double[] arr1 = new double[] {1.0, 2.0, 3.0, 4.0, 5.0}; - double[] arr2 = new double[] {6.0, 7.0, 8.0, 9.0}; - - double[] arr3 = ArrayUtilities.concat(arr1, arr2); - - assertArrayEquals(new double[] {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, arr3); - } - - @Test - void testTArrayConcat() { - Integer[] arr1 = {1, 2, 3, 4, 5}; - Integer[] arr2 = {6, 7, 8, 9}; - - Integer[] arr3 = ArrayUtilities.concat(arr1, arr2); - - assertArrayEquals(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr3); - } - - @Test - void testIntegerArrayFromCommaSeparatedString() { - String testStringWithInts = "100,200,300,1,2,3"; - String testStringWithoutInts = "paslikodja,2asdasd,sadasd"; - String testNull = null; - String testEmpty = ""; - String testDelimiterEmpty = ","; - - int[] intsFromString = ArrayUtilities.splitInt(testStringWithInts); - int[] stringWithoutInts = ArrayUtilities.splitInt(testStringWithoutInts); - int[] nullString = ArrayUtilities.splitInt(testNull); - int[] emptyString = ArrayUtilities.splitInt(testEmpty); - int[] emptyDelimiterString = ArrayUtilities.splitInt(testDelimiterEmpty); - - assertArrayEquals(new int[] {100, 200, 300, 1, 2, 3}, intsFromString); - assertArrayEquals(new int[] {0, 0, 0}, stringWithoutInts); - assertArrayEquals(new int[] {}, nullString); - assertArrayEquals(new int[] {}, emptyString); - assertArrayEquals(new int[] {,}, emptyDelimiterString); - } - - @Test - void testDoubleArrayFromCommaSeparatedString() { - String testStringWithDoubles = "100.1,200.2,300.3,1.4,2.5,3.6"; - String testStringWithoutDoubles = "paslikodja,2asdasd,sadasd"; - String testNull = null; - String testEmpty = ""; - String testDelimiterEmpty = ","; - - double[] doublesFromString = ArrayUtilities.splitDouble(testStringWithDoubles); - double[] stringWithoutDoubles = ArrayUtilities.splitDouble(testStringWithoutDoubles); - double[] nullString = ArrayUtilities.splitDouble(testNull); - double[] emptyString = ArrayUtilities.splitDouble(testEmpty); - double[] emptyDelimiterString = ArrayUtilities.splitDouble(testDelimiterEmpty); - - assertArrayEquals(new double[] {100.1, 200.2, 300.3, 1.4, 2.5, 3.6}, doublesFromString); - assertArrayEquals(new double[] {0, 0, 0}, stringWithoutDoubles); - assertArrayEquals(new double[] {}, nullString); - assertArrayEquals(new double[] {}, emptyString); - assertArrayEquals(new double[] {}, emptyDelimiterString); - } - - @Test - void testCommaSeparatedStringFromIntegerArray() { - int[] intsArr = new int[] {100, 200, 300, 1, 2, 3}; - String testStringWithInts = ArrayUtilities.join(intsArr); - assertEquals("100,200,300,1,2,3", testStringWithInts); - } - - @Test - void testCommaSeparatedStringFromIntegerArrayEmpty() { - String testEmpty = ArrayUtilities.join(new int[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromIntegerArrayDelimiter() { - int[] intsArr = new int[] {100, 200, 300, 1, 2, 3}; - String testStringWithIntsDelimiter = ArrayUtilities.join(intsArr, ";"); - assertEquals("100;200;300;1;2;3", testStringWithIntsDelimiter); - } - - @Test - void testCommaSeparatedStringFromBooleanArray() { - boolean[] boolArr = new boolean[] {true, false, true}; - String testStringWithBool = ArrayUtilities.join(boolArr); - assertEquals("true,false,true", testStringWithBool); - } - - @Test - void testCommaSeparatedStringFromBooleanEmpty() { - String testEmpty = ArrayUtilities.join(new boolean[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromBooleanArrayDelimiter() { - boolean[] boolArr = new boolean[] {true, false, true}; - String testStringWithBoolDelimiter = ArrayUtilities.join(boolArr, ";"); - assertEquals("true;false;true", testStringWithBoolDelimiter); - } - - @Test - void testCommaSeparatedStringFromDoubleArray() { - double[] doubleArr = new double[] {100.0, 200.0, 300.0, 1.0, 2.0, 3.0}; - String testStringWithDouble = ArrayUtilities.join(doubleArr); - assertEquals("100.0,200.0,300.0,1.0,2.0,3.0", testStringWithDouble); - } - - @Test - void testCommaSeparatedStringFromDoubleArrayEmpty() { - String testEmpty = ArrayUtilities.join(new double[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromDoubleArrayDelimiter() { - double[] doubleArr = new double[] {100.0, 200.0, 300.0, 1.0, 2.0, 3.0}; - String testStringWithDoubleDelimiter = ArrayUtilities.join(doubleArr, ";"); - assertEquals("100.0;200.0;300.0;1.0;2.0;3.0", testStringWithDoubleDelimiter); - } - - @Test - void testCommaSeparatedStringFromFloatArray() { - float[] floatArr = new float[] {100.0f, 200.0f, 300.0f, 1.0f, 2.0f, 3.0f}; - String testStringWithFloat = ArrayUtilities.join(floatArr); - assertEquals("100.0,200.0,300.0,1.0,2.0,3.0", testStringWithFloat); - } - - @Test - void testCommaSeparatedStringFromFloatArrayEmpty() { - String testEmpty = ArrayUtilities.join(new double[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromFloatArrayDelimiter() { - float[] floatArr = new float[] {100.0f, 200.0f, 300.0f, 1.0f, 2.0f, 3.0f}; - String testStringWithFloatDelimiter = ArrayUtilities.join(floatArr, ";"); - assertEquals("100.0;200.0;300.0;1.0;2.0;3.0", testStringWithFloatDelimiter); - } - - @Test - void testCommaSeparatedStringFromStringArray() { - String[] stringArr = new String[] {"test", "test2", "test3"}; - String testStringWithString = ArrayUtilities.join(stringArr); - assertEquals("test,test2,test3", testStringWithString); - } - - @Test - void testCommaSeparatedStringFromStringArrayEmpty() { - String testEmpty = ArrayUtilities.join(new String[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromStringArrayDelimiter() { - String[] stringArr = new String[] {"test", "test2", "test3"}; - String testStringWithStringDelimiter = ArrayUtilities.join(stringArr, ";"); - assertEquals("test;test2;test3", testStringWithStringDelimiter); - } - - @Test - void testCommaSeparatedStringFromShortArray() { - short[] shortArr = new short[] {100, 200, 300, 1, 2, 3}; - String testStringWithShort = ArrayUtilities.join(shortArr); - assertEquals("100,200,300,1,2,3", testStringWithShort); - } - - @Test - void testCommaSeparatedStringFromShortArrayEmpty() { - String testEmpty = ArrayUtilities.join(new short[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromShortArrayDelimiter() { - short[] shortArr = new short[] {100, 200, 300, 1, 2, 3}; - String testStringWithShortDelimiter = ArrayUtilities.join(shortArr, ";"); - assertEquals("100;200;300;1;2;3", testStringWithShortDelimiter); - } - - @Test - void testCommaSeparatedStringFromLongArray() { - long[] longArr = new long[] {100, 200, 300, 1, 2, 3}; - String testStringWithLong = ArrayUtilities.join(longArr); - assertEquals("100,200,300,1,2,3", testStringWithLong); - } - - @Test - void testCommaSeparatedStringFromLongArrayEmpty() { - long[] longArr = new long[] {100, 200, 300, 1, 2, 3}; - String testStringWithLongDelimiter = ArrayUtilities.join(longArr, ";"); - assertEquals("100;200;300;1;2;3", testStringWithLongDelimiter); - } - - @Test - void testCommaSeparatedStringFromLongArrayDelimiter() { - long[] longArr = new long[] {100, 200, 300, 1, 2, 3}; - String testStringWithLongDelimiter = ArrayUtilities.join(longArr, ";"); - assertEquals("100;200;300;1;2;3", testStringWithLongDelimiter); - } - - @Test - void testCommaSeparatedStringFromByteArray() { - byte[] byteArr = new byte[] {100, 127, -128, 1, 2, 3}; - String testStringWithByte = ArrayUtilities.join(byteArr); - assertEquals("100,127,-128,1,2,3", testStringWithByte); - } - - @Test - void testCommaSeparatedStringFromByteArrayEmpty() { - String testEmpty = ArrayUtilities.join(new byte[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromByteArrayDelimiter() { - byte[] byteArr = new byte[] {100, 127, -128, 1, 2, 3}; - String testStringWithByteDelimiter = ArrayUtilities.join(byteArr, ";"); - assertEquals("100;127;-128;1;2;3", testStringWithByteDelimiter); - } - - @Test - void testCommaSeparatedStringFromCollectionArray() { - List collectionArr = Arrays.asList("test", "test2", "test3"); - String testStringWithCollectionDelimiter = ArrayUtilities.join(collectionArr, ";"); - assertEquals("test;test2;test3", testStringWithCollectionDelimiter); - } - - @Test - void testCommaSeparatedStringFromCollectionArrayEmpty() { - String testEmpty = ArrayUtilities.join(new String[] {}); - assertEquals("", testEmpty); - } - - @Test - void testCommaSeparatedStringFromCollectionArrayDelimiter() { - List collectionArr = Arrays.asList("test", "test2", "test3"); - String testStringWithCollectionDelimiter = ArrayUtilities.join(collectionArr, ";"); - assertEquals("test;test2;test3", testStringWithCollectionDelimiter); - } - - @ParameterizedTest - @MethodSource("getArray") - void testTwoDimensionalArrayToList(List expectedValue) { - Integer[][] arr = - new Integer[][] { - { - 0, 0, 0, - }, - { - 1, 1, 1, - }, - { - 2, 2, 2, - }, - }; - - List list = ArrayUtilities.toList(arr); - for (int i = 0; i < list.size(); i++) { - assertEquals(expectedValue.get(i).intValue(), list.get(i).intValue()); - } - } - - private static Stream getArray() { - return Stream.of(Arguments.of(Arrays.asList(0, 0, 0, 1, 1, 1, 2, 2, 2))); - } - - @Test - void testAppend() { - Integer[] test = new Integer[] {1, 2, 3, 4, 5}; - Integer[] result = ArrayUtilities.append(test, 6); - - assertArrayEquals(new Integer[] {1, 2, 3, 4, 5, 6}, result); - } - - @Test - void testDistinct() { - Integer[] first = new Integer[] {1, 2, 3, 4, 5}; - Integer[] second = new Integer[] {1, 2, 3, 4, 5, 6}; - Integer[] result = ArrayUtilities.distinct(first, second); - - assertArrayEquals(new Integer[] {1, 2, 3, 4, 5, 6}, result); - } - - @ParameterizedTest - @MethodSource("getContains") - void testContains(Object[] array, Object value, Boolean expected) { - assertEquals(expected, ArrayUtilities.contains(array, value)); - } - - private static Stream getContains() { - return Stream.of( - Arguments.of(new Object[] {1, 2, 3, 4, 5, null}, 2, true), - Arguments.of(new Object[] {1, 2, 3, 4, 5, null}, null, true), - Arguments.of(new Object[] {}, "", false), - Arguments.of(new Object[] {null}, null, true), - Arguments.of(new Object[] {4}, 4, true)); - } - - @ParameterizedTest - @MethodSource("getContainsString") - void testContainsString(String[] string, String argument, Boolean ignoreCase, Boolean expected) { - assertEquals(expected, ArrayUtilities.contains(string, argument, ignoreCase)); - } - - private static Stream getContainsString() { - return Stream.of( - Arguments.of(new String[] {"test", "test123"}, "Test", true, true), - Arguments.of(new String[] {"test", "test123"}, "Test", false, false), - Arguments.of(new String[] {"test", "test123", null, ""}, "Test", false, false), - Arguments.of(new String[] {"test", "test123", null, ""}, null, false, false), - Arguments.of(new String[] {}, "", true, false), - Arguments.of(new String[] {}, "", true, false), - Arguments.of(new String[] {null}, null, false, false), - Arguments.of(new String[] {null}, null, true, false), - Arguments.of(new String[] {"test"}, "Test", true, true), - Arguments.of(new String[] {"test"}, "test", false, true), - Arguments.of(null, null, false, false)); - } - - @Test - void testRemove() { - Integer[] test = new Integer[] {1, 2, 3, 4, 5}; - Integer[] result = ArrayUtilities.remove(test, 6); - - assertArrayEquals(new Integer[] {1, 2, 3, 4, 5}, result); - } -} diff --git a/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/PropertyPanel.java b/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/PropertyPanel.java index 8dfa6df97..b447635e3 100644 --- a/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/PropertyPanel.java +++ b/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/PropertyPanel.java @@ -5,7 +5,6 @@ import de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty; import de.gurkenlabs.litiengine.graphics.Spritesheet; import de.gurkenlabs.litiengine.resources.Resources; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import de.gurkenlabs.utiliti.controller.ControlBehavior; import de.gurkenlabs.utiliti.controller.Editor; import de.gurkenlabs.utiliti.controller.UndoManager; @@ -20,9 +19,11 @@ import java.awt.event.ItemListener; import java.awt.image.BufferedImage; import java.util.ArrayList; +import java.util.Arrays; import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; +import java.util.stream.Collectors; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.GroupLayout.ParallelGroup; @@ -850,7 +851,9 @@ protected class TableListener extends MabObjectPropertyTableModelListener { for (int i = 0; i < table.getRowCount(); i++) { values.add(table.getValueAt(i, column)); } - m.setValue(prop, ArrayUtilities.join(values, ",")); + m.setValue(prop, values.stream() + .map(String::valueOf) + .collect(Collectors.joining(","))); column++; } }); diff --git a/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TagPanel.java b/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TagPanel.java index 33222bb68..8cee8729e 100644 --- a/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TagPanel.java +++ b/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TagPanel.java @@ -1,7 +1,6 @@ package de.gurkenlabs.utiliti.view.components; import de.gurkenlabs.litiengine.Game; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import de.gurkenlabs.utiliti.controller.WrapLayout; import java.awt.Component; import java.awt.Dimension; @@ -47,9 +46,12 @@ public void componentAdded(ContainerEvent e) { this.textFieldInput.setColumns(7); this.textFieldInput.addActionListener( e -> { + // reference the event to avoid 'parameter never used' warnings + assert e != null; + boolean isEmpty = this.textFieldInput.getText() == null - || this.textFieldInput.getText().trim().length() == 0; + || this.textFieldInput.getText().trim().isEmpty(); if (isEmpty) { this.textFieldInput.setText(null); return; @@ -138,7 +140,8 @@ public List getTagStrings() { } public String getTagsString() { - return ArrayUtilities.join(this.getTags()); + // join the tag string values instead of Tag objects + return String.join(",", this.getTagStrings()); } public void clear() { @@ -151,7 +154,7 @@ public void clear() { } public void bind(String tagString) { - if (tagString == null || tagString.trim().length() == 0) { + if (tagString == null || tagString.trim().isEmpty()) { this.clear(); return; } @@ -217,7 +220,7 @@ private void autoComplete() { } private String findAutoCompletion(String currentText) { - if (currentText == null || currentText.trim().length() == 0) { + if (currentText == null || currentText.trim().isEmpty()) { return null; } diff --git a/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TextList.java b/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TextList.java index 82e1fb4f9..4922aacb8 100644 --- a/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TextList.java +++ b/utiliti/src/main/java/de/gurkenlabs/utiliti/view/components/TextList.java @@ -1,8 +1,8 @@ package de.gurkenlabs.utiliti.view.components; -import de.gurkenlabs.litiengine.util.ArrayUtilities; import java.awt.event.ActionListener; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; @@ -140,8 +140,8 @@ public String getJoinedString() { public void setJoinedString(String rows) { this.model.setRowCount(0); - for (int target : ArrayUtilities.splitInt(rows)) { - this.model.addRow(new Object[] {target}); + for (int target : rows == null || rows.isEmpty() ? new int[0] : Arrays.stream(rows.split(",")).mapToInt(Integer::parseInt).toArray()) { + this.model.addRow(new Object[]{target}); } } }