From 476d3c5ac92b5eef9ef8f7b6581e930fb824cd55 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 1 Nov 2024 17:41:01 +0800 Subject: [PATCH] =?UTF-8?q?ArrayTools=20=E5=92=8C=20StringTools=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20repeat=20=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/ArrayTools.java | 88 +++++++++++++++++++ .../plusone/commons/util/StringTools.java | 9 ++ 2 files changed, 97 insertions(+) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java index 1fd381d..9b460ea 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java @@ -337,6 +337,94 @@ public class ArrayTools { // #endregion + // #region - repeat + + // repeat - char + + public static char[] repeat(char[] arr, int times) { + return repeat(arr, times, Integer.MAX_VALUE); + } + + public static char[] repeat(char[] arr, int times, int maxLength) { + AssertTools.checkArgumentNotNull(arr); + AssertTools.checkArgument(times >= 0, + "The number of times must be greater than or equal to zero"); + AssertTools.checkArgument(maxLength >= 0, + "The max length must be greater than or equal to zero"); + if (times == 0) { + return EMPTY_CHAR_ARRAY; + } + final int length = Integer.min(arr.length * times, maxLength); + final char[] result = new char[length]; + fill(result, 0, length, arr); + return result; + } + + // repeat - long + + public static long[] repeat(long[] arr, int times) { + return repeat(arr, times, Integer.MAX_VALUE); + } + + public static long[] repeat(long[] arr, int times, int maxLength) { + AssertTools.checkArgumentNotNull(arr); + AssertTools.checkArgument(times >= 0, + "The number of times must be greater than or equal to zero"); + AssertTools.checkArgument(maxLength >= 0, + "The max length must be greater than or equal to zero"); + if (times == 0) { + return EMPTY_LONG_ARRAY; + } + final int length = Integer.min(arr.length * times, maxLength); + final long[] result = new long[length]; + fill(result, 0, length, arr); + return result; + } + + // repeat - float + + public static float[] repeat(float[] arr, int times) { + return repeat(arr, times, Integer.MAX_VALUE); + } + + public static float[] repeat(float[] arr, int times, int maxLength) { + AssertTools.checkArgumentNotNull(arr); + AssertTools.checkArgument(times >= 0, + "The number of times must be greater than or equal to zero"); + AssertTools.checkArgument(maxLength >= 0, + "The max length must be greater than or equal to zero"); + if (times == 0) { + return EMPTY_FLOAT_ARRAY; + } + final int length = Integer.min(arr.length * times, maxLength); + final float[] result = new float[length]; + fill(result, 0, length, arr); + return result; + } + + // repeat - double + + public static double[] repeat(double[] arr, int times) { + return repeat(arr, times, Integer.MAX_VALUE); + } + + public static double[] repeat(double[] arr, int times, int maxLength) { + AssertTools.checkArgumentNotNull(arr); + AssertTools.checkArgument(times >= 0, + "The number of times must be greater than or equal to zero"); + AssertTools.checkArgument(maxLength >= 0, + "The max length must be greater than or equal to zero"); + if (times == 0) { + return EMPTY_DOUBLE_ARRAY; + } + final int length = Integer.min(arr.length * times, maxLength); + final double[] result = new double[length]; + fill(result, 0, length, arr); + return result; + } + + // #endregion + // #region - fill // fill - char diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java index 951ead0..5c95d80 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java @@ -35,6 +35,15 @@ public class StringTools { return false; } + public static String repeat(String str, int times) { + return repeat(str, times, Integer.MAX_VALUE); + } + + public static String repeat(String str, int times, int maxLength) { + AssertTools.checkArgumentNotNull(str); + return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength)); + } + private StringTools() { throw new IllegalStateException("Utility class"); }