{
/**
* 获取子数组
*
- * @param begin 开始位置(包括)
- * @param end 结束位置(不包括)
+ * @param beginInclude 开始位置(包括)
+ * @param endExclude 结束位置(不包括)
* @return 新的数组
* @see Arrays#copyOfRange(Object[], int, int)
* @since 4.2.2
*/
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
- public A getSub(int begin, int end) {
+ public A getSub(int beginInclude, int endExclude) {
final int length = this.length;
- if (begin < 0) {
- begin += length;
+ if (beginInclude < 0) {
+ beginInclude += length;
}
- if (end < 0) {
- end += length;
+ if (endExclude < 0) {
+ endExclude += length;
}
- if (begin > end) {
- final int tmp = begin;
- begin = end;
- end = tmp;
+ if (beginInclude > endExclude) {
+ final int tmp = beginInclude;
+ beginInclude = endExclude;
+ endExclude = tmp;
}
- if (begin >= length) {
+ if (beginInclude >= length) {
return (A) Array.newInstance(this.componentType, 0);
}
- if (end > length) {
- end = length;
+ if (endExclude > length) {
+ endExclude = length;
}
- final A result = (A) Array.newInstance(this.componentType, end - begin);
- System.arraycopy(this.array, begin, result, 0, end - begin);
+ final A result = (A) Array.newInstance(this.componentType, endExclude - beginInclude);
+ System.arraycopy(this.array, beginInclude, result, 0, endExclude - beginInclude);
return result;
}
+ /**
+ * 获取子数组
+ *
+ * @param beginInclude 开始位置(包括)
+ * @param endExclude 结束位置(不包括)
+ * @param step 步进
+ * @return 新的数组
+ */
+ @SuppressWarnings("unchecked")
+ public A getSub(int beginInclude, int endExclude, int step) {
+ final int length = this.length;
+ if (beginInclude < 0) {
+ beginInclude += length;
+ }
+ if (endExclude < 0) {
+ endExclude += length;
+ }
+ if (beginInclude > endExclude) {
+ final int tmp = beginInclude;
+ beginInclude = endExclude;
+ endExclude = tmp;
+ }
+ if (beginInclude >= length) {
+ return (A) Array.newInstance(this.componentType, 0);
+ }
+ if (endExclude > length) {
+ endExclude = length;
+ }
+
+ if (step <= 1) {
+ step = 1;
+ }
+
+ final int size = (endExclude - beginInclude + step - 1) / step;
+ final A result = (A) Array.newInstance(this.componentType, size);
+ int j = 0;
+ for (int i = beginInclude; i < endExclude; i += step) {
+ Array.set(result, j, get(i));
+ j++;
+ }
+ return result;
+ }
+
+ /**
+ * 检查数组是否有序,升序或者降序
+ * 若传入空数组,则返回{@code false};元素全部相等,返回 {@code true}
+ *
+ * @param comparator 比较器
+ * @return 数组是否有序
+ * @throws NullPointerException 如果数组元素含有null值
+ * @since 6.0.0
+ */
+ public boolean isSorted(final Comparator super A> comparator) {
+ if (isEmpty()) {
+ return false;
+ }
+ final int lastIndex = this.length - 1;
+ // 对比第一个和最后一个元素,大致预估这个数组是升序还是降序
+ final int cmp = comparator.compare(get(0), get(lastIndex));
+ if (cmp < 0) {
+ return isSorted(comparator, false);
+ } else if (cmp > 0) {
+ return isSorted(comparator, true);
+ }
+
+ // 可能全等数组
+ for (int i = 0; i < lastIndex; i++) {
+ if (comparator.compare(get(i), get(i + 1)) != 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * 数组是否有有序
+ *
+ * - 反序,前一个小于后一个则返回错
+ * - 正序,前一个大于后一个则返回错
+ *
+ *
+ * @param comparator {@link Comparator}
+ * @param isDESC 是否反序
+ * @return 是否有序
+ */
+ public boolean isSorted(final Comparator super A> comparator, final boolean isDESC) {
+ if (null == comparator) {
+ return false;
+ }
+
+ int compare;
+ for (int i = 0; i < this.length; i++) {
+ compare = comparator.compare(get(i), get(i + 1));
+ if ((isDESC && compare < 0) ||
+ (false == isDESC && compare > 0)) {
+ // 反序,前一个小于后一个则返回错
+ // 正序,前一个大于后一个则返回错
+ return false;
+ }
+ }
+ return true;
+ }
+
@Override
public String toString() {
final A array = this.array;
diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/comparator/CompareUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/comparator/CompareUtil.java
index 8d358125d..f1673bf2e 100644
--- a/hutool-core/src/main/java/org/dromara/hutool/core/comparator/CompareUtil.java
+++ b/hutool-core/src/main/java/org/dromara/hutool/core/comparator/CompareUtil.java
@@ -317,7 +317,7 @@ public class CompareUtil {
*/
@SuppressWarnings("unchecked")
public static Comparator comparingIndexed(final Function super T, ? extends U> keyExtractor, final Iterable objs) {
- return comparingIndexed(keyExtractor, false, ArrayUtil.toArray(objs, (Class) objs.iterator().next().getClass()));
+ return comparingIndexed(keyExtractor, false, ArrayUtil.ofArray(objs, (Class) objs.iterator().next().getClass()));
}
/**
diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/BufferUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/BufferUtil.java
index 712234401..d72e6ebfb 100644
--- a/hutool-core/src/main/java/org/dromara/hutool/core/io/BufferUtil.java
+++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/BufferUtil.java
@@ -20,6 +20,7 @@ import org.dromara.hutool.core.util.CharsetUtil;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
+import java.util.Arrays;
/**
* {@link ByteBuffer} 工具类
@@ -31,6 +32,26 @@ import java.nio.charset.Charset;
*/
public class BufferUtil {
+ /**
+ * {@link ByteBuffer} 转byte数组
+ *
+ * @param bytebuffer {@link ByteBuffer}
+ * @return byte数组
+ */
+ public static byte[] toBytes(final ByteBuffer bytebuffer) {
+ if (bytebuffer.hasArray()) {
+ return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
+ } else {
+ final int oldPosition = bytebuffer.position();
+ bytebuffer.position(0);
+ final int size = bytebuffer.limit();
+ final byte[] buffers = new byte[size];
+ bytebuffer.get(buffers);
+ bytebuffer.position(oldPosition);
+ return buffers;
+ }
+ }
+
/**
* 拷贝到一个新的ByteBuffer
*
diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayWrapperTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayWrapperTest.java
new file mode 100644
index 000000000..1390b68a6
--- /dev/null
+++ b/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayWrapperTest.java
@@ -0,0 +1,41 @@
+package org.dromara.hutool.core.array;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class ArrayWrapperTest {
+
+ @Test
+ void getSubTest() {
+ ArrayWrapper array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5});
+ int[] sub = array.getSub(1, 4);
+ Assertions.assertArrayEquals(new int[]{2, 3, 4}, sub);
+
+ array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5, 6});
+ sub = array.getSub(1, 4);
+ Assertions.assertArrayEquals(new int[]{2, 3, 4}, sub);
+ }
+
+ @Test
+ void getSubStepTest() {
+ ArrayWrapper array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5});
+ int[] sub = array.getSub(1, 4, 2);
+ Assertions.assertArrayEquals(new int[]{2, 4}, sub);
+
+ array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5, 6});
+ sub = array.getSub(1, 4, 2);
+ Assertions.assertArrayEquals(new int[]{2, 4}, sub);
+
+ array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5, 6});
+ sub = array.getSub(0, 5, 2);
+ Assertions.assertArrayEquals(new int[]{1, 3, 5}, sub);
+
+ array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5, 6});
+ sub = array.getSub(0, 5, 3);
+ Assertions.assertArrayEquals(new int[]{1, 4}, sub);
+
+ array = ArrayWrapper.of(new int[]{1, 2, 3, 4, 5, 6});
+ sub = array.getSub(1, 6, 2);
+ Assertions.assertArrayEquals(new int[]{2, 4, 6}, sub);
+ }
+}
diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/util/ArrayUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/util/ArrayUtilTest.java
index aa6901c0b..fff4307cd 100644
--- a/hutool-core/src/test/java/org/dromara/hutool/core/util/ArrayUtilTest.java
+++ b/hutool-core/src/test/java/org/dromara/hutool/core/util/ArrayUtilTest.java
@@ -305,7 +305,7 @@ public class ArrayUtilTest {
@Test
public void toArrayTest() {
final List list = ListUtil.of("A", "B", "C", "D");
- final String[] array = ArrayUtil.toArray(list, String.class);
+ final String[] array = ArrayUtil.ofArray(list, String.class);
Assertions.assertEquals("A", array[0]);
Assertions.assertEquals("B", array[1]);
Assertions.assertEquals("C", array[2]);