Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve tree tool #24

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions uno-core/src/main/java/cc/allio/uno/core/api/Single.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cc.allio.uno.core.api;

import cc.allio.uno.core.exception.Exceptions;
import cc.allio.uno.core.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
* description 'single' value transfer and filter.
*
* @author j.x
* @date 2024/7/6 17:57
* @since 0.1.1
* @see Stream
*/
public interface Single<E> {

/**
* from {@link List} single
*
* @param list the list
* @return the {@link Single} instance
*/
static <E> Single<E> from(List<E> list) {
if (CollectionUtils.isEmpty(list)) {
throw Exceptions.unNull("list");
}
return new InternalSingle<>(list.stream());
}

/**
* from {@link Set} single
*
* @param set the set
* @return the {@link Single} instance
*/
static <E> Single<E> from(Set<E> set) {
if (CollectionUtils.isEmpty(set)) {
throw Exceptions.unNull("set");
}
return new InternalSingle<>(set.stream());
}

/**
* from {@link Map} single
*
* @param map the map
* @return the {@link Single} instance
*/
static <E, K, V> Single<E> from(Map<K, V> map) {
if (CollectionUtils.isEmpty(map)) {
throw Exceptions.unNull("map");
}
return (Single<E>) new InternalSingle<>(map.entrySet().stream());
}

/**
* from {@link Stream} single
*
* @param stream the stream
* @return the {@link Single} instance
*/
static <E> Single<E> from(Stream<E> stream) {
if (stream == null) {
throw Exceptions.unNull("map");
}
return new InternalSingle<>(stream);
}

/**
* single elements trigger map operator
*
* @param func the map function
* @return the {@link Single} instance
*/
<V> Single<V> map(Function<E, V> func);

/**
* single elements trigger filter operator
*
* @param predicate the predicate function
* @return the {@link Single} instance
*/
Single<E> filter(Predicate<E> predicate);

/**
* foreach all elements, if one of through predicate, then return element of {@link Optional} instance, otherwise
* return empty
*
* @param predicate the element predicate
* @return the {@link Optional} instance
*/
Optional<E> forReturn(Predicate<E> predicate);

class InternalSingle<E> implements Single<E>, Self<InternalSingle<E>> {

private Stream<E> stream;

public InternalSingle(Stream<E> stream) {
this.stream = stream;
}

@Override
public <V> Single<V> map(Function<E, V> func) {
return new InternalSingle<>(stream.map(func));
}

@Override
public Single<E> filter(Predicate<E> predicate) {
this.stream = stream.filter(predicate);
return self();
}

@Override
public Optional<E> forReturn(Predicate<E> predicate) {
List<E> elements = stream.toList();
for (E element : elements) {
if (predicate.test(element)) {
return Optional.ofNullable(element);
}
}
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.allio.uno.core.concurrent;

import cc.allio.uno.core.api.Single;
import com.google.common.collect.Maps;

import java.util.Map;
Expand All @@ -14,8 +15,8 @@
public class MultiCheckedException extends Exception {

// 受检查的异常
final Map<Class<? extends Throwable>, Throwable> checked = Maps.newHashMap();
final Map<Class<? extends RuntimeException>, RuntimeException> unchecked = Maps.newHashMap();
final transient Map<Class<? extends Throwable>, Throwable> checked = Maps.newHashMap();
final transient Map<Class<? extends RuntimeException>, RuntimeException> unchecked = Maps.newHashMap();

/**
* 追加异常
Expand All @@ -39,11 +40,14 @@ public <T extends Throwable> void appendException(T ex) {
* @return Exception or null
*/
public <T extends RuntimeException> T findUncheckedException(Class<T> errType) {
RuntimeException err = unchecked.get(errType);
if (err != null) {
return (T) err;
if (hasUncheckedException(errType)) {
return (T) unchecked.get(errType);
}
return null;
var err = Single.from(unchecked.entrySet())
.forReturn(errEntry -> errEntry.getKey().isAssignableFrom(errType))
.map(Map.Entry::getValue)
.orElse(null);
return (T) err;
}

/**
Expand All @@ -54,11 +58,15 @@ public <T extends RuntimeException> T findUncheckedException(Class<T> errType) {
* @return Exception or null
*/
public <T extends Throwable> T findCheckedException(Class<T> errType) {
Throwable err = checked.get(errType);
if (err != null) {
return (T) err;
if (hasCheckedException(errType)) {
return (T) checked.get(errType);
}
return null;
var err = Single.from(checked.entrySet())
.forReturn(errEntry -> errEntry.getKey().isAssignableFrom(errType))
.map(Map.Entry::getValue)
.orElse(null);

return (T) err;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static <T, R> Stream<R> doExpandFn(Collection<T> forest, MethodFunction<T,Collec
String fieldName = childrenFunc.getFieldName();
Collection<T> children = BeanWrapper.getValue(element, fieldName, Collection.class);
if (CollectionUtils.isNotEmpty(children)) {
return doExpandFn(forest, childrenFunc, transfer);
return doExpandFn(children, childrenFunc, transfer);
}
return Stream.of(transfer.apply(element));
});
Expand Down
Loading