Skip to content

Commit

Permalink
v0.1.3
Browse files Browse the repository at this point in the history
Functional utils extension
String utils extension
Collection utils extension
  • Loading branch information
HijackerMax committed Dec 21, 2023
1 parent f128769 commit 526735a
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ Just add this to your **pom.xml**
<dependency>
<groupId>com.hijackermax</groupId>
<artifactId>utils</artifactId>
<version>0.1.2</version>
<version>0.1.3</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.1.2</version>
<version>0.1.3</version>

<name>utils</name>
<description>A set of utils that can help in app development</description>
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/hijackermax/utils/entities/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import static java.util.Optional.ofNullable;
Expand All @@ -13,7 +14,7 @@
* @param <V> value type
* @since 0.0.1
*/
public class Single<V> {
public class Single<V> implements Consumer<V>, Supplier<V> {
private V value;

/**
Expand Down Expand Up @@ -122,4 +123,24 @@ public String toString() {
"value=" + value +
'}';
}

/**
* Accepts and stores provided value inside this instance of {@link Single}
*
* @param value new value
*/
@Override
public void accept(V value) {
setValue(value);
}

/**
* Returns stored value
*
* @return stored value
*/
@Override
public V get() {
return value;
}
}
26 changes: 19 additions & 7 deletions src/main/java/com/hijackermax/utils/entities/Triple.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @param <V> value type
* @since 0.0.3
*/
public class Triple<K, M, V> extends Tuple<K, V> {
public class Triple<K, M, V> extends Tuple<K, V> implements TriConsumer<K, M, V> {
private M middle;

/**
Expand All @@ -32,6 +32,12 @@ public Triple(K key, M middle, V value) {
this.middle = middle;
}

/**
* Creates new instance of {@link Triple} with null key, middle and value
*/
public Triple() {
}

/**
* Creates instance of {@link Triple} with provided key, middle and value
*
Expand All @@ -47,12 +53,6 @@ public static <K, M, V> Triple<K, M, V> of(K key, M middle, V value) {
return new Triple<>(key, middle, value);
}

/**
* Creates new instance of {@link Triple} with null key, middle and value
*/
public Triple() {
}

/**
* Returns middle of this instance of {@link Triple}
*
Expand Down Expand Up @@ -171,4 +171,16 @@ public String toString() {
"value=" + getValue() +
'}';
}

/**
* Accepts and stores provided key, middle and value inside this instance of {@link Triple}
*
* @param key new key
* @param middle new middle
* @param value new value
*/
@Override
public void accept(K key, M middle, V value) {
setThree(key, middle, value);
}
}
43 changes: 27 additions & 16 deletions src/main/java/com/hijackermax/utils/entities/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @param <V> value type
* @since 0.0.1
*/
public class Tuple<K, V> extends Single<V> {
public class Tuple<K, V> extends Single<V> implements BiConsumer<K, V> {
private K key;

/**
Expand All @@ -30,6 +30,12 @@ public Tuple(K key, V value) {
this.key = key;
}

/**
* Creates new instance of {@link Tuple} with null key and value
*/
public Tuple() {
}

/**
* Creates instance of {@link Tuple} with provided key and value
*
Expand All @@ -55,12 +61,6 @@ public static <K, V> Tuple<K, V> of(Map.Entry<K, V> entry) {
return new Tuple<>(entry.getKey(), entry.getValue());
}

/**
* Creates new instance of {@link Tuple} with null key and value
*/
public Tuple() {
}

/**
* Returns key of this instance of {@link Tuple}
*
Expand All @@ -70,6 +70,15 @@ public K getKey() {
return key;
}

/**
* Sets key to this instance of {@link Tuple}
*
* @param key new key
*/
public void setKey(K key) {
this.key = key;
}

/**
* Returns left value (key) of this instance of {@link Tuple}
*
Expand Down Expand Up @@ -106,15 +115,6 @@ public void setRight(V right) {
setValue(right);
}

/**
* Sets key to this instance of {@link Tuple}
*
* @param key new key
*/
public void setKey(K key) {
this.key = key;
}

/**
* Provides ability to modify existing key
*
Expand Down Expand Up @@ -230,4 +230,15 @@ public String toString() {
"value=" + getValue() +
'}';
}

/**
* Accepts and stores provided key and value inside this instance of {@link Tuple}
*
* @param key new key
* @param value new value
*/
@Override
public void accept(K key, V value) {
setPair(key, value);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/hijackermax/utils/lang/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Optional.ofNullable;
Expand Down Expand Up @@ -883,4 +884,23 @@ public static <K, V> Map<K, Tuple<V, V>> getDifferences(Map<? extends K, ? exten
.filter(t -> !Objects.equals(t.getMiddle(), t.getValue()))
.collect(Collectors.toMap(Triple::getKey, t -> Tuple.of(t.getMiddle(), t.getValue())));
}

/**
* Splits provided {@link List} to partitions of provided size, doesn't modify source list
*
* @param values values to partition
* @param partitionSize required size of partition
* @param <I> input type
* @return {@link List} of partitions of predefines size, empty list if source list is empty, null or partition siz is less than 1
* @since 0.1.3
*/
public static <I> List<List<I>> partition(List<I> values, int partitionSize) {
if (isEmpty(values) || 1 > partitionSize) {
return Collections.emptyList();
}
return IntStream.range(0, values.size())
.filter(idx -> 0 == idx % partitionSize)
.mapToObj(idx -> values.subList(idx, Math.min(idx + partitionSize, values.size())))
.collect(Collectors.toList());
}
}
79 changes: 76 additions & 3 deletions src/main/java/com/hijackermax/utils/lang/FunctionalUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hijackermax.utils.lang;

import com.hijackermax.utils.entities.Tuple;

import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -35,6 +37,28 @@ public static <T> Consumer<T> failSafeStrategy(Consumer<T> consumer, BiConsumer<
};
}

/**
* Provides wrapper {@link BiConsumer} which catches {@link Exception} thrown by the input value {@link BiConsumer}
* and supplies it to provided exception{@link BiConsumer}
*
* @param consumer values {@link BiConsumer} that can throw {@link Exception}
* @param exceptionConsumer input values tuple and thrown {@link Exception} {@link BiConsumer}
* @param <L> left input value type
* @param <R> right input value type
* @return wrapper {@link BiConsumer}
* @since 0.1.3
*/
public static <L, R> BiConsumer<L, R> failSafeStrategy(BiConsumer<L, R> consumer, BiConsumer<Tuple<L, R>, Exception> exceptionConsumer) {
return (left, right) -> {
try {
consumer.accept(left, right);
} catch (Exception e) {
exceptionConsumer.accept(Tuple.of(left, right), e);
}
};
}


/**
* Catches {@link Exception} thrown by provided {@link Runnable} and supplies it to provided exception{@link Consumer}
*
Expand Down Expand Up @@ -91,14 +115,63 @@ public static <T, U> Predicate<T> mappedPredicate(Function<T, U> transformer, Pr
}

/**
* Provides empty value {@link Consumer}
* Provides empty {@link Consumer}
*
* @param <T> supplied value type
* @return empty value consumer
* @return empty consumer
* @since 0.0.9
*/
public static <T> Consumer<T> emptyConsumer() {
return v -> {
return t -> {
};
}

/**
* Provides empty {@link Runnable}
*
* @return empty runnable
* @since 0.1.3
*/
public static Runnable emptyRunnable() {
return () -> {
};
}

/**
* Provides empty {@link BiConsumer}
*
* @param <L> left supplied value type
* @param <R> right supplied value type
* @return empty consumer
* @since 0.1.3
*/
public static <L, R> BiConsumer<L, R> emptyBiConsumer() {
return (l, r) -> {
};
}

/**
* Provides wrapper {@link Function} which catches {@link Exception} thrown by the input value {@link Function}
* and supplies it to provided exception{@link BiConsumer}
*
* @param function {@link Function} that can throw {@link Exception}
* @param fallback value {@link Supplier} in case of failure of {@link Function}
* @param exceptionConsumer input value and thrown {@link Exception} {@link BiConsumer}
* @param <T> input value type
* @param <R> result value type
* @return wrapper {@link Function}
* @since 0.1.3
*/
public static <T, R> Function<T, R> failSafeStrategy(Function<T, R> function,
Supplier<R> fallback,
BiConsumer<T, Exception> exceptionConsumer) {
return value -> {
try {
return function.apply(value);
} catch (Exception e) {
exceptionConsumer.accept(value, e);
}
return fallback.get();
};
}
}
Loading

0 comments on commit 526735a

Please sign in to comment.