Skip to content

Commit

Permalink
Merge branch 'master' into rxsocks
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Nov 20, 2023
2 parents ab36fe3 + 60d9d63 commit 7ce3ca5
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 30 deletions.
1 change: 1 addition & 0 deletions rxlib/src/main/java/org/rx/core/Disposable.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public synchronized void close() {
if (closed) {
return;
}
//todo fields may be null
freeObjects();
closed = true;
}
Expand Down
124 changes: 100 additions & 24 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.SystemUtils;
import org.rx.annotation.Subscribe;
import org.rx.bean.DynamicProxyBean;
import org.rx.bean.LogStrategy;
import org.rx.bean.ProceedEventArgs;
import org.rx.bean.Tuple;
import org.rx.codec.CodecUtil;
import org.rx.exception.InvalidException;
import org.rx.exception.TraceHandler;
Expand All @@ -32,13 +35,15 @@
import java.io.File;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.URI;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -500,42 +505,113 @@ public static String cacheKey(String region, Object... args) {
}

//region json
public static <T> T readJsonValue(@NonNull Map<String, ?> json, String path,

/**
* @param json
* @param path
* @param childSelect !Reflects.isBasicType(cur.getClass())
* @param throwOnEmptyChild
* @param <T>
* @return
*/
public static <T> T readJsonValue(@NonNull Object json, String path,
BiFunc<Object, ?> childSelect,
boolean throwOnEmptyChild) {
Object child = json.get(path);
if (child != null) {
if (childSelect != null) {
child = childSelect.apply(child);
if (json instanceof Map) {
Map<String, Object> jObj = (Map<String, Object>) json;
Object cur = jObj.get(path);
if (cur != null) {
if (childSelect != null) {
cur = childSelect.apply(cur);
}
return (T) cur;
}
return (T) child;
}

String[] paths = Strings.split(path, ".");
if (paths.length == 0) {
return null;
Object cur = json;
int max = path.length() - 1;

AtomicInteger i = new AtomicInteger();
StringBuilder buf = new StringBuilder();
for (; i.get() <= max; i.incrementAndGet()) {
char c = path.charAt(i.get());
if (c != objKey && c != arrBeginKey) {
buf.append(c);
continue;
}

cur = visitJson(cur, path, i, c, buf.isEmpty() ? null : buf.toString(), max, childSelect, throwOnEmptyChild);
buf.setLength(0);
}
if (!buf.isEmpty()) {
cur = visitJson(cur, path, i, objKey, buf.toString(), max, childSelect, throwOnEmptyChild);
}
return (T) cur;
}

static final char objKey = '.', arrBeginKey = '[', arrEndKey = ']';

static Object visitJson(Object cur, String path, AtomicInteger i, char c, String visitor,
int max, BiFunc<Object, ?> childSelect, boolean throwOnEmptyChild) {
if (visitor != null) {
if (cur instanceof Map) {
Map<String, ?> obj = (Map<String, ?>) cur;
cur = obj.get(visitor);
} else if (cur instanceof Iterable) {
System.out.println(cur);
} else {
try {
cur = Reflects.readField(cur, visitor);
} catch (Throwable e) {
throw new InvalidException("Object {} is not a map type or not found field with path {}", cur, visitor, e);
}
}
}

int last = paths.length - 1;
Map<String, ?> tmp = json;
for (int i = 0; i < last; i++) {
child = tmp.get(paths[i]);
if (childSelect != null) {
child = childSelect.apply(child);
if (c == arrBeginKey) {
StringBuilder idxBuf = new StringBuilder();
for (i.incrementAndGet(); i.get() < path.length(); i.incrementAndGet()) {
char ic = path.charAt(i.get());
if (ic != arrEndKey) {
idxBuf.append(ic);
continue;
}
break;
}
int idx;
try {
idx = Integer.parseInt(idxBuf.toString());
visitor = String.format("%s[%s]", visitor, idxBuf);
} catch (Throwable e) {
throw new InvalidException("Index {} is not a int type", idxBuf, e);
}
if ((tmp = as(child, Map.class)) == null) {
if (throwOnEmptyChild) {
throw new InvalidException("Get empty sub object by path {}", paths[i]);

if (cur instanceof Iterable) {
try {
cur = IterableUtils.get((Iterable<?>) cur, idx);
} catch (IndexOutOfBoundsException e) {
throw new InvalidException("Array {} is index out of bounds with path {}", cur, visitor, e);
}
} else if (cur != null && 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);
}
return null;
} else {
throw new InvalidException("Object {} is not a array type with path {}", cur, visitor);
}
}
child = tmp.get(paths[last]);
if (child != null
&& childSelect != null && !Reflects.isBasicType(child.getClass())) {
child = childSelect.apply(child);
if (cur != null && childSelect != null) {
cur = childSelect.apply(cur);
}
if (i.get() < max && cur == null) {
if (throwOnEmptyChild) {
throw new InvalidException("Get empty sub object by path {}", visitor);
}
return null;
}
return (T) child;
return cur;
}

//TypeReference
Expand Down
5 changes: 4 additions & 1 deletion rxlib/src/main/java/org/rx/io/FileStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.extern.slf4j.Slf4j;
import org.rx.bean.FlagsEnum;
import org.rx.core.Constants;
import org.rx.core.Extends;
import org.rx.util.Lazy;
import org.rx.util.Snowflake;
import org.rx.util.function.TripleFunc;
Expand All @@ -17,6 +18,8 @@
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;

import static org.rx.core.Extends.tryClose;

@Slf4j
public class FileStream extends IOStream implements Serializable {
private static final long serialVersionUID = 8857792573177348449L;
Expand Down Expand Up @@ -211,7 +214,7 @@ public FileStream(@NonNull File file, FileMode mode, int bufSize, @NonNull Flags

@Override
protected void freeObjects() throws Throwable {
randomAccessFile.close();
tryClose(randomAccessFile);
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion rxlib/src/main/java/org/rx/io/HybridStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.rx.core.Constants;
import org.rx.core.Extends;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import static org.rx.core.Extends.tryClose;

@Slf4j
public final class HybridStream extends IOStream implements Serializable {
private static final long serialVersionUID = 2137331266386948293L;
Expand Down Expand Up @@ -76,7 +79,7 @@ public HybridStream(int maxMemorySize, boolean directMemory, String tempFilePath

@Override
protected void freeObjects() {
stream.close();
tryClose(stream);
}

FileStream newFileStream() {
Expand Down
99 changes: 95 additions & 4 deletions rxlib/src/test/java/org/rx/util/TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.rx.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.Data;
import lombok.SneakyThrows;
Expand All @@ -20,8 +22,8 @@
import java.util.Arrays;
import java.util.*;

import static org.rx.core.Sys.toJsonObject;
import static org.rx.core.Sys.toJsonString;
import static org.rx.core.Extends.eq;
import static org.rx.core.Sys.*;

@Slf4j
public class TestUtil extends AbstractTester {
Expand Down Expand Up @@ -270,9 +272,98 @@ public void other() {
String ce = "适用苹果ipad 10.2平板钢化膜9.7寸/air5全屏防爆膜2020/2021版高清贴膜10.9/11寸12.9寸护眼抗蓝紫光钢化膜";
System.out.println(Strings.subStringByByteLen(ce, 78));

String json = "{\"ctp\":\"pages/gold/item/pages/detail/index\",\"par\":\"\",\"ref\":\"pages/gold/item/pages/detail/index\",\"rpr\":\"\",\"seq\":2,\"vts\":110,\"pin\":\"jd_759f867040f60\",\"fst\":\"1577630901829\",\"pst\":\"1677280413\",\"vct\":\"1677479554\",\"jsver\":\"TR1.0.0\",\"net\":\"wifi\",\"lat\":\"\",\"lon\":\"\",\"speed\":\"\",\"accuracy\":\"\",\"pixelRatio\":\"1\",\"jdv\":\"1|weixin|t_335139774_xcx_1036_appfxxx|xcx|-|1677479554238\",\"customerInfo\":\"\",\"unpl\":\"\",\"scene\":1036,\"sdkver\":\"2.19.2\",\"ext\":{},\"eid\":\"W_jdgwxcx_productdetail_consultationiconexpo\",\"eparam\":\"{\\\"mainskuid\\\":100006612085}\",\"elevel\":\"\",\"page_id\":\"W_jdgwxcx_productdetail\",\"pname\":\"pages/gold/item/pages/detail/index\",\"pparam\":\"\",\"tar\":\"\",\"x\":0,\"y\":0,\"typ\":\"ep\"}";
Object v = Sys.readJsonValue(toJsonObject(json), "eparam.mainskuid", Sys::toJsonObject, true);
String json = "{\n" +
" \"data\": {\n" +
" \"name\": \"张三\",\n" +
" \"age\": 10,\n" +
" \"mark\": [\n" +
" 0,1,2,3,4,5,6,7,8,9,10,11,\n" +
" [\n" +
" 2,\n" +
" 3,\n" +
" {\n" +
" \"name\": \"李四\",\n" +
" \"mark\": [\n" +
" 0,\n" +
" [\n" +
" 1\n" +
" ]\n" +
" ]\n" +
" }\n" +
" ]\n" +
" ]\n" +
" },\n" +
" \"status\": 200\n" +
"}";
JSONObject jObj = toJsonObject(json);
Object v;
// v = Sys.readJsonValue(jObj, "data", null, true);
// System.out.println(v);
// assert v instanceof JSONObject;
//
// v = Sys.readJsonValue(jObj, "data.mark", null, true);
// System.out.println(v);
// assert v instanceof JSONArray;
//
// v = Sys.readJsonValue(jObj, "status", null, true);
// System.out.println(v);
// assert eq(v, 200);
//
// v = Sys.readJsonValue(jObj, "data.name", null, true);
// System.out.println(v);
// assert eq(v, "张三");
//
// v = Sys.readJsonValue(jObj, "data.mark[1]", null, true);
// System.out.println(v);
// assert eq(v, 1);
//
// v = Sys.readJsonValue(jObj, "data.mark[11]", null, true);
// System.out.println(v);
// assert eq(v, 11);
//
// v = Sys.readJsonValue(jObj, "data.mark[12][1]", null, true);
// System.out.println(v);
// assert eq(v, 3);

v = Sys.readJsonValue(jObj, "data.mark[12][2].name", null, true);
System.out.println(v);
assert eq(v, "李四");

v = Sys.readJsonValue(jObj, "data.mark[12][2].mark[1][0]", null, true);
System.out.println(v);
assert eq(v, 1);

JSONArray jArr = toJsonArray("[\n" +
" 0,1,2,3,4,5,6,7,8,9,10,11,\n" +
" [\n" +
" 2,\n" +
" 3,\n" +
" {\n" +
" \"name\": \"李四\",\n" +
" \"mark\": [\n" +
" 0,\n" +
" [\n" +
" 1\n" +
" ]\n" +
" ]\n" +
" }\n" +
" ]\n" +
"]");
v = Sys.readJsonValue(jArr, "[1]", null, true);
System.out.println(v);
assert eq(v, 1);

v = Sys.readJsonValue(jArr, "[12][0]", null, true);
System.out.println(v);
assert eq(v, 2);

v = Sys.readJsonValue(jArr, "[12][2].name", null, true);
System.out.println(v);
assert eq(v, "李四");

v = Sys.readJsonValue(jArr, "[12][2].mark[1][0]", null, true);
System.out.println(v);
assert eq(v, 1);
}

@Test
Expand Down

0 comments on commit 7ce3ca5

Please sign in to comment.