diff --git a/README.md b/README.md
index 6c66761..72fe9ba 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,6 @@ Just add this to your **pom.xml**
com.hijackermax
utils
- 0.0.4
+ 0.0.5
```
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index daf8030..d68acd5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.hijackermax
utils
- 0.0.4
+ 0.0.5
utils
A set of utils that can help in app development
diff --git a/src/main/java/com/hijackermax/utils/lang/BooleanUtils.java b/src/main/java/com/hijackermax/utils/lang/BooleanUtils.java
index 0a3306b..0faa3d3 100644
--- a/src/main/java/com/hijackermax/utils/lang/BooleanUtils.java
+++ b/src/main/java/com/hijackermax/utils/lang/BooleanUtils.java
@@ -95,9 +95,9 @@ public static boolean isNotTrue(Boolean value) {
}
/**
- * Checks if value of provided {@link String} corresponds to group of true or false expressions
+ * Maps value of provided {@link String} to corresponding group of true or false expressions
*
- * @param value {@link String} that should be evaluated
+ * @param value {@link String} that should be mapped
* @return {@code Boolean.TRUE} if provided {@link String} has value that corresponds to group of true expressions,
* {@code Boolean.FALSE} if provided {@link String} has value that corresponds to group of false expressions,
* otherwise null
@@ -118,4 +118,25 @@ public static Boolean parseBoolean(String value) {
return null;
}
+ /**
+ * Checks if value of provided {@link String} corresponds to group of true or false expressions
+ *
+ * @param value {@link String} that should be evaluated
+ * @return true if provided {@link String} equal to one of the true or false expressions, false otherwise
+ * @since 0.0.5
+ */
+ public static boolean isBoolean(String value) {
+ return Objects.nonNull(parseBoolean(value));
+ }
+
+ /**
+ * Checks if value of provided {@link String} does not correspond to group of true or false expressions
+ *
+ * @param value {@link String} that should be evaluated
+ * @return false if provided {@link String} equal to one of the true or false expressions, true otherwise
+ * @since 0.0.5
+ */
+ public static boolean isNotBoolean(String value) {
+ return Objects.isNull(parseBoolean(value));
+ }
}
diff --git a/src/main/java/com/hijackermax/utils/lang/CollectionUtils.java b/src/main/java/com/hijackermax/utils/lang/CollectionUtils.java
index a5a1300..3fd9434 100644
--- a/src/main/java/com/hijackermax/utils/lang/CollectionUtils.java
+++ b/src/main/java/com/hijackermax/utils/lang/CollectionUtils.java
@@ -92,7 +92,7 @@ public static Stream safeFilteredStreamOf(Collection input, Predicate<
* @since 0.0.1
*/
public static Stream safeStreamOf(T[] input) {
- return Objects.isNull(input) ? Stream.empty() : Stream.of(input);
+ return Objects.isNull(input) ? Stream.empty() : Arrays.stream(input);
}
/**
@@ -257,14 +257,13 @@ public static List filter(Collection extends I> input, Predicate supe
}
/**
- * Conducts null-safe check if left input {@link Collection}
- * contains all elements from right input {@link Collection}
+ * Checks if left input {@link Collection} contains all elements from right input {@link Collection}
*
* @param left {@link Collection} that should be checked if contains all elements from right collection
* @param right {@link Collection} that should be tested
* @param input type
* @return true if left {@link Collection} contains all elements from right {@link Collection}
- * or both collections are null, {@code false} if left {@link Collection} does not contains all elements
+ * or both collections are null, false if left {@link Collection} does not contain all elements
* from right {@link Collection} or one of the collections is null
* @since 0.0.1
*/
@@ -369,8 +368,7 @@ public static I first(Collection extends I> collection) {
* @param left fist input {@link Collection}
* @param right second input {@link Collection}
* @param input collection elements type
- * @return true if both {@link Collection} have same elements inside,
- * {@code false} if there is a difference in elements
+ * @return true if both {@link Collection} have same elements inside, false if there is a difference in elements
* @since 0.0.1
*/
public static boolean haveSameElements(Collection left, Collection right) {
@@ -664,4 +662,98 @@ public static Predicate> valuePredicate(Predicate super
StringBuffer::toString
);
}
+
+ /**
+ * Checks if input {@link Collection} contains provided value
+ *
+ * @param source {@link Collection} that should be checked
+ * @param value that can be present in input collection
+ * @param input type
+ * @return true if input {@link Collection} contains provided value,
+ * false if input {@link Collection} does not contain provided value or one of the arguments is null
+ * @since 0.0.5
+ */
+ public static boolean safeContains(Collection super I> source, I value) {
+ return Objects.nonNull(source) && Objects.nonNull(value) && source.contains(value);
+ }
+
+ /**
+ * Provides input {@link Collection} if non-empty or gets fallback one from provided fallback {@link Supplier}
+ *
+ * @param source {@link Collection} that can be empty or null
+ * @param fallback {@link Collection} {@link Supplier} that will supply collection in case of
+ * source one if empty or null
+ * @param input type
+ * @param input collection type
+ * @return input {@link Collection} if not empty or null,
+ * otherwise {@link Collection} supplied by provided fallback {@link Supplier}
+ * @since 0.0.5
+ */
+ public static > X notEmptyOrElseGet(X source, Supplier fallback) {
+ Objects.requireNonNull(fallback, "Fallback supplier must not be null");
+ return isEmpty(source) ? fallback.get() : source;
+ }
+
+ /**
+ * Checks if source input {@link Collection} contains any of elements from vararg array
+ *
+ * @param source {@link Collection} that should be checked if contains any of elements from vararg array
+ * @param options vararg array that should be tested
+ * @param input type
+ * @return true if source {@link Collection} is not null and contains some elements from vararg array,
+ * otherwise false
+ * @since 0.0.5
+ */
+ @SafeVarargs
+ public static boolean safeContainsAnyArg(Collection super T> source, T... options) {
+ return Objects.nonNull(source) && Arrays.stream(options)
+ .anyMatch(source::contains);
+ }
+
+ /**
+ * Checks if source input {@link Collection} contains all elements from vararg array
+ *
+ * @param source {@link Collection} that should be checked if contains all elements from vararg array
+ * @param options vararg array that should be tested
+ * @param input type
+ * @return true if source {@link Collection} is not null and contains all elements from vararg array,
+ * otherwise false
+ * @since 0.0.5
+ */
+ @SafeVarargs
+ public static boolean safeContainsAllArg(Collection super T> source, T... options) {
+ return Objects.nonNull(source) && Arrays.stream(options)
+ .allMatch(source::contains);
+ }
+
+ /**
+ * Checks if source input {@link Collection} does not contain any of elements from vararg array
+ *
+ * @param source {@link Collection} that should be checked if not contains any of elements from vararg array
+ * @param options vararg array that should be tested
+ * @param input type
+ * @return true if source {@link Collection} is not null and does not contain any of elements from vararg array,
+ * otherwise false
+ * @since 0.0.5
+ */
+ @SafeVarargs
+ public static boolean safeNotContainsArg(Collection super T> source, T... options) {
+ return Objects.nonNull(source) && Arrays.stream(options)
+ .noneMatch(source::contains);
+ }
+
+ /**
+ * Checks if source input {@link Collection} contains some elements from target input {@link Collection}
+ *
+ * @param source {@link Collection} that should be checked if contains all elements from target collection
+ * @param target {@link Collection} that should be tested
+ * @param input type
+ * @return true if both {@link Collection} are not null and input {@link Collection} contains some elements
+ * from target {@link Collection}, otherwise false
+ * @since 0.0.5
+ */
+ public static boolean safeContainsAny(Collection super T> source, Collection extends T> target) {
+ return Objects.nonNull(source) && Objects.nonNull(target) && target.stream()
+ .anyMatch(source::contains);
+ }
}
diff --git a/src/main/java/com/hijackermax/utils/lang/DateUtils.java b/src/main/java/com/hijackermax/utils/lang/DateUtils.java
index 762ad1c..089ed07 100644
--- a/src/main/java/com/hijackermax/utils/lang/DateUtils.java
+++ b/src/main/java/com/hijackermax/utils/lang/DateUtils.java
@@ -2,6 +2,8 @@
import java.time.Instant;
import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Objects;
@@ -109,6 +111,44 @@ public static Date shiftDateByDays(Date source, int shiftValue) {
return Date.from(source.toInstant().plus(shiftValue, ChronoUnit.DAYS));
}
+ /**
+ * Shifts provided {@link Date} with provided amount of months
+ *
+ * @param source source date
+ * @param shiftValue amount of months that should be used to shift source date
+ * @return shifted date, or null if source is null
+ * @since 0.0.5
+ */
+ public static Date shiftDateByMonths(Date source, int shiftValue) {
+ if (Objects.isNull(source)) {
+ return null;
+ }
+ return Date.from(
+ getZonedDateTime(source)
+ .plusMonths(shiftValue)
+ .toInstant()
+ );
+ }
+
+ /**
+ * Shifts provided {@link Date} with provided amount of years
+ *
+ * @param source source date
+ * @param shiftValue amount of years that should be used to shift source date
+ * @return shifted date, or null if source is null
+ * @since 0.0.5
+ */
+ public static Date shiftDateByYears(Date source, int shiftValue) {
+ if (Objects.isNull(source)) {
+ return null;
+ }
+ return Date.from(
+ getZonedDateTime(source)
+ .plusYears(shiftValue)
+ .toInstant()
+ );
+ }
+
/**
* Provides null-safe check if provided value {@link Date} between two provided boundary {@link Date}
*
@@ -124,4 +164,8 @@ public static boolean between(Date value, Date leftBound, Date rightBound) {
}
return leftBound.before(value) && rightBound.after(value);
}
+
+ private static ZonedDateTime getZonedDateTime(Date source) {
+ return ZonedDateTime.ofInstant(source.toInstant(), ZoneOffset.UTC);
+ }
}
diff --git a/src/main/java/com/hijackermax/utils/lang/RandomUtils.java b/src/main/java/com/hijackermax/utils/lang/RandomUtils.java
index 208acc2..e325715 100644
--- a/src/main/java/com/hijackermax/utils/lang/RandomUtils.java
+++ b/src/main/java/com/hijackermax/utils/lang/RandomUtils.java
@@ -68,6 +68,19 @@ public static String randomNumericString(int length) {
return randomStringSequence(NUMERIC, length);
}
+ /**
+ * Provides random byte array with predefined length
+ *
+ * @param length output array length
+ * @return byte array of predefined length
+ * @since 0.0.5
+ */
+ public static byte[] randomBytes(int length) {
+ byte[] result = new byte[length];
+ SECURE_RANDOM.nextBytes(result);
+ return result;
+ }
+
/**
* Provides shuffled array of int range with provided boundaries
*
diff --git a/src/main/java/com/hijackermax/utils/lang/StringUtils.java b/src/main/java/com/hijackermax/utils/lang/StringUtils.java
index afc9f57..ef880af 100644
--- a/src/main/java/com/hijackermax/utils/lang/StringUtils.java
+++ b/src/main/java/com/hijackermax/utils/lang/StringUtils.java
@@ -312,4 +312,16 @@ public static String decompress(String source) throws IOException {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
+
+ /**
+ * Returs provided value {@link String} or default {@link String} if value is null, empty or blank
+ *
+ * @param value that can be null, blank or empty
+ * @param defaultValue fallback value
+ * @return value if it is not null, blank or empty, otherwise defaultValue
+ * @since 0.0.5
+ */
+ public static String notBlankOrElse(String value, String defaultValue) {
+ return isBlank(value) ? defaultValue : value;
+ }
}
diff --git a/src/main/java/com/hijackermax/utils/models/MercatorCoordinates.java b/src/main/java/com/hijackermax/utils/models/MercatorCoordinates.java
index 90a0e86..ce78598 100644
--- a/src/main/java/com/hijackermax/utils/models/MercatorCoordinates.java
+++ b/src/main/java/com/hijackermax/utils/models/MercatorCoordinates.java
@@ -11,6 +11,7 @@ public class MercatorCoordinates {
/**
* Creates new instance of MercatorCoordinates with provided x and y coordinate
+ *
* @param x x - coordinate
* @param y y - coordinate
*/
@@ -21,6 +22,7 @@ public MercatorCoordinates(double x, double y) {
/**
* Returns x coordinate of point represented by this instance of MercatorCoordinates
+ *
* @return x coordinate
*/
public double getX() {
@@ -29,6 +31,7 @@ public double getX() {
/**
* Returns y coordinate of point represented by this instance of MercatorCoordinates
+ *
* @return y coordinate
*/
public double getY() {
diff --git a/src/main/java/com/hijackermax/utils/models/WSG84Coordinates.java b/src/main/java/com/hijackermax/utils/models/WSG84Coordinates.java
index 30cef42..83f39b1 100644
--- a/src/main/java/com/hijackermax/utils/models/WSG84Coordinates.java
+++ b/src/main/java/com/hijackermax/utils/models/WSG84Coordinates.java
@@ -11,6 +11,7 @@ public class WSG84Coordinates {
/**
* Creates new instance of WSG84Coordinates with provided longitude and latitude
+ *
* @param longitude of point
* @param latitude of point
*/
@@ -21,6 +22,7 @@ public WSG84Coordinates(double longitude, double latitude) {
/**
* Returns longitude of point represented by this instance of WSG84Coordinates
+ *
* @return longitude value
*/
public double getLongitude() {
@@ -29,6 +31,7 @@ public double getLongitude() {
/**
* Returns latitude of point represented by this instance of WSG84Coordinates
+ *
* @return latitude value
*/
public double getLatitude() {
diff --git a/src/test/java/com/hijackermax/switches/ActionsDynamicSwitchTest.java b/src/test/java/com/hijackermax/switches/ActionsDynamicSwitchTest.java
index 9e970e0..a645e55 100644
--- a/src/test/java/com/hijackermax/switches/ActionsDynamicSwitchTest.java
+++ b/src/test/java/com/hijackermax/switches/ActionsDynamicSwitchTest.java
@@ -1,7 +1,7 @@
package com.hijackermax.switches;
-import com.hijackermax.utils.switches.ActionsDynamicSwitch;
import com.hijackermax.utils.entities.Single;
+import com.hijackermax.utils.switches.ActionsDynamicSwitch;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
diff --git a/src/test/java/com/hijackermax/switches/ClassDynamicSwitchTest.java b/src/test/java/com/hijackermax/switches/ClassDynamicSwitchTest.java
index c45744f..fd9cfe9 100644
--- a/src/test/java/com/hijackermax/switches/ClassDynamicSwitchTest.java
+++ b/src/test/java/com/hijackermax/switches/ClassDynamicSwitchTest.java
@@ -5,7 +5,9 @@
import com.hijackermax.utils.switches.ClassDynamicSwitch;
import org.junit.jupiter.api.Test;
-import java.util.*;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
diff --git a/src/test/java/com/hijackermax/utils/BooleanUtilsTest.java b/src/test/java/com/hijackermax/utils/BooleanUtilsTest.java
index f62bc2c..870db26 100644
--- a/src/test/java/com/hijackermax/utils/BooleanUtilsTest.java
+++ b/src/test/java/com/hijackermax/utils/BooleanUtilsTest.java
@@ -74,4 +74,58 @@ void testParseBoolean() {
assertEquals(Boolean.FALSE, BooleanUtils.parseBoolean("0"));
assertEquals(Boolean.FALSE, BooleanUtils.parseBoolean("off"));
}
+
+ @Test
+ void testIsBoolean() {
+ assertFalse(BooleanUtils.isBoolean(null));
+ assertFalse(BooleanUtils.isBoolean(StringUtils.EMPTY));
+ assertFalse(BooleanUtils.isBoolean(StringUtils.BLANK));
+ assertFalse(BooleanUtils.isBoolean("Foo"));
+ assertFalse(BooleanUtils.isBoolean("Bar"));
+ assertFalse(BooleanUtils.isBoolean("tru"));
+ assertFalse(BooleanUtils.isBoolean("yep"));
+ assertFalse(BooleanUtils.isBoolean("true 1"));
+ assertFalse(BooleanUtils.isBoolean(",true"));
+
+ assertTrue(BooleanUtils.isBoolean("true"));
+ assertTrue(BooleanUtils.isBoolean("t"));
+ assertTrue(BooleanUtils.isBoolean("y"));
+ assertTrue(BooleanUtils.isBoolean("yes"));
+ assertTrue(BooleanUtils.isBoolean("1"));
+ assertTrue(BooleanUtils.isBoolean("on"));
+ assertTrue(BooleanUtils.isBoolean("false"));
+ assertTrue(BooleanUtils.isBoolean("f"));
+ assertTrue(BooleanUtils.isBoolean("n"));
+ assertTrue(BooleanUtils.isBoolean("no"));
+ assertTrue(BooleanUtils.isBoolean("0"));
+ assertTrue(BooleanUtils.isBoolean("off"));
+ assertTrue(BooleanUtils.isBoolean("true "));
+ }
+
+ @Test
+ void testIsNotBoolean() {
+ assertFalse(BooleanUtils.isNotBoolean("true"));
+ assertFalse(BooleanUtils.isNotBoolean("t"));
+ assertFalse(BooleanUtils.isNotBoolean("y"));
+ assertFalse(BooleanUtils.isNotBoolean("yes"));
+ assertFalse(BooleanUtils.isNotBoolean("1"));
+ assertFalse(BooleanUtils.isNotBoolean("on"));
+ assertFalse(BooleanUtils.isNotBoolean("false"));
+ assertFalse(BooleanUtils.isNotBoolean("f"));
+ assertFalse(BooleanUtils.isNotBoolean("n"));
+ assertFalse(BooleanUtils.isNotBoolean("no"));
+ assertFalse(BooleanUtils.isNotBoolean("0"));
+ assertFalse(BooleanUtils.isNotBoolean("off"));
+ assertFalse(BooleanUtils.isNotBoolean("true "));
+
+ assertTrue(BooleanUtils.isNotBoolean(null));
+ assertTrue(BooleanUtils.isNotBoolean(StringUtils.EMPTY));
+ assertTrue(BooleanUtils.isNotBoolean(StringUtils.BLANK));
+ assertTrue(BooleanUtils.isNotBoolean("Foo"));
+ assertTrue(BooleanUtils.isNotBoolean("Bar"));
+ assertTrue(BooleanUtils.isNotBoolean("tru"));
+ assertTrue(BooleanUtils.isNotBoolean("yep"));
+ assertTrue(BooleanUtils.isNotBoolean("true 1"));
+ assertTrue(BooleanUtils.isNotBoolean(",true"));
+ }
}
diff --git a/src/test/java/com/hijackermax/utils/CollectionUtilsTest.java b/src/test/java/com/hijackermax/utils/CollectionUtilsTest.java
index 9c8ea09..703f3dd 100644
--- a/src/test/java/com/hijackermax/utils/CollectionUtilsTest.java
+++ b/src/test/java/com/hijackermax/utils/CollectionUtilsTest.java
@@ -473,4 +473,63 @@ void testToStringCollector() {
.collect(CollectionUtils.toStringCollector());
assertEquals(chars.length, result.length());
}
+
+ @Test
+ void testSafeContains() {
+ assertFalse(CollectionUtils.safeContains(null, null));
+ assertFalse(CollectionUtils.safeContains(null, "Test"));
+ assertFalse(CollectionUtils.safeContains(List.of(1), null));
+ assertFalse(CollectionUtils.safeContains(List.of(1, 2, 3, 4, 5), 6));
+ assertFalse(CollectionUtils.safeContains(List.of(1, 2, 3, 4, 5), "String"));
+ assertTrue(CollectionUtils.safeContains(List.of(1, 2, 3, 4, 5), 2));
+ assertTrue(CollectionUtils.safeContains(List.of("Foo", "Bar", "Test"), "Bar"));
+ }
+
+ @Test
+ void testNotEmptyOrElseGet() {
+ assertEquals(List.of(1, 2, 3), CollectionUtils.notEmptyOrElseGet(List.of(1, 2, 3), () -> List.of(4, 5, 6)));
+ assertEquals(List.of(4, 5, 6), CollectionUtils.notEmptyOrElseGet(null, () -> List.of(4, 5, 6)));
+ assertEquals(List.of(4, 5, 6), CollectionUtils.notEmptyOrElseGet(List.of(), () -> List.of(4, 5, 6)));
+ assertThrows(NullPointerException.class, () -> CollectionUtils.notEmptyOrElseGet(null, null));
+ }
+
+ @Test
+ void testSafeContainsAnyArg() {
+ assertFalse(CollectionUtils.safeContainsAnyArg(null, 1, 2, 3));
+ assertFalse(CollectionUtils.safeContainsAnyArg(List.of(), 1, 2, 3));
+ assertFalse(CollectionUtils.safeContainsAnyArg(List.of()));
+ assertFalse(CollectionUtils.safeContainsAnyArg(List.of(1, 2, 3, 4, 5), 0));
+ assertTrue(CollectionUtils.safeContainsAnyArg(List.of(1, 2, 3, 4, 5), 2));
+ }
+
+ @Test
+ void testSafeContainsAllArg() {
+ assertFalse(CollectionUtils.safeContainsAllArg(null, 1, 2, 3));
+ assertFalse(CollectionUtils.safeContainsAllArg(List.of(), 1, 2, 3));
+ assertFalse(CollectionUtils.safeContainsAllArg(List.of(1, 2, 3, 4, 5), 1, 2, 3, 4, 6));
+ assertTrue(CollectionUtils.safeContainsAllArg(List.of(1, 2, 3, 4, 5), 1, 2, 3, 4));
+ assertTrue(CollectionUtils.safeContainsAllArg(List.of()));
+ assertTrue(CollectionUtils.safeContainsAllArg(List.of(1, 2, 3, 4, 5), 1, 2, 3, 4, 5));
+ }
+
+ @Test
+ void testSafeNotContainsArg() {
+ assertFalse(CollectionUtils.safeNotContainsArg(null, 1, 2, 3));
+ assertFalse(CollectionUtils.safeNotContainsArg(List.of(1, 2, 3, 4, 5), 1, 2));
+ assertFalse(CollectionUtils.safeNotContainsArg(List.of(1, 2, 3, 4, 5), 1, 2, 3, 4, 5));
+ assertTrue(CollectionUtils.safeNotContainsArg(List.of(1, 2, 3, 4, 5), 7, 8, 9, 10));
+ assertTrue(CollectionUtils.safeNotContainsArg(List.of()));
+ assertTrue(CollectionUtils.safeNotContainsArg(List.of(), 1, 2, 3));
+ }
+
+ @Test
+ void testSafeContainsAny() {
+ assertFalse(CollectionUtils.safeContainsAny(null, null));
+ assertFalse(CollectionUtils.safeContainsAny(List.of(1), null));
+ assertFalse(CollectionUtils.safeContainsAny(null, List.of(1)));
+ assertFalse(CollectionUtils.safeContainsAny(List.of(), List.of(1, 2, 3)));
+ assertFalse(CollectionUtils.safeContainsAny(List.of(), List.of()));
+ assertFalse(CollectionUtils.safeContainsAny(List.of(1, 2, 3, 4, 5), List.of(0, 10)));
+ assertTrue(CollectionUtils.safeContainsAny(List.of(1, 2, 3, 4, 5), List.of(2, 10)));
+ }
}
diff --git a/src/test/java/com/hijackermax/utils/DateUtilsTest.java b/src/test/java/com/hijackermax/utils/DateUtilsTest.java
index 36b859a..a8b8151 100644
--- a/src/test/java/com/hijackermax/utils/DateUtilsTest.java
+++ b/src/test/java/com/hijackermax/utils/DateUtilsTest.java
@@ -75,6 +75,26 @@ void testShiftDayByDays() {
assertEquals(new Date(minusMillis), DateUtils.shiftDateByDays(new Date(timeMillis), -1));
}
+ @Test
+ void testShiftDayByMoths() {
+ assertNull(DateUtils.shiftDateByDays(null, 10));
+ long timeMillis = 1679970309662L; //Tue Mar 28 02:25:09.662 GMT 2023
+ long plusMillis = 1682648709662L; //Fri Apr 28 29 02:25:09.662 GMT 2023
+ long minusMillis = 1677551109662L; //Tue Feb 28 02:25:09.662 GMT 2023
+ assertEquals(new Date(plusMillis), DateUtils.shiftDateByMonths(new Date(timeMillis), 1));
+ assertEquals(new Date(minusMillis), DateUtils.shiftDateByMonths(new Date(timeMillis), -1));
+ }
+
+ @Test
+ void testShiftDayByYears() {
+ assertNull(DateUtils.shiftDateByDays(null, 10));
+ long timeMillis = 1679970309662L; //Tue Mar 28 02:25:09.662 GMT 2023
+ long plusMillis = 1711592709662L; //Thu Mar 28 02:25:09.662 GMT 2024
+ long minusMillis = 1648434309662L; //Mon Mar 28 02:25:09.662 GMT 2022
+ assertEquals(new Date(plusMillis), DateUtils.shiftDateByYears(new Date(timeMillis), 1));
+ assertEquals(new Date(minusMillis), DateUtils.shiftDateByYears(new Date(timeMillis), -1));
+ }
+
@Test
void testBetween() {
assertFalse(DateUtils.between(null, new Date(), new Date()));
diff --git a/src/test/java/com/hijackermax/utils/RandomUtilsTest.java b/src/test/java/com/hijackermax/utils/RandomUtilsTest.java
index 8f5ecec..839ec22 100644
--- a/src/test/java/com/hijackermax/utils/RandomUtilsTest.java
+++ b/src/test/java/com/hijackermax/utils/RandomUtilsTest.java
@@ -77,6 +77,12 @@ void testRandomNumericString() {
assertTrue(randomStrings.stream().map(String::length).allMatch(v -> stringLength == v));
}
+ @Test
+ void testRandomByteArray() {
+ int size = 32;
+ assertEquals(size, RandomUtils.randomBytes(size).length);
+ }
+
@Test
void testShuffleIntArray() {
int[] source = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 0xAA};
diff --git a/src/test/java/com/hijackermax/utils/StringUtilsTest.java b/src/test/java/com/hijackermax/utils/StringUtilsTest.java
index b627731..69e4282 100644
--- a/src/test/java/com/hijackermax/utils/StringUtilsTest.java
+++ b/src/test/java/com/hijackermax/utils/StringUtilsTest.java
@@ -182,4 +182,14 @@ void testCompressDecompress() throws IOException {
String decompressedString = StringUtils.decompress(compressedString);
assertEquals(testString, decompressedString);
}
+
+ @Test
+ void testNotBlankOrElse() {
+ assertEquals("Hello", StringUtils.notBlankOrElse("Hello", "Foo"));
+ assertEquals("Hello", StringUtils.notBlankOrElse("Hello", null));
+ assertEquals("Foo", StringUtils.notBlankOrElse(null, "Foo"));
+ assertEquals("Foo", StringUtils.notBlankOrElse("", "Foo"));
+ assertEquals("Foo", StringUtils.notBlankOrElse(" ", "Foo"));
+ assertNull(StringUtils.notBlankOrElse(" ", null));
+ }
}