Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Dec 21, 2023
1 parent ae4ba41 commit 67535f4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
4 changes: 2 additions & 2 deletions rxlib/src/main/java/org/rx/core/StringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void preAppend(Object obj) {
}

@Override
public StringBuilder append(CharSequence csq) throws IOException {
public StringBuilder append(CharSequence csq) {
preAppend(csq);
buffer.append(csq);
return this;
Expand All @@ -177,7 +177,7 @@ public StringBuilder append(String format, Object... args) {
}

@Override
public StringBuilder append(CharSequence csq, int start, int end) throws IOException {
public StringBuilder append(CharSequence csq, int start, int end) {
buffer.append(csq, start, end);
return this;
}
Expand Down
69 changes: 59 additions & 10 deletions rxlib/src/main/java/org/rx/core/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.rx.exception.InvalidException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -49,23 +50,71 @@ public static List<String> getVarExpressionNames(@NonNull String expr, boolean o
return list;
}

public static String resolveVarExpression(String expr, Map<String, Object> vars) {
return resolveVarExpression(new StringBuilder(expr), vars);
}

/**
* 替换变量
* 变量格式: %{变量名}
*
* @param expr This is ${name}
* @param vars {\"${name}\": \"xf\"}
* @param vars {\"name\": \"xf\"}
* @return This is xf
*/
public static String resolveVarExpression(@NonNull StringBuilder expr, @NonNull Map<String, Object> vars) {
for (Map.Entry<String, Object> var : vars.entrySet()) {
expr.replace(var.getKey(), ifNull(var.getValue(), Strings.EMPTY).toString());
public static String resolveVarExpression(@NonNull CharSequence expr, Map<String, Object> vars) {
if (vars == null) {
vars = Collections.emptyMap();
}
return expr.toString();
final char begin0 = '$', begin1 = '{', end = '}';
final byte inExpr = 2;
int len = expr.length(), mark = -1;
StringBuilder buf = new StringBuilder(len), pBuf = new StringBuilder();
byte stat = 0;
for (int i = 0; i < len; i++) {
char c = expr.charAt(i);
if (c == begin0) {
if (stat == 0) {
mark = buf.length();
stat = 1;
// continue;
} else {
if (stat == inExpr) {
throw new InvalidException("表达式在索引{}开始,索引{}格式错误", mark, i);
}
mark = -1;
stat = 0;
}
} else if (c == begin1) {
if (stat == 1) {
buf.append(c);
stat = inExpr;
continue;
} else {
if (stat == inExpr) {
throw new InvalidException("表达式{}在索引{}开始,索引{}格式错误", pBuf, mark, i);
}
mark = -1;
stat = 0;
}
} else if (c == end) {
if (stat == inExpr) {
buf.setLength(mark);
Object var = Sys.readJsonValue(vars, pBuf.toString(), null, false);
if (var != null) {
buf.append(var);
}
pBuf.setLength(0);
mark = -1;
stat = 0;
continue;
} else {
mark = -1;
stat = 0;
}
}
if (stat == inExpr) {
pBuf.append(c);
continue;
}
buf.append(c);
}
return buf.toString();
}
//endregion

Expand Down
10 changes: 8 additions & 2 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,19 @@ static Object visitJson(Object cur, String path, AtomicInteger i, char c, String
try {
cur = IterableUtils.get((Iterable<?>) cur, idx);
} catch (IndexOutOfBoundsException e) {
throw new InvalidException("Array \"{}\" is index out of bounds with path {}", cur, visitor, e);
if (throwOnEmptyChild) {
throw new InvalidException("Array \"{}\" is index out of bounds with path {}", cur, visitor, e);
}
cur = null;
}
} else if (cur.getClass().isArray()) {
try {
cur = Array.get(cur, idx);
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidException("Array \"{}\" is index out of bounds with path {}", cur, visitor, e);
if (throwOnEmptyChild) {
throw new InvalidException("Array \"{}\" is index out of bounds with path {}", cur, visitor, e);
}
cur = null;
}
} else {
throw new InvalidException("Object \"{}\" is not a array with path {}", cur, visitor);
Expand Down
8 changes: 4 additions & 4 deletions rxlib/src/main/java/org/rx/core/ThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@ public <T> Future<T> run(Func<T> task, Object taskId, FlagsEnum<RunFlag> flags)
}

@SneakyThrows
public <T> T runAny(Collection<Func<T>> tasks, long timeoutMillis) {
public <T> T runAny(Iterable<Func<T>> tasks, long timeoutMillis) {
List<Callable<T>> callables = Linq.from(tasks).select(p -> (Callable<T>) new Task<>(p, null, null)).toList();
return timeoutMillis > 0 ? super.invokeAny(callables, timeoutMillis, TimeUnit.MILLISECONDS) : super.invokeAny(callables);
}

@SneakyThrows
public <T> List<Future<T>> runAll(Collection<Func<T>> tasks, long timeoutMillis) {
public <T> List<Future<T>> runAll(Iterable<Func<T>> tasks, long timeoutMillis) {
List<Callable<T>> callables = Linq.from(tasks).select(p -> (Callable<T>) new Task<>(p, null, null)).toList();
return timeoutMillis > 0 ? super.invokeAll(callables, timeoutMillis, TimeUnit.MILLISECONDS) : super.invokeAll(callables);
}
Expand Down Expand Up @@ -530,15 +530,15 @@ <T> CompletableFuture<T> runSerialAsync(@NonNull Func<T> task, @NonNull Object t
return f;
}

public <T> MultiTaskFuture<T, T> runAnyAsync(Collection<Func<T>> tasks) {
public <T> MultiTaskFuture<T, T> runAnyAsync(Iterable<Func<T>> tasks) {
CompletableFuture<T>[] futures = Linq.from(tasks).select(task -> {
Task<T> t = new Task<>(task, null, null);
return CompletableFuture.supplyAsync(t, asyncExecutor);
}).toArray();
return new MultiTaskFuture<>((CompletableFuture<T>) CompletableFuture.anyOf(futures), futures);
}

public <T> MultiTaskFuture<Void, T> runAllAsync(Collection<Func<T>> tasks) {
public <T> MultiTaskFuture<Void, T> runAllAsync(Iterable<Func<T>> tasks) {
CompletableFuture<T>[] futures = Linq.from(tasks).select(task -> {
Task<T> t = new Task<>(task, null, null);
//allOf().join() will hang
Expand Down
4 changes: 4 additions & 0 deletions rxlib/src/test/java/org/rx/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ public void other() {

assert eq(Sys.readJsonValue(demo, "user.name"), "张三");
assert eq(Sys.readJsonValue(demo, "group[3]"), 4);

System.out.println("的用法用量和注意事项,System回$}答{}要$求}简{洁},36\n" +
Strings.resolveVarExpression("${recordDrugName}的用法用量和注意事项,${x}回$}答{}要$求}简{洁},${12}3${45}6",
Collections.singletonMap("x", "System")));
}

@Data
Expand Down

0 comments on commit 67535f4

Please sign in to comment.