From 4a52c6366f3be09e9201598554e93ddb29d97908 Mon Sep 17 00:00:00 2001 From: xYLiuuuuu Date: Fri, 13 Dec 2024 00:56:47 +0800 Subject: [PATCH] adjust code styles --- .../ngbatis/annotations/base/GraphId.java | 1 - .../contrib/ngbatis/base/GraphBaseExt.java | 266 +++++++++--------- .../binding/DefaultResultResolver.java | 83 +++--- .../contrib/ngbatis/enums/Direction.java | 22 +- .../nebula/contrib/ngbatis/enums/IdType.java | 4 +- .../contrib/ngbatis/proxy/NebulaDaoBasic.java | 25 +- 6 files changed, 199 insertions(+), 202 deletions(-) diff --git a/src/main/java/org/nebula/contrib/ngbatis/annotations/base/GraphId.java b/src/main/java/org/nebula/contrib/ngbatis/annotations/base/GraphId.java index 8da572d..c5ae598 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/annotations/base/GraphId.java +++ b/src/main/java/org/nebula/contrib/ngbatis/annotations/base/GraphId.java @@ -4,7 +4,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; - import org.nebula.contrib.ngbatis.enums.IdType; /** diff --git a/src/main/java/org/nebula/contrib/ngbatis/base/GraphBaseExt.java b/src/main/java/org/nebula/contrib/ngbatis/base/GraphBaseExt.java index 5a4eb67..778ab9e 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/base/GraphBaseExt.java +++ b/src/main/java/org/nebula/contrib/ngbatis/base/GraphBaseExt.java @@ -1,16 +1,16 @@ package org.nebula.contrib.ngbatis.base; +import static org.nebula.contrib.ngbatis.utils.ReflectUtil.getNameByColumn; +import static org.nebula.contrib.ngbatis.utils.ReflectUtil.getValue; + import com.vesoft.nebula.client.graph.data.ResultSet; import com.vesoft.nebula.client.graph.exception.BindSpaceFailedException; import com.vesoft.nebula.client.graph.exception.IOErrorException; import com.vesoft.nebula.client.graph.net.Session; - import java.lang.reflect.Field; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; - import org.nebula.contrib.ngbatis.ArgsResolver; import org.nebula.contrib.ngbatis.Env; import org.nebula.contrib.ngbatis.ResultResolver; @@ -18,16 +18,10 @@ import org.nebula.contrib.ngbatis.annotations.base.EdgeType; import org.nebula.contrib.ngbatis.annotations.base.GraphId; import org.nebula.contrib.ngbatis.annotations.base.Tag; -import org.nebula.contrib.ngbatis.proxy.MapperProxy; import org.nebula.contrib.ngbatis.session.LocalSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.apache.commons.lang3.ObjectUtils.isEmpty; -import static org.nebula.contrib.ngbatis.proxy.MapperProxy.executeWithParameter; -import static org.nebula.contrib.ngbatis.utils.ReflectUtil.getNameByColumn; -import static org.nebula.contrib.ngbatis.utils.ReflectUtil.getValue; - /** * 提供给实体调用的拓展方法。 * @author xYLiuuuuuu @@ -38,40 +32,39 @@ public class GraphBaseExt { public static Env ENV; private static Logger log = LoggerFactory.getLogger(GraphBaseExt.class); - public static ResultSet executeGql(String textTpl,Map m1,Map m2) { - Session session = null; - LocalSession localSession = null; - ResultSet result = null; - //从env中获取本地会话调度器 - SessionDispatcher dispatcher = ENV.getDispatcher(); - localSession = dispatcher.poll(); - //从env中获取space - String currentSpace = ENV.getSpace(); - - ArgsResolver argsResolver = ENV.getArgsResolver(); - - String gql = ENV.getTextResolver().resolve(textTpl, m1); - - Map parasForDb = argsResolver.resolve(m2); - - String[] qlAndSpace = null; - try { - //确保当前图空间正确 - qlAndSpace = qlWithSpace(localSession, gql, currentSpace); - gql = qlAndSpace[1]; - session = localSession.getSession(); - result = session.executeWithParameter(gql,parasForDb); - } - catch (Exception e) { - throw new RuntimeException(e); - } - finally { - log.debug("\n\t- nGql:{}" - + "\n\t- params: {}" - + "\n\t- result:{}", - gql, m2, result); - } - return result; + public static ResultSet executeGql(String textTpl, + Map m1, Map m2) { + Session session = null; + LocalSession localSession = null; + ResultSet result = null; + //从env中获取本地会话调度器 + SessionDispatcher dispatcher = ENV.getDispatcher(); + localSession = dispatcher.poll(); + //从env中获取space + String currentSpace = ENV.getSpace(); + + ArgsResolver argsResolver = ENV.getArgsResolver(); + + String gql = ENV.getTextResolver().resolve(textTpl, m1); + + Map parasForDb = argsResolver.resolve(m2); + + String[] qlAndSpace = null; + try { + //确保当前图空间正确 + qlAndSpace = qlWithSpace(localSession, gql, currentSpace); + gql = qlAndSpace[1]; + session = localSession.getSession(); + result = session.executeWithParameter(gql, parasForDb); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + log.debug("\n\t- nGql:{}" + + "\n\t- params: {}" + + "\n\t- result:{}", + gql, m2, result); + } + return result; } /** @@ -82,29 +75,29 @@ public static ResultSet executeGql(String textTpl,Map m1,Map String getV2Tag(T v2) { - Class v2Class = v2.getClass(); - if (v2Class.isAnnotationPresent(Tag.class)) { - Tag tagAnnotation = v2Class.getAnnotation(Tag.class); - return tagAnnotation.name(); - } - else { - throw new RuntimeException("Entity " + v2Class.getName() + " does not have @Tag annotation."); - } + Class v2Class = v2.getClass(); + if (v2Class.isAnnotationPresent(Tag.class)) { + Tag tagAnnotation = v2Class.getAnnotation(Tag.class); + return tagAnnotation.name(); + } else { + throw new RuntimeException("Entity " + v2Class.getName() + " does not have @Tag annotation."); + } } /** @@ -148,29 +139,28 @@ public static String getV2Tag(T v2) { * @return 点的属性map */ public static Map getV2Property(T v2) { - Class v2Class = v2.getClass(); - Map result = new HashMap(); - Field[] fields = v2Class.getDeclaredFields(); - for (Field field : fields) { - Object fieldValue = getValue(v2,field); - if (fieldValue == null) { - continue; - } - // 如果是基本类型且是初始值,跳过 - if (field.getType().isPrimitive()) { - if (isPrimitiveDefaultValue(field.getType(), fieldValue)) { - continue; // 跳过初始值 - } - } - // 处理带有 @GraphId 注解的字段 - if (field.isAnnotationPresent(GraphId.class)) { - result.put("id", fieldValue); - } - else { - result.put(getNameByColumn(field), fieldValue); - } - } - return result; + Class v2Class = v2.getClass(); + Map result = new HashMap(); + Field[] fields = v2Class.getDeclaredFields(); + for (Field field : fields) { + Object fieldValue = getValue(v2, field); + if (fieldValue == null) { + continue; + } + // 如果是基本类型且是初始值,跳过 + if (field.getType().isPrimitive()) { + if (isPrimitiveDefaultValue(field.getType(), fieldValue)) { + continue; // 跳过初始值 + } + } + // 处理带有 @GraphId 注解的字段 + if (field.isAnnotationPresent(GraphId.class)) { + result.put("id", fieldValue); + } else { + result.put(getNameByColumn(field), fieldValue); + } + } + return result; } /** @@ -180,28 +170,28 @@ public static Map getV2Property(T v2 * @return 判断结果 */ public static boolean isPrimitiveDefaultValue(Class type, Object value) { - if (type == int.class && (int) value == 0) { - return true; - } - if (type == long.class && (long) value == 0L) { - return true; - } - if (type == float.class && (float) value == 0.0f) { - return true; - } - if (type == double.class && (double) value == 0.0d) { - return true; - } - if (type == byte.class && (byte) value == 0) { - return true; - } - if (type == short.class && (short) value == 0) { - return true; - } - if (type == char.class && (char) value == '\u0000') { // 默认字符是空字符 - return true; - } - return false; + if (type == int.class && (int) value == 0) { + return true; + } + if (type == long.class && (long) value == 0L) { + return true; + } + if (type == float.class && (float) value == 0.0f) { + return true; + } + if (type == double.class && (double) value == 0.0d) { + return true; + } + if (type == byte.class && (byte) value == 0) { + return true; + } + if (type == short.class && (short) value == 0) { + return true; + } + if (type == char.class && (char) value == '\u0000') { // 默认字符是空字符 + return true; + } + return false; } /** @@ -211,17 +201,17 @@ public static boolean isPrimitiveDefaultValue(Class type, Object value) { * @return edge子句 */ public static String getEdgeTypes(String sep, Class... edgeClass) { - if (edgeClass == null) { - return ""; - } - StringBuilder edgeTypeBuilder = new StringBuilder(); - for (Class edgeType : edgeClass) { - String type = GraphBaseExt.getEdgeType(edgeType); - edgeTypeBuilder.append(type).append(sep); - } - if (edgeTypeBuilder.length() > 0) { - edgeTypeBuilder.setLength(edgeTypeBuilder.length() - 1); - } - return edgeTypeBuilder.toString(); + if (edgeClass == null) { + return ""; + } + StringBuilder edgeTypeBuilder = new StringBuilder(); + for (Class edgeType : edgeClass) { + String type = GraphBaseExt.getEdgeType(edgeType); + edgeTypeBuilder.append(type).append(sep); + } + if (edgeTypeBuilder.length() > 0) { + edgeTypeBuilder.setLength(edgeTypeBuilder.length() - 1); + } + return edgeTypeBuilder.toString(); } } diff --git a/src/main/java/org/nebula/contrib/ngbatis/binding/DefaultResultResolver.java b/src/main/java/org/nebula/contrib/ngbatis/binding/DefaultResultResolver.java index 8272be0..c614b2f 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/binding/DefaultResultResolver.java +++ b/src/main/java/org/nebula/contrib/ngbatis/binding/DefaultResultResolver.java @@ -11,7 +11,6 @@ import org.nebula.contrib.ngbatis.utils.ReflectUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.stereotype.Component; /** @@ -27,57 +26,57 @@ public class DefaultResultResolver implements ResultResolver { @Override public Object resolve(MethodModel methodModel, ResultSet result) { - Class returnType = methodModel.getReturnType(); - Class resultType = methodModel.getResultType(); + Class returnType = methodModel.getReturnType(); + Class resultType = methodModel.getResultType(); - if (resultType == null) { - resultType = returnType; - } + if (resultType == null) { + resultType = returnType; + } - if (returnType == void.class) { - return null; - } + if (returnType == void.class) { + return null; + } - // 核心方法。获取真正执行结果集类型转换的结果处理执行者 - ResultHandler handler = ResultHandler.getHandler( - ReflectUtil.sealingBasicType(returnType), - ReflectUtil.sealingBasicType(resultType) - ); - log.debug("针对java返回类型 {} 与 xml 返回类型 {} 的结果集处理器为:{}", - returnType.getName(), - resultType.getName(), - handler - ); + // 核心方法。获取真正执行结果集类型转换的结果处理执行者 + ResultHandler handler = ResultHandler.getHandler( + ReflectUtil.sealingBasicType(returnType), + ReflectUtil.sealingBasicType(resultType) + ); + log.debug("针对java返回类型 {} 与 xml 返回类型 {} 的结果集处理器为:{}", + returnType.getName(), + resultType.getName(), + handler + ); - if (handler == null) { - return result; - } - return handler.handle(returnType, result, resultType); + if (handler == null) { + return result; + } + return handler.handle(returnType, result, resultType); } @Override public Object resolve(ResultSet result, Class returnType, Class resultType) { - if (resultType == null) { - resultType = returnType; - } + if (resultType == null) { + resultType = returnType; + } - if (returnType == void.class) { - return null; - } + if (returnType == void.class) { + return null; + } - // 核心方法。获取真正执行结果集类型转换的结果处理执行者 - ResultHandler handler = ResultHandler.getHandler( - ReflectUtil.sealingBasicType(returnType), - ReflectUtil.sealingBasicType(resultType) - ); - log.debug("返回类型 {},结果集处理器为:{}", - returnType.getName(), - handler - ); + // 核心方法。获取真正执行结果集类型转换的结果处理执行者 + ResultHandler handler = ResultHandler.getHandler( + ReflectUtil.sealingBasicType(returnType), + ReflectUtil.sealingBasicType(resultType) + ); + log.debug("返回类型 {},结果集处理器为:{}", + returnType.getName(), + handler + ); - if (handler == null) { - return result; - } - return handler.handle(returnType, result, resultType); + if (handler == null) { + return result; + } + return handler.handle(returnType, result, resultType); } } diff --git a/src/main/java/org/nebula/contrib/ngbatis/enums/Direction.java b/src/main/java/org/nebula/contrib/ngbatis/enums/Direction.java index 2f5add9..d57f92d 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/enums/Direction.java +++ b/src/main/java/org/nebula/contrib/ngbatis/enums/Direction.java @@ -7,14 +7,16 @@ */ public enum Direction { - NULL(""), //默认是出边 - BIDIRECT("BIDIRECT"), //无向边 - REVERSELY("REVERSELY"); //入边 - private final String symbol; - Direction(String symbol) { - this.symbol = symbol; - } - public String getSymbol() { - return symbol; - } + NULL(""), //默认是出边 + BIDIRECT("BIDIRECT"), //无向边 + REVERSELY("REVERSELY"); //入边 + private final String symbol; + + Direction(String symbol) { + this.symbol = symbol; + } + + public String getSymbol() { + return symbol; + } } diff --git a/src/main/java/org/nebula/contrib/ngbatis/enums/IdType.java b/src/main/java/org/nebula/contrib/ngbatis/enums/IdType.java index 9dd73e0..cff27f5 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/enums/IdType.java +++ b/src/main/java/org/nebula/contrib/ngbatis/enums/IdType.java @@ -6,6 +6,6 @@ * @since 2024/9/8 10:15 */ public enum IdType { - STRING, - INT64; + STRING, + INT64; } diff --git a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java index 42474b1..13dbda0 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java +++ b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java @@ -4,7 +4,21 @@ package org.nebula.contrib.ngbatis.proxy; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.edgeName; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.entityType; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.getClassModel; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.getCqlTpl; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.getMethodModel; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.pkType; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.proxy; +import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.vertexName; + import com.vesoft.nebula.client.graph.data.ResultSet; +import java.io.Serializable; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.nebula.contrib.ngbatis.exception.QueryException; import org.nebula.contrib.ngbatis.handler.CollectionStringResultHandler; import org.nebula.contrib.ngbatis.models.ClassModel; @@ -13,13 +27,6 @@ import org.nebula.contrib.ngbatis.utils.Page; import org.springframework.data.repository.query.Param; -import java.io.Serializable; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import static org.nebula.contrib.ngbatis.proxy.NebulaDaoBasicExt.*; /** * 数据访问的基类,用于提供单表 CRUD 与基本的节点关系操作
@@ -524,8 +531,8 @@ default E startNode(Class startType, Class edgeType, I endId) { * @param srcId the start node's id * @param dstId the end node's id * @return Shortest path list. entities containing vertext in path. - * If you want to obtain attributes within an entity, - * you need to use “with prop” in the nGQL. + * If you want to obtain attributes within an entity, + * you need to use “with prop” in the nGQL. */ default List> shortestPath(@Param("srcId") I srcId, @Param("dstId") I dstId) { MethodModel methodModel = getMethodModel();