forked from plusone/plusone-commons
Compare commits
3 Commits
8d3bbbc56b
...
26efd1125e
Author | SHA1 | Date | |
---|---|---|---|
26efd1125e | |||
b912f4b063 | |||
90da2b8eaa |
@ -24,4 +24,7 @@
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
@ -86,6 +86,7 @@ public enum Quarter implements IWithIntCode {
|
||||
*/
|
||||
@StaticFactoryMethod(Quarter.class)
|
||||
public static Quarter fromMonth(Month month) {
|
||||
AssertTools.checkNotNull(month);
|
||||
final int monthValue = month.getValue();
|
||||
return of(computeQuarterValueInternal(monthValue));
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.errorprone.annotations.Immutable;
|
||||
|
||||
@ -52,7 +52,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
/** 季度结束日期 */
|
||||
private final LocalDate lastDate;
|
||||
|
||||
private YearQuarter(int year, @Nonnull Quarter quarter) {
|
||||
private YearQuarter(int year, Quarter quarter) {
|
||||
this.year = year;
|
||||
this.quarter = quarter;
|
||||
this.firstDate = quarter.firstMonthDay().atYear(year);
|
||||
@ -249,7 +249,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
@ -264,6 +264,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
|
||||
// #region - compare
|
||||
|
||||
@SuppressWarnings("null")
|
||||
@Override
|
||||
public int compareTo(YearQuarter other) {
|
||||
int cmp = (this.year - other.year);
|
||||
|
@ -24,4 +24,7 @@
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
@ -26,7 +26,6 @@ import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@ -259,7 +258,7 @@ public class ArrayTools {
|
||||
*
|
||||
* @throws IllegalArgumentException 当参数为空时抛出
|
||||
*/
|
||||
public static <T> boolean isAllElementsNotNull(@Nonnull final T[] arr) {
|
||||
public static <T> boolean isAllElementsNotNull(final T[] arr) {
|
||||
AssertTools.checkArgument(arr != null, "The array cannot be null.");
|
||||
return Arrays.stream(arr).allMatch(Objects::nonNull);
|
||||
}
|
||||
@ -622,15 +621,15 @@ public class ArrayTools {
|
||||
|
||||
// fill - char
|
||||
|
||||
public static void fill(char[] a, char[] values) {
|
||||
public static void fill(char[] a, @Nullable char[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(char[] a, String values) {
|
||||
public static void fill(char[] a, @Nullable String values) {
|
||||
fill(a, 0, a.length, values != null ? values.toCharArray() : EMPTY_CHAR_ARRAY);
|
||||
}
|
||||
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, char[] values) {
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -655,11 +654,11 @@ public class ArrayTools {
|
||||
|
||||
// fill - byte
|
||||
|
||||
public static void fill(byte[] a, byte[] values) {
|
||||
public static void fill(byte[] a, @Nullable byte[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, byte[] values) {
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -684,11 +683,11 @@ public class ArrayTools {
|
||||
|
||||
// fill - short
|
||||
|
||||
public static void fill(short[] a, short[] values) {
|
||||
public static void fill(short[] a, @Nullable short[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, short[] values) {
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -713,11 +712,11 @@ public class ArrayTools {
|
||||
|
||||
// fill - int
|
||||
|
||||
public static void fill(int[] a, int[] values) {
|
||||
public static void fill(int[] a, @Nullable int[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, int[] values) {
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -742,11 +741,11 @@ public class ArrayTools {
|
||||
|
||||
// fill - long
|
||||
|
||||
public static void fill(long[] a, long[] values) {
|
||||
public static void fill(long[] a, @Nullable long[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, long[] values) {
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -771,11 +770,11 @@ public class ArrayTools {
|
||||
|
||||
// fill - float
|
||||
|
||||
public static void fill(float[] a, float[] values) {
|
||||
public static void fill(float[] a, @Nullable float[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, float[] values) {
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -800,11 +799,11 @@ public class ArrayTools {
|
||||
|
||||
// fill - double
|
||||
|
||||
public static void fill(double[] a, double[] values) {
|
||||
public static void fill(double[] a, @Nullable double[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, double[] values) {
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -829,15 +828,15 @@ public class ArrayTools {
|
||||
|
||||
// fill - T
|
||||
|
||||
public static <T> void fill(@Nonnull T[] a, T[] values) {
|
||||
public static <T> void fill(T[] a, @Nullable T[] values) {
|
||||
fillInternal(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static <T> void fill(@Nonnull T[] a, int fromIndex, int toIndex, T[] values) {
|
||||
public static <T> void fill(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
|
||||
fillInternal(a, fromIndex, toIndex, values);
|
||||
}
|
||||
|
||||
private static <T> void fillInternal(@Nonnull T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
|
||||
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
@ -864,9 +863,9 @@ public class ArrayTools {
|
||||
|
||||
// #region - indexOf
|
||||
|
||||
public static <T> int indexOfWithPredicate(T[] arr, Predicate<? super T> predicate) {
|
||||
public static <T> int indexOfWithPredicate(@Nullable T[] arr, Predicate<? super T> predicate) {
|
||||
AssertTools.checkNotNull(predicate);
|
||||
if (isNullOrEmpty(arr)) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -877,12 +876,12 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static <T> int indexOf(T[] arr, T obj) {
|
||||
public static <T> int indexOf(@Nullable T[] arr, @Nullable T obj) {
|
||||
return indexOfWithPredicate(arr, item -> Objects.equals(item, obj));
|
||||
}
|
||||
|
||||
public static int indexOf(char[] arr, char value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable char[] arr, char value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -893,8 +892,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int indexOf(byte[] arr, byte value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable byte[] arr, byte value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -905,8 +904,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int indexOf(short[] arr, short value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable short[] arr, short value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -917,8 +916,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int indexOf(int[] arr, int value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable int[] arr, int value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -929,8 +928,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int indexOf(long[] arr, long value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable long[] arr, long value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -941,8 +940,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int indexOf(float[] arr, float value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable float[] arr, float value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -953,8 +952,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int indexOf(double[] arr, double value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int indexOf(@Nullable double[] arr, double value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
@ -969,9 +968,9 @@ public class ArrayTools {
|
||||
|
||||
// #region - lastIndexOf
|
||||
|
||||
public static <T> int lastIndexOfWithPredicate(T[] arr, @Nonnull Predicate<? super T> predicate) {
|
||||
public static <T> int lastIndexOfWithPredicate(@Nullable T[] arr, Predicate<? super T> predicate) {
|
||||
AssertTools.checkNotNull(predicate);
|
||||
if (isNullOrEmpty(arr)) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -986,8 +985,8 @@ public class ArrayTools {
|
||||
return lastIndexOfWithPredicate(arr, item -> Objects.equals(item, obj));
|
||||
}
|
||||
|
||||
public static int lastIndexOf(char[] arr, char value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable char[] arr, char value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -998,8 +997,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int lastIndexOf(byte[] arr, byte value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable byte[] arr, byte value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -1010,8 +1009,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int lastIndexOf(short[] arr, short value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable short[] arr, short value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -1022,8 +1021,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int lastIndexOf(int[] arr, int value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable int[] arr, int value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -1034,8 +1033,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int lastIndexOf(long[] arr, long value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable long[] arr, long value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -1046,8 +1045,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int lastIndexOf(float[] arr, float value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable float[] arr, float value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -1058,8 +1057,8 @@ public class ArrayTools {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static int lastIndexOf(double[] arr, double value) {
|
||||
if (isNullOrEmpty(arr)) {
|
||||
public static int lastIndexOf(@Nullable double[] arr, double value) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
for (int i = arr.length - 1; i >= 0; i--) {
|
||||
@ -1074,39 +1073,39 @@ public class ArrayTools {
|
||||
|
||||
// #region - contains
|
||||
|
||||
public static <T> boolean contains(T[] arr, T obj) {
|
||||
public static <T> boolean contains(@Nullable T[] arr, @Nullable T obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(char[] arr, char obj) {
|
||||
public static boolean contains(@Nullable char[] arr, char obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(byte[] arr, byte obj) {
|
||||
public static boolean contains(@Nullable byte[] arr, byte obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(short[] arr, short obj) {
|
||||
public static boolean contains(@Nullable short[] arr, short obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(int[] arr, int obj) {
|
||||
public static boolean contains(@Nullable int[] arr, int obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(long[] arr, long obj) {
|
||||
public static boolean contains(@Nullable long[] arr, long obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(float[] arr, float obj) {
|
||||
public static boolean contains(@Nullable float[] arr, float obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean contains(double[] arr, double obj) {
|
||||
public static boolean contains(@Nullable double[] arr, double obj) {
|
||||
return indexOf(arr, obj) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
public static boolean containsValue(BigDecimal[] arr, BigDecimal obj) {
|
||||
public static boolean containsValue(@Nullable BigDecimal[] arr, @Nullable BigDecimal obj) {
|
||||
return indexOfWithPredicate(arr, item -> BigDecimals.equalsValue(item, obj)) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.DataNotExistsException;
|
||||
import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
|
||||
@ -55,12 +55,12 @@ public class AssertTools {
|
||||
}
|
||||
|
||||
/** Throw {@link IllegalArgumentException} if the {@code condition} is false. */
|
||||
public static void checkArgument(boolean condition, String errMsg) {
|
||||
public static void checkArgument(boolean condition, @Nullable String errMsg) {
|
||||
checkCondition(condition, () -> new IllegalArgumentException(errMsg));
|
||||
}
|
||||
|
||||
/** Throw {@link IllegalArgumentException} if the {@code condition} is false. */
|
||||
public static void checkArgument(boolean condition, @Nonnull Supplier<String> messageSupplier) {
|
||||
public static void checkArgument(boolean condition, Supplier<String> messageSupplier) {
|
||||
checkCondition(condition, () -> new IllegalArgumentException(messageSupplier.get()));
|
||||
}
|
||||
|
||||
@ -83,12 +83,12 @@ public class AssertTools {
|
||||
}
|
||||
|
||||
/** Throw {@link IllegalStateException} if the {@code condition} is false. */
|
||||
public static void checkState(boolean condition, String errMsg) {
|
||||
public static void checkState(boolean condition, @Nullable String errMsg) {
|
||||
checkCondition(condition, () -> new IllegalStateException(errMsg));
|
||||
}
|
||||
|
||||
/** Throw {@link IllegalStateException} if the {@code condition} is false. */
|
||||
public static void checkState(boolean condition, @Nonnull Supplier<String> messageSupplier) {
|
||||
public static void checkState(boolean condition, Supplier<String> messageSupplier) {
|
||||
checkCondition(condition, () -> new IllegalStateException(messageSupplier.get()));
|
||||
}
|
||||
|
||||
@ -106,22 +106,22 @@ public class AssertTools {
|
||||
// ================================
|
||||
|
||||
/** Throw {@link NullPointerException} if the {@code obj} is null. */
|
||||
public static <T> void checkNotNull(T obj) {
|
||||
public static <T> void checkNotNull(@Nullable T obj) {
|
||||
checkCondition(obj != null, NullPointerException::new);
|
||||
}
|
||||
|
||||
/** Throw {@link NullPointerException} if the {@code obj} is null. */
|
||||
public static <T> void checkNotNull(T obj, String errMsg) {
|
||||
public static <T> void checkNotNull(@Nullable T obj, String errMsg) {
|
||||
checkCondition(obj != null, () -> new NullPointerException(errMsg));
|
||||
}
|
||||
|
||||
/** Throw {@link NullPointerException} if the {@code obj} is null. */
|
||||
public static <T> void checkNotNull(T obj, @Nonnull Supplier<String> messageSupplier) {
|
||||
public static <T> void checkNotNull(@Nullable T obj, Supplier<String> messageSupplier) {
|
||||
checkCondition(obj != null, () -> new NullPointerException(messageSupplier.get()));
|
||||
}
|
||||
|
||||
/** Throw {@link NullPointerException} if the {@code obj} is null. */
|
||||
public static <T> void checkNotNull(T obj, String format, Object... args) {
|
||||
public static <T> void checkNotNull(@Nullable T obj, String format, Object... args) {
|
||||
checkCondition(obj != null, () -> new NullPointerException(String.format(format, args)));
|
||||
}
|
||||
|
||||
@ -134,56 +134,56 @@ public class AssertTools {
|
||||
// ================================
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code obj} is null. */
|
||||
public static <T> T checkExists(T obj)
|
||||
public static <T> T checkExists(@Nullable T obj)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(Objects.nonNull(obj), DataNotExistsException::new);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code obj} is null. */
|
||||
public static <T> T checkExists(T obj, String message)
|
||||
public static <T> T checkExists(@Nullable T obj, String message)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(message));
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code obj} is null. */
|
||||
public static <T> T checkExists(T obj, @Nonnull Supplier<String> messageSupplier)
|
||||
public static <T> T checkExists(@Nullable T obj, Supplier<String> messageSupplier)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(messageSupplier.get()));
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code obj} is null. */
|
||||
public static <T> T checkExists(T obj, String format, Object... args)
|
||||
public static <T> T checkExists(@Nullable T obj, String format, Object... args)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(String.format(format, args)));
|
||||
return obj;
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code optional} is present. */
|
||||
public static <T> T checkExists(@Nonnull Optional<T> optional)
|
||||
public static <T> T checkExists(Optional<T> optional)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), DataNotExistsException::new);
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code optional} is present. */
|
||||
public static <T> T checkExists(@Nonnull Optional<T> optional, String message)
|
||||
public static <T> T checkExists(Optional<T> optional, String message)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), () -> new DataNotExistsException(message));
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code optional} is present. */
|
||||
public static <T> T checkExists(@Nonnull Optional<T> optional, @Nonnull Supplier<String> messageSupplier)
|
||||
public static <T> T checkExists(Optional<T> optional, Supplier<String> messageSupplier)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), () -> new DataNotExistsException(messageSupplier.get()));
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
/** Throw {@link DataNotExistsException} if the {@code optional} is present. */
|
||||
public static <T> T checkExists(@Nonnull Optional<T> optional, String format, Object... args)
|
||||
public static <T> T checkExists(Optional<T> optional, String format, Object... args)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), () -> new DataNotExistsException(String.format(format, args)));
|
||||
return optional.get();
|
||||
@ -202,17 +202,18 @@ public class AssertTools {
|
||||
"The number of rows affected is expected to be %d, but is: %d", expectedValue, result);
|
||||
}
|
||||
|
||||
public static void checkAffectedRows(int expectedValue, int result, String message) {
|
||||
public static void checkAffectedRows(int expectedValue, int result, @Nullable String message) {
|
||||
checkCondition(expectedValue == result, () -> new DataOperationResultException(message));
|
||||
}
|
||||
|
||||
public static void checkAffectedRows(int expectedValue, int result,
|
||||
@Nonnull Supplier<String> messageSupplier) {
|
||||
Supplier<String> messageSupplier) {
|
||||
checkCondition(expectedValue == result,
|
||||
() -> new DataOperationResultException(messageSupplier.get()));
|
||||
}
|
||||
|
||||
public static void checkAffectedRows(int expectedValue, int result, String format, Object... args) {
|
||||
public static void checkAffectedRows(int expectedValue, int result,
|
||||
String format, Object... args) {
|
||||
checkCondition(expectedValue == result,
|
||||
() -> new DataOperationResultException(String.format(format, args)));
|
||||
}
|
||||
@ -222,17 +223,18 @@ public class AssertTools {
|
||||
"The number of rows affected is expected to be %d, but is: %d", expectedValue, result);
|
||||
}
|
||||
|
||||
public static void checkAffectedRows(long expectedValue, long result, String message) {
|
||||
public static void checkAffectedRows(long expectedValue, long result, @Nullable String message) {
|
||||
checkCondition(expectedValue == result, () -> new DataOperationResultException(message));
|
||||
}
|
||||
|
||||
public static void checkAffectedRows(long expectedValue, long result,
|
||||
@Nonnull Supplier<String> messageSupplier) {
|
||||
Supplier<String> messageSupplier) {
|
||||
checkCondition(expectedValue == result,
|
||||
() -> new DataOperationResultException(messageSupplier.get()));
|
||||
}
|
||||
|
||||
public static void checkAffectedRows(long expectedValue, long result, String format, Object... args) {
|
||||
public static void checkAffectedRows(long expectedValue, long result,
|
||||
String format, Object... args) {
|
||||
checkCondition(expectedValue == result,
|
||||
() -> new DataOperationResultException(String.format(format, args)));
|
||||
}
|
||||
@ -246,7 +248,7 @@ public class AssertTools {
|
||||
checkAffectedRows(1, result, message);
|
||||
}
|
||||
|
||||
public static void checkAffectedOneRow(int result, @Nonnull Supplier<String> messageSupplier) {
|
||||
public static void checkAffectedOneRow(int result, Supplier<String> messageSupplier) {
|
||||
checkAffectedRows(1, result, messageSupplier);
|
||||
}
|
||||
|
||||
@ -263,11 +265,12 @@ public class AssertTools {
|
||||
checkAffectedRows(1L, result, message);
|
||||
}
|
||||
|
||||
public static void checkAffectedOneRow(long result, @Nonnull Supplier<String> messageSupplier) {
|
||||
public static void checkAffectedOneRow(long result, Supplier<String> messageSupplier) {
|
||||
checkAffectedRows(1L, result, messageSupplier);
|
||||
}
|
||||
|
||||
public static void checkAffectedOneRow(long result, String format, Object... args) {
|
||||
public static void checkAffectedOneRow(long result,
|
||||
String format, Object... args) {
|
||||
checkAffectedRows(1L, result, format, args);
|
||||
}
|
||||
|
||||
@ -279,7 +282,7 @@ public class AssertTools {
|
||||
// #region - Condition
|
||||
// ================================
|
||||
|
||||
public static <T extends Exception> void checkCondition(boolean condition, @Nonnull Supplier<T> e)
|
||||
public static <T extends Exception> void checkCondition(boolean condition, Supplier<T> e)
|
||||
throws T {
|
||||
if (!condition) {
|
||||
throw e.get();
|
||||
|
@ -46,7 +46,9 @@ public class BigDecimals {
|
||||
}
|
||||
|
||||
public static boolean ge(BigDecimal a, BigDecimal b) {
|
||||
return gt(a, b) || equalsValue(a, b);
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a == b) || (a.compareTo(b) >= 0);
|
||||
}
|
||||
|
||||
public static boolean lt(BigDecimal a, BigDecimal b) {
|
||||
@ -56,7 +58,9 @@ public class BigDecimals {
|
||||
}
|
||||
|
||||
public static boolean le(BigDecimal a, BigDecimal b) {
|
||||
return lt(a, b) || equalsValue(a, b);
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a == b) || (a.compareTo(b) <= 0);
|
||||
}
|
||||
|
||||
public static BigDecimal sum(final BigDecimal... numbers) {
|
||||
@ -79,7 +83,7 @@ public class BigDecimals {
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(BigDecimal.class)
|
||||
public static BigDecimal of(final String val) {
|
||||
public static BigDecimal of(@Nullable final String val) {
|
||||
return (StringTools.isNotBlank(val)) ? new BigDecimal(val) : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,9 @@ import xyz.zhouxy.plusone.commons.time.YearQuarter;
|
||||
*/
|
||||
public class DateTimeTools {
|
||||
|
||||
// ================================
|
||||
// #region - toString
|
||||
// ================================
|
||||
|
||||
public static String toYearString(int year) {
|
||||
return Integer.toString(YEAR.checkValidIntValue(year));
|
||||
@ -70,9 +72,13 @@ public class DateTimeTools {
|
||||
return String.format("%02d", month.getValue());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toDate
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将时间戳转换为 {@link Date} 对象
|
||||
@ -137,9 +143,13 @@ public class DateTimeTools {
|
||||
return Date.from(ZonedDateTime.of(localDate, localTime, zone).toInstant());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将时间戳转换为 {@link Instant} 对象
|
||||
@ -192,9 +202,13 @@ public class DateTimeTools {
|
||||
return ZonedDateTime.of(localDateTime, zone).toInstant();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 获取时间戳在指定时区的地区时间。
|
||||
@ -285,9 +299,13 @@ public class DateTimeTools {
|
||||
return ZonedDateTime.of(localDateTime, zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 获取时间戳在指定时区的地区时间。
|
||||
@ -355,11 +373,13 @@ public class DateTimeTools {
|
||||
return LocalDateTime.ofInstant(zonedDateTime.toInstant(), zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ====================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.Instant} 转换为 {@link org.joda.time.Instant}
|
||||
@ -392,9 +412,13 @@ public class DateTimeTools {
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link org.joda.time.Instant} 对象转换为 {@link java.time.Instant} 对象
|
||||
@ -432,9 +456,13 @@ public class DateTimeTools {
|
||||
return toJavaInstant(localDateTime.toDateTime(zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 Java 中表示日期时间的 {@link java.time.ZonedDateTime} 对象
|
||||
@ -479,9 +507,13 @@ public class DateTimeTools {
|
||||
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 joda-time 中带时区的日期时间,转换为 java.time 中带时区的日期时间
|
||||
@ -511,7 +543,7 @@ public class DateTimeTools {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 joda-time 中的 {@link org.joda.time.Instant} 在指定时区的时间,用 Java 8
|
||||
* 获取 joda-time 中的 {@link org.joda.time.Instant} 在指定时区的时间,用 Java 8+ 的
|
||||
* {@link java.time.ZonedDateTime} 表示
|
||||
*
|
||||
* @param instant joda-time 中的时间戳
|
||||
@ -525,9 +557,13 @@ public class DateTimeTools {
|
||||
return toJavaInstant(instant).atZone(zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.LocalDateTime} 转换为 {@link org.joda.time.LocalDateTime}
|
||||
@ -541,9 +577,13 @@ public class DateTimeTools {
|
||||
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link org.joda.time.LocalDateTime} 转换为 {@link java.time.LocalDateTime}
|
||||
@ -557,9 +597,13 @@ public class DateTimeTools {
|
||||
return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||
@ -581,9 +625,13 @@ public class DateTimeTools {
|
||||
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 获取指定日期所在季度
|
||||
@ -646,10 +694,12 @@ public class DateTimeTools {
|
||||
return YearQuarter.of(date);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// #region - start & end
|
||||
// ================================
|
||||
|
||||
public static LocalDate startDateOfYear(int year) {
|
||||
@ -668,6 +718,14 @@ public class DateTimeTools {
|
||||
return date.plusDays(1L).atStartOfDay(zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - start & end
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// ================================
|
||||
|
||||
public static Range<LocalDateTime> toDateTimeRange(LocalDate date) {
|
||||
return Range.closedOpen(date.atStartOfDay(), startOfNextDate(date));
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@ -42,7 +41,7 @@ public final class EnumTools {
|
||||
* @deprecated 不推荐使用枚举的 ordinal。
|
||||
*/
|
||||
@Deprecated
|
||||
private static <E extends Enum<?>> E valueOfInternal(@Nonnull Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
private static <E extends Enum<?>> E valueOfInternal(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
E[] values = enumType.getEnumConstants();
|
||||
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
|
||||
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||
@ -76,7 +75,7 @@ public final class EnumTools {
|
||||
*/
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用,但不删掉
|
||||
@Nullable Integer ordinal, E defaultValue) {
|
||||
@Nullable Integer ordinal, @Nullable E defaultValue) {
|
||||
AssertTools.checkNotNull(enumType);
|
||||
return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
@ -190,7 +189,7 @@ public final class EnumTools {
|
||||
*/
|
||||
@Nullable
|
||||
private static <E extends Enum<?>> Integer checkOrdinalOrDefaultInternal(
|
||||
@Nonnull Class<E> enumType,
|
||||
Class<E> enumType,
|
||||
@Nullable Integer ordinal,
|
||||
@Nullable Integer defaultValue) {
|
||||
return ordinal != null
|
||||
@ -200,9 +199,9 @@ public final class EnumTools {
|
||||
|
||||
@Nullable
|
||||
private static <E extends Enum<?>> Integer checkOrdinalOrGetInternal(
|
||||
@Nonnull Class<E> enumType,
|
||||
Class<E> enumType,
|
||||
@Nullable Integer ordinal,
|
||||
@Nonnull Supplier<Integer> defaultValueSupplier) {
|
||||
Supplier<Integer> defaultValueSupplier) {
|
||||
return ordinal != null
|
||||
? checkOrdinal(enumType, ordinal)
|
||||
: defaultValueSupplier.get();
|
||||
|
@ -24,6 +24,8 @@ import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
/**
|
||||
@ -54,6 +56,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||
return name;
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
@Override
|
||||
public final int compareTo(final T o) {
|
||||
return Integer.compare(this.id, o.id);
|
||||
@ -65,7 +68,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(final Object obj) {
|
||||
public final boolean equals(@Nullable final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
|
@ -21,8 +21,6 @@ import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* ID 生成器
|
||||
*
|
||||
@ -50,7 +48,7 @@ public class IdGenerator {
|
||||
return toSimpleString(UUID.randomUUID());
|
||||
}
|
||||
|
||||
public static String toSimpleString(@Nonnull UUID uuid) {
|
||||
public static String toSimpleString(UUID uuid) {
|
||||
AssertTools.checkArgument(Objects.nonNull(uuid));
|
||||
return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) +
|
||||
uuidDigits(uuid.getMostSignificantBits() >> 16, 4) +
|
||||
|
@ -22,6 +22,8 @@ import java.util.Enumeration;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
|
||||
|
||||
/**
|
||||
@ -115,7 +117,7 @@ public class IdWorker {
|
||||
* init workerId
|
||||
* @param workerId if null, then auto generate one
|
||||
*/
|
||||
private void initWorkerId(Long workerId) {
|
||||
private void initWorkerId(@Nullable Long workerId) {
|
||||
if (workerId == null) {
|
||||
workerId = generateWorkerId();
|
||||
}
|
||||
|
@ -22,8 +22,6 @@ import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* 随机工具类
|
||||
* <p>
|
||||
@ -68,20 +66,20 @@ public final class RandomTools {
|
||||
* @param length 字符串长度
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomStr(@Nonnull Random random, @Nonnull char[] sourceCharacters, int length) {
|
||||
public static String randomStr(Random random, char[] sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(random, sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String randomStr(@Nonnull char[] sourceCharacters, int length) {
|
||||
public static String randomStr(char[] sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String secureRandomStr(@Nonnull char[] sourceCharacters, int length) {
|
||||
public static String secureRandomStr(char[] sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
||||
@ -97,20 +95,20 @@ public final class RandomTools {
|
||||
* @param length 字符串长度
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomStr(@Nonnull Random random, @Nonnull String sourceCharacters, int length) {
|
||||
public static String randomStr(Random random, String sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(random, sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String randomStr(@Nonnull String sourceCharacters, int length) {
|
||||
public static String randomStr(String sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String secureRandomStr(@Nonnull String sourceCharacters, int length) {
|
||||
public static String secureRandomStr(String sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
||||
@ -126,7 +124,7 @@ public final class RandomTools {
|
||||
* @param length 字符串长度
|
||||
* @return 随机字符串
|
||||
*/
|
||||
private static String randomStrInternal(@Nonnull Random random, @Nonnull char[] sourceCharacters, int length) {
|
||||
private static String randomStrInternal(Random random, char[] sourceCharacters, int length) {
|
||||
if (length == 0) {
|
||||
return StringTools.EMPTY_STRING;
|
||||
}
|
||||
@ -147,7 +145,7 @@ public final class RandomTools {
|
||||
* @param length 字符串长度
|
||||
* @return 随机字符串
|
||||
*/
|
||||
private static String randomStrInternal(@Nonnull Random random, @Nonnull String sourceCharacters, int length) {
|
||||
private static String randomStrInternal(Random random, String sourceCharacters, int length) {
|
||||
if (length == 0) {
|
||||
return StringTools.EMPTY_STRING;
|
||||
}
|
||||
|
@ -272,7 +272,7 @@ public final class RegexTools {
|
||||
* @return {@link Pattern} 实例
|
||||
*/
|
||||
@Nonnull
|
||||
private static Pattern cacheAndGetPatternInternal(@Nonnull final String pattern) {
|
||||
private static Pattern cacheAndGetPatternInternal(final String pattern) {
|
||||
if (PATTERN_CACHE.size() < MAX_CACHE_SIZE) {
|
||||
return PATTERN_CACHE.computeIfAbsent(pattern, Pattern::compile);
|
||||
}
|
||||
@ -290,7 +290,7 @@ public final class RegexTools {
|
||||
* @return {@link Pattern} 实例
|
||||
*/
|
||||
@Nonnull
|
||||
private static Pattern getPatternInternal(@Nonnull final String pattern) {
|
||||
private static Pattern getPatternInternal(final String pattern) {
|
||||
Pattern result = PATTERN_CACHE.get(pattern);
|
||||
if (result == null) {
|
||||
result = Pattern.compile(pattern);
|
||||
@ -305,7 +305,7 @@ public final class RegexTools {
|
||||
* @return {@link Pattern} 实例数组
|
||||
*/
|
||||
@Nonnull
|
||||
private static Pattern[] cacheAndGetPatternsInternal(@Nonnull final String[] patterns) {
|
||||
private static Pattern[] cacheAndGetPatternsInternal(final String[] patterns) {
|
||||
return Arrays.stream(patterns)
|
||||
.map(RegexTools::cacheAndGetPatternInternal)
|
||||
.toArray(Pattern[]::new);
|
||||
@ -318,7 +318,7 @@ public final class RegexTools {
|
||||
* @return {@link Pattern} 实例数组
|
||||
*/
|
||||
@Nonnull
|
||||
private static Pattern[] getPatternsInternal(@Nonnull final String[] patterns) {
|
||||
private static Pattern[] getPatternsInternal(final String[] patterns) {
|
||||
return Arrays.stream(patterns)
|
||||
.map(RegexTools::getPatternInternal)
|
||||
.toArray(Pattern[]::new);
|
||||
@ -331,17 +331,17 @@ public final class RegexTools {
|
||||
* @param pattern 正则
|
||||
* @return 判断结果
|
||||
*/
|
||||
private static boolean matchesInternal(@Nullable final CharSequence input, @Nonnull final Pattern pattern) {
|
||||
private static boolean matchesInternal(@Nullable final CharSequence input, final Pattern pattern) {
|
||||
return input != null && pattern.matcher(input).matches();
|
||||
}
|
||||
|
||||
private static boolean matchesOneInternal(@Nullable final CharSequence input, @Nonnull final Pattern[] patterns) {
|
||||
private static boolean matchesOneInternal(@Nullable final CharSequence input, final Pattern[] patterns) {
|
||||
return input != null
|
||||
&& Arrays.stream(patterns)
|
||||
.anyMatch(pattern -> pattern.matcher(input).matches());
|
||||
}
|
||||
|
||||
private static boolean matchesAllInternal(@Nullable final CharSequence input, @Nonnull final Pattern[] patterns) {
|
||||
private static boolean matchesAllInternal(@Nullable final CharSequence input, final Pattern[] patterns) {
|
||||
return input != null
|
||||
&& Arrays.stream(patterns)
|
||||
.allMatch(pattern -> pattern.matcher(input).matches());
|
||||
|
@ -18,14 +18,22 @@ package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@Beta
|
||||
/**
|
||||
* StringTools
|
||||
*
|
||||
* <p>
|
||||
* 字符串工具类。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
public class StringTools {
|
||||
|
||||
public static final String EMPTY_STRING = "";
|
||||
|
||||
public static boolean isNotBlank(final String cs) {
|
||||
public static boolean isNotBlank(@Nullable final String cs) {
|
||||
if (cs == null || cs.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
@ -41,7 +49,7 @@ public class StringTools {
|
||||
return repeat(str, times, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
public static String repeat(String str, int times, int maxLength) {
|
||||
public static String repeat(final String str, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(str));
|
||||
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
}
|
||||
|
||||
public TreeBuilder(Function<T, TIdentity> identityGetter, Function<T, Optional<TIdentity>> parentIdentityGetter,
|
||||
BiConsumer<TSubTree, T> addChild, Comparator<? super T> defaultComparator) {
|
||||
BiConsumer<TSubTree, T> addChild, @Nullable Comparator<? super T> defaultComparator) {
|
||||
this.identityGetter = identityGetter;
|
||||
this.parentIdentityGetter = parentIdentityGetter;
|
||||
this.addChildMethod = addChild;
|
||||
|
@ -22,4 +22,7 @@
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
@ -36,6 +36,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@Slf4j
|
||||
@SuppressWarnings("null")
|
||||
public class YearQuarterTests {
|
||||
|
||||
// ================================
|
||||
@ -588,7 +589,7 @@ public class YearQuarterTests {
|
||||
Year.MIN_VALUE,
|
||||
Year.MAX_VALUE,
|
||||
})
|
||||
void of_ValidYearMonth_CreatesYearMnoth_Q1(int year) {
|
||||
void of_ValidYearMonth_CreatesYearMonth_Q1(int year) {
|
||||
{
|
||||
YearMonth yearMonth = YearMonth.of(year, 1);
|
||||
YearQuarter yearQuarter = YearQuarter.of(yearMonth);
|
||||
@ -619,7 +620,7 @@ public class YearQuarterTests {
|
||||
Year.MIN_VALUE,
|
||||
Year.MAX_VALUE,
|
||||
})
|
||||
void of_ValidYearMonth_CreatesYearMnoth_Q2(int year) {
|
||||
void of_ValidYearMonth_CreatesYearMonth_Q2(int year) {
|
||||
{
|
||||
YearMonth yearMonth = YearMonth.of(year, 4);
|
||||
YearQuarter yearQuarter = YearQuarter.of(yearMonth);
|
||||
@ -650,7 +651,7 @@ public class YearQuarterTests {
|
||||
Year.MIN_VALUE,
|
||||
Year.MAX_VALUE,
|
||||
})
|
||||
void of_ValidYearMonth_CreatesYearMnoth_Q3(int year) {
|
||||
void of_ValidYearMonth_CreatesYearMonth_Q3(int year) {
|
||||
{
|
||||
YearMonth yearMonth = YearMonth.of(year, 7);
|
||||
YearQuarter yearQuarter = YearQuarter.of(yearMonth);
|
||||
@ -681,7 +682,7 @@ public class YearQuarterTests {
|
||||
Year.MIN_VALUE,
|
||||
Year.MAX_VALUE,
|
||||
})
|
||||
void of_ValidYearMonth_CreatesYearMnoth_Q4(int year) {
|
||||
void of_ValidYearMonth_CreatesYearMonth_Q4(int year) {
|
||||
{
|
||||
YearMonth yearMonth = YearMonth.of(year, 10);
|
||||
YearQuarter yearQuarter = YearQuarter.of(yearMonth);
|
||||
@ -712,7 +713,7 @@ public class YearQuarterTests {
|
||||
Year.MIN_VALUE,
|
||||
Year.MAX_VALUE,
|
||||
})
|
||||
void of_NullYearMonth_CreatesYearMnoth_Q4(int year) {
|
||||
void of_NullYearMonth_CreatesYearMonth_Q4(int year) {
|
||||
YearMonth yearMonth = null;
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> YearQuarter.of(yearMonth));
|
||||
|
@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@SuppressWarnings("null")
|
||||
public class BigDecimalsTests {
|
||||
|
||||
@Test
|
||||
|
@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@SuppressWarnings({"deprecation", "null"})
|
||||
public
|
||||
class EnumToolsTests {
|
||||
|
||||
|
@ -35,6 +35,7 @@ import org.junit.jupiter.api.Test;
|
||||
/**
|
||||
* {@link OptionalTools} 单元测试
|
||||
*/
|
||||
@SuppressWarnings("null")
|
||||
public
|
||||
class OptionalToolsTests {
|
||||
|
||||
|
@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@SuppressWarnings("null")
|
||||
public
|
||||
class RegexToolsTests {
|
||||
|
||||
|
@ -28,6 +28,7 @@ import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@SuppressWarnings("null")
|
||||
public
|
||||
class StringToolsTests {
|
||||
|
||||
|
@ -39,6 +39,7 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
@SuppressWarnings("null")
|
||||
class TreeBuilderTests {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TreeBuilderTests.class);
|
||||
|
Loading…
x
Reference in New Issue
Block a user