Skip to content

Commit

Permalink
v0.0.5
Browse files Browse the repository at this point in the history
Date utils extension
String utils extension
Collection utils extension
Random utils extension
Boolean utils extension
  • Loading branch information
HijackerMax committed Apr 19, 2023
1 parent 1253d9a commit c5fd890
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ Just add this to your **pom.xml**
<dependency>
<groupId>com.hijackermax</groupId>
<artifactId>utils</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
</dependency>
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.hijackermax</groupId>
<artifactId>utils</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>

<name>utils</name>
<description>A set of utils that can help in app development</description>
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/com/hijackermax/utils/lang/BooleanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
104 changes: 98 additions & 6 deletions src/main/java/com/hijackermax/utils/lang/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static <T> Stream<T> safeFilteredStreamOf(Collection<T> input, Predicate<
* @since 0.0.1
*/
public static <T> Stream<T> safeStreamOf(T[] input) {
return Objects.isNull(input) ? Stream.empty() : Stream.of(input);
return Objects.isNull(input) ? Stream.empty() : Arrays.stream(input);
}

/**
Expand Down Expand Up @@ -257,14 +257,13 @@ public static <I> List<I> 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 <I> 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
*/
Expand Down Expand Up @@ -369,8 +368,7 @@ public static <I> I first(Collection<? extends I> collection) {
* @param left fist input {@link Collection}
* @param right second input {@link Collection}
* @param <I> 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 <I> boolean haveSameElements(Collection<I> left, Collection<I> right) {
Expand Down Expand Up @@ -664,4 +662,98 @@ public static <K, V> Predicate<Map.Entry<K, V>> 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 <I> 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 <I> 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 <T> input type
* @param <X> 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 <T, X extends Collection<? super T>> X notEmptyOrElseGet(X source, Supplier<X> 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 <T> 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 <T> 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 <T> 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 <T> 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 <T> 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 <T> 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 <T> 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 <T> boolean safeContainsAny(Collection<? super T> source, Collection<? extends T> target) {
return Objects.nonNull(source) && Objects.nonNull(target) && target.stream()
.anyMatch(source::contains);
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/hijackermax/utils/lang/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
*
Expand All @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/hijackermax/utils/lang/RandomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/hijackermax/utils/lang/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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() {
Expand All @@ -29,6 +31,7 @@ public double getX() {

/**
* Returns y coordinate of point represented by this instance of MercatorCoordinates
*
* @return y coordinate
*/
public double getY() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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() {
Expand All @@ -29,6 +31,7 @@ public double getLongitude() {

/**
* Returns latitude of point represented by this instance of WSG84Coordinates
*
* @return latitude value
*/
public double getLatitude() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit c5fd890

Please sign in to comment.