Skip to content

Commit

Permalink
Merge pull request #17 from ClearXs/1.1.x
Browse files Browse the repository at this point in the history
1.1.x
  • Loading branch information
ClearXs authored Apr 12, 2024
2 parents a7c6d1d + 97093a0 commit a115227
Show file tree
Hide file tree
Showing 31 changed files with 415 additions and 103 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</modules>

<properties>
<java.version>21</java.version>
<java.version>22</java.version>
<drools.version>9.44.0.Final</drools.version>
<reactor-bom>2023.0.3</reactor-bom>
<spring.boot.version>3.2.4</spring.boot.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public synchronized <K extends T> Mono<K> setCoverage(K target, String name, boo
.flatMap(descriptor -> write(target, descriptor, forceCoverage, value))
.onErrorContinue((error, o) -> {
if (log.isWarnEnabled()) {
log.warn("target {} setValue field {} value error setValue empty", target.getClass().getSimpleName(), name);
log.warn("Target {} setValue field {} value error setValue empty", target.getClass().getSimpleName(), name);
}
});
}
Expand Down Expand Up @@ -297,7 +297,7 @@ private <K extends T> Mono<K> write(K target, PropertyDescriptor descriptor, boo
}
} catch (Throwable err) {
if (log.isWarnEnabled()) {
log.warn("Target {} setValue field {} value error setValue empty", target.getClass().getSimpleName(), descriptor.getName());
log.warn("Target {} setValue field {} value error. the error msg: {}", target.getClass().getSimpleName(), descriptor.getName(), err.getMessage());
}
}
return Mono.just(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import java.io.Serializable;
import java.util.Comparator;

/**
* comparable tree element base on {@link Comparator}
*
* @author j.x
* @date 2023/4/11 22:21
* @since 1.1.4
*/
public class ComparableElement<T extends ComparableElement<T>> extends DefaultElement<T> {

private final Comparator<T> comparator;
Expand All @@ -16,7 +23,12 @@ public ComparableElement(Serializable id, Comparator<T> comparator) {
public void addChildren(T element) {
super.addChildren(element);
if (comparator != null) {
getChildren().sort((o1, o2) -> comparator.compare((T) o1, (T) o2));
getChildren().sort(comparator);
}
}

@Override
public Element obtainSentinel() {
return new ComparableElement(-1, comparator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ default void accept(Visitor<T> visitor) {
/**
* 获取Sentinel结点
*/
static Element getRootSentinel() {
default Element obtainSentinel() {
return new DefaultElement<>(-1, -1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,22 @@ public static <R extends Element<R>> List<R> adjust(List<R> elements) {
}
}

Element sentinel = Element.getRootSentinel();
for (R virtual : idElement.values()) {
if (virtual.getDepth() == Element.ROOT_NODE) {
// 触发Element添加结点的特性,如排序
sentinel.addChildren(virtual);
}
}
// base on first element (if empty then return empty list) get sentinel element then traversal each element and add children (maybe trigger element feature, like as sort)
return idElement.values()
.stream()
.findFirst()
.map(Element::obtainSentinel)
.map(sentinel -> {
for (R virtual : idElement.values()) {
if (virtual.getDepth() == Element.ROOT_NODE) {
// 触发Element添加结点的特性,如排序
sentinel.addChildren(virtual);
}
}
return sentinel.getChildren();
})
.orElse(Collections.emptyList());

return sentinel.getChildren();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public Integer convert(Object target, Class<?> maybeType) {
return d.intValue();
} else if (target instanceof Float f) {
return f.intValue();
} else if (target instanceof Short s) {
return s.intValue();
}
throw new IllegalArgumentException(String.format("target %s can't cast type %s", target, maybeType));
}
Expand Down
6 changes: 4 additions & 2 deletions uno-core/src/main/java/cc/allio/uno/core/type/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,17 @@ public static boolean isArray(Class<?> clazz) {
return clazz.isArray();
}


/**
* 判断给定的class是否是Bean
*
* @param clazz class对象实例
* @return true false
*/
public static boolean isBean(Class<?> clazz) {
return !SIMPLE_TYPES.contains(clazz) && !isList(clazz) && !isMap(clazz);
return !SIMPLE_TYPES.contains(clazz)
&& !isList(clazz)
&& !isMap(clazz)
&& !clazz.isEnum();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static String parse(String template, String k1, Object v1, String k2, Object v2)
* @see #parseTemplate(String, String, Object, String, Object, String, Object)
*/
static String parse(String template, String k1, Object v1, String k2, Object v2, String k3, Object v3) {
return defaultTemplate().parseTemplate(template, k1, v2, k2, v2, k3, v3);
return defaultTemplate().parseTemplate(template, k1, v1, k2, v2, k3, v3);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions uno-core/src/test/java/cc/allio/uno/core/type/TypeValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import cc.allio.uno.core.BaseTestCase;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -59,6 +61,14 @@ void testCompositeType() {
assertEquals("n", children.getName());
}

@Test
void testEnumType() {
Object success = new TypeValue(Status.class, "SUCCESS").tryConvert();

assertNotNull(success);
assertEquals(Status.SUCCESS, success);
}

public static class TypeSet {
ENUM[] enums;
List<ENUM> enumsList;
Expand All @@ -82,4 +92,14 @@ public static class Children {
public static class Parent {
private Children children;
}


@Getter
@AllArgsConstructor
public enum Status {
SUCCESS("SUCCESS"),
FAILED("FAILED");

private final String value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.lang.annotation.*;
import java.util.Objects;
import java.util.function.Supplier;

/**
* DSL操作
Expand All @@ -26,10 +27,27 @@ public interface Operator<T extends Operator<T>> {
* 解析DSL
*
* @param dsl dsl
* @return SQLOperator
* @return self
*/
T parse(String dsl);

/**
* @see #customize(String)
*/
default T customize(Supplier<String> dslSupplier) {
return customize(dslSupplier.get());
}

/**
* support custom dsl string, assure cover all condition
*
* @param dsl the dsl syntax
* @return self
*/
default T customize(String dsl) {
return parse(dsl);
}

/**
* reset operator
*/
Expand All @@ -51,7 +69,7 @@ public interface Operator<T extends Operator<T>> {
* 如果给定原始为null,则返回{@link ValueWrapper#EMPTY_VALUE}
*
* @param originalValue originalValue
* @return
* @return origin value or {@link ValueWrapper#EMPTY_VALUE}
*/
default Object getValueIfNull(Object originalValue) {
return Objects.requireNonNullElse(originalValue, ValueWrapper.EMPTY_VALUE);
Expand Down Expand Up @@ -81,13 +99,14 @@ default <O extends Operator<?>> O castReality(Class<O> realityType) {
* @param operatorType operatorType
* @return {@link Operator} or parent type
*/
static Class<? extends Operator<?>> getHireachialType(Class<? extends Operator<?>> operatorType) {
if (!operatorType.isInterface()) {
Class<?>[] interfaces = operatorType.getInterfaces();
for (Class<?> hireachial : interfaces) {
if (Operator.class.isAssignableFrom(hireachial)) {
return (Class<? extends Operator<?>>) hireachial;
}
static Class<? extends Operator<?>> getHierarchicalType(Class<? extends Operator<?>> operatorType) {
if (operatorType.isInterface()) {
return operatorType;
}
Class<?>[] interfaces = operatorType.getInterfaces();
for (Class<?> hierarchy : interfaces) {
if (Operator.class.isAssignableFrom(hierarchy)) {
return (Class<? extends Operator<?>>) hierarchy;
}
}
return operatorType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public OperatorTraitGroup() {
*/
public OperatorTrait find(Class<? extends Operator<?>> operatorClazz) {
// find operator class parent
Class<? extends Operator<?>> parent = Operator.getHireachialType(operatorClazz);
Class<? extends Operator<?>> parent = Operator.getHierarchicalType(operatorClazz);
return this.traits.stream()
.filter(trait -> trait.getParent().equals(parent))
.findFirst()
Expand Down Expand Up @@ -209,7 +209,7 @@ static class OperatorTrait {

public OperatorTrait(Class<? extends Operator<?>> clazz) {
this.clazz = clazz;
this.parent = Operator.getHireachialType(clazz);
this.parent = Operator.getHierarchicalType(clazz);
if (this.parent == null) {
throw new DSLException(String.format("operator impl %s not implement any Operator", clazz.getName()));
}
Expand Down
Loading

0 comments on commit a115227

Please sign in to comment.