Skip to content

Commit

Permalink
style: 优化 == 及 != 表达式格式
Browse files Browse the repository at this point in the history
1.将 null 或常量值调整到符号左侧
2.将无特殊意义的方法判空写法改为表达式判断写法
  • Loading branch information
Charles7c committed Aug 15, 2023
1 parent 94f88ba commit 487fa82
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected <E> List<E> list(Q query, SortQuery sortQuery, Class<E> targetClass) {
// 设置排序
Sort sort = Opt.ofNullable(sortQuery).orElseGet(SortQuery::new).getSort();
for (Sort.Order order : sort) {
queryWrapper.orderBy(order != null, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty()));
queryWrapper.orderBy(null != order, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty()));
}
List<T> entityList = baseMapper.selectList(queryWrapper);
return BeanUtil.copyToList(entityList, targetClass);
Expand All @@ -168,7 +168,7 @@ public D get(Long id) {
@Override
@Transactional(rollbackFor = Exception.class)
public Long add(C request) {
if (request == null) {
if (null == request) {
return 0L;
}
T entity = BeanUtil.copyProperties(request, entityClass);
Expand Down Expand Up @@ -220,7 +220,7 @@ protected void fill(Object baseObj) {
if (baseObj instanceof BaseVO) {
BaseVO baseVO = (BaseVO)baseObj;
Long createUser = baseVO.getCreateUser();
if (createUser == null) {
if (null == createUser) {
return;
}
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
Expand All @@ -240,7 +240,7 @@ protected void fillDetail(Object detailObj) {
this.fill(detailVO);

Long updateUser = detailVO.getUpdateUser();
if (updateUser == null) {
if (null == updateUser) {
return;
}
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public OpenAPI openApi() {
@Bean
public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
return openApi -> {
if (openApi.getTags() != null) {
if (null != openApi.getTags()) {
openApi.getTags()
.forEach(tag -> tag.setExtensions(MapUtil.of("x-order", RandomUtil.randomInt(0, 100))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;

import top.charles7c.cnadmin.common.base.BaseEnum;
import top.charles7c.cnadmin.common.constant.StringConsts;
Expand Down Expand Up @@ -63,7 +62,7 @@ public BaseEnum convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty
@Override
public WriteCellData<String> convertToExcelData(BaseEnum<Integer, String> value,
ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (ObjectUtil.isNull(value)) {
if (null == value) {
return new WriteCellData<>(StringConsts.EMPTY);
}
return new WriteCellData<>(value.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;

/**
* Easy Excel 大数值转换器(Excel 中对长度超过 15 位的数值输入是有限制的,从 16 位开始无论录入什么数字均会变为 0,因此输入时只能以文本的形式进行录入)
Expand Down Expand Up @@ -66,7 +65,7 @@ public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty con
@Override
public WriteCellData<Object> convertToExcelData(Long value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ObjectUtil.isNotNull(value)) {
if (null != value) {
String str = Long.toString(value);
if (str.length() > MAX_LENGTH) {
return new WriteCellData<>(str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public class SimpleDeserializersWrapper extends SimpleDeserializers {
public JsonDeserializer<?> findEnumDeserializer(Class<?> type, DeserializationConfig config,
BeanDescription beanDesc) throws JsonMappingException {
JsonDeserializer<?> deser = super.findEnumDeserializer(type, config, beanDesc);
if (deser != null) {
if (null != deser) {
return deser;
}

// 重写增强:开始查找指定枚举类型的接口的反序列化器(例如:GenderEnum 枚举类型,则是找它的接口 BaseEnum 的反序列化器)
for (Class<?> typeInterface : type.getInterfaces()) {
deser = this._classMappings.get(new ClassKey(typeInterface));
if (deser != null) {
if (null != deser) {
return deser;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;

Expand Down Expand Up @@ -80,13 +79,13 @@ public Expression getSqlSegment(Expression where, String mappedStatementId) {
String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(StringConsts.DOT) + 1);
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
DataPermission annotation = method.getAnnotation(DataPermission.class);
if (ObjectUtils.isNotEmpty(annotation)
DataPermission dataPermission = method.getAnnotation(DataPermission.class);
if (null != dataPermission
&& (method.getName().equals(methodName) || (method.getName() + "_COUNT").equals(methodName))) {
// 获取当前登录用户
LoginUser loginUser = LoginHelper.getLoginUser();
if (ObjectUtils.isNotEmpty(loginUser) && !loginUser.isAdmin()) {
return buildDataScopeFilter(loginUser, annotation.value(), where);
if (null != loginUser && !loginUser.isAdmin()) {
return buildDataScopeFilter(loginUser, dataPermission.value(), where);
}
}
}
Expand Down Expand Up @@ -134,20 +133,19 @@ private static Expression buildDataScopeFilter(LoginUser user, String tableAlias
InExpression inExpression = new InExpression();
inExpression.setLeftExpression(buildColumn(tableAlias, DEPT_ID));
inExpression.setRightExpression(subSelect);
expression =
ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, inExpression) : inExpression;
expression = null != expression ? new OrExpression(expression, inExpression) : inExpression;
} else if (DataScopeEnum.DEPT.equals(dataScope)) {
// select t1.* from table as t1 where t1.`dept_id` = xxx;
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(buildColumn(tableAlias, DEPT_ID));
equalsTo.setRightExpression(new LongValue(user.getDeptId()));
expression = ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, equalsTo) : equalsTo;
expression = null != expression ? new OrExpression(expression, equalsTo) : equalsTo;
} else if (DataScopeEnum.SELF.equals(dataScope)) {
// select t1.* from table as t1 where t1.`create_user` = xxx;
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(buildColumn(tableAlias, CREATE_USER));
equalsTo.setRightExpression(new LongValue(user.getId()));
expression = ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, equalsTo) : equalsTo;
expression = null != expression ? new OrExpression(expression, equalsTo) : equalsTo;
} else if (DataScopeEnum.CUSTOM.equals(dataScope)) {
// select t1.* from table as t1 where t1.`dept_id` in (select `dept_id` from `sys_role_dept` where
// `role_id` = xxx);
Expand All @@ -165,11 +163,10 @@ private static Expression buildDataScopeFilter(LoginUser user, String tableAlias
InExpression inExpression = new InExpression();
inExpression.setLeftExpression(buildColumn(tableAlias, DEPT_ID));
inExpression.setRightExpression(subSelect);
expression =
ObjectUtils.isNotEmpty(expression) ? new OrExpression(expression, inExpression) : inExpression;
expression = null != expression ? new OrExpression(expression, inExpression) : inExpression;
}
}
return ObjectUtils.isNotEmpty(where) ? new AndExpression(where, new Parenthesis(expression)) : expression;
return null != where ? new AndExpression(where, new Parenthesis(expression)) : expression;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNull(metaObject)) {
if (null == metaObject) {
return;
}

Expand Down Expand Up @@ -88,7 +88,7 @@ public void insertFill(MetaObject metaObject) {
@Override
public void updateFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNull(metaObject)) {
if (null == metaObject) {
return;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ public void updateFill(MetaObject metaObject) {
private void fillFieldValue(MetaObject metaObject, String fieldName, Object fillFieldValue, boolean isOverride) {
if (metaObject.hasSetter(fieldName)) {
Object fieldValue = metaObject.getValue(fieldName);
setFieldValByName(fieldName, fieldValue != null && !isOverride ? fieldValue : fillFieldValue, metaObject);
setFieldValByName(fieldName, null != fieldValue && !isOverride ? fieldValue : fillFieldValue, metaObject);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMappi
@Override
protected RequestMappingInfo getMappingForMethod(@NonNull Method method, @NonNull Class<?> handlerType) {
RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);
if (requestMappingInfo == null) {
if (null == requestMappingInfo) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class PageDataVO<V> implements Serializable {
* @return 分页信息
*/
public static <T, V> PageDataVO<V> build(IPage<T> page, Class<V> targetClass) {
if (page == null) {
if (null == page) {
return null;
}
PageDataVO<V> pageDataVO = new PageDataVO<>();
Expand All @@ -88,7 +88,7 @@ public static <T, V> PageDataVO<V> build(IPage<T> page, Class<V> targetClass) {
* @return 分页信息
*/
public static <V> PageDataVO<V> build(IPage<V> page) {
if (page == null) {
if (null == page) {
return null;
}
PageDataVO<V> pageDataVO = new PageDataVO<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ExceptionUtils {
* 异常
*/
public static void printException(Runnable runnable, Throwable throwable) {
if (throwable == null && runnable instanceof Future<?>) {
if (null == throwable && runnable instanceof Future<?>) {
try {
Future<?> future = (Future<?>)runnable;
if (future.isDone()) {
Expand All @@ -60,7 +60,7 @@ public static void printException(Runnable runnable, Throwable throwable) {
Thread.currentThread().interrupt();
}
}
if (throwable != null) {
if (null != throwable) {
log.error(throwable.getMessage(), throwable);
}
}
Expand Down Expand Up @@ -135,9 +135,9 @@ public static <T> T exToDefault(ExSupplier<T> exSupplier, T defaultValue) {
public static <T> T exToDefault(ExSupplier<T> exSupplier, T defaultValue, Consumer<Exception> exConsumer) {
try {
return exSupplier.get();
} catch (Exception ex) {
if (exConsumer != null) {
exConsumer.accept(ex);
} catch (Exception e) {
if (null != exConsumer) {
exConsumer.accept(e);
}
return defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static String getLocalCityInfo(String ip) {
}
Ip2regionSearcher ip2regionSearcher = SpringUtil.getBean(Ip2regionSearcher.class);
IpInfo ipInfo = ip2regionSearcher.memorySearch(ip);
if (ipInfo != null) {
if (null != ipInfo) {
return ipInfo.getAddress();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static HttpServletResponse getResponse() {
* @return 浏览器及其版本信息
*/
public static String getBrowser(HttpServletRequest request) {
if (request == null) {
if (null == request) {
return null;
}
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class LoginHelper {
* 登录用户信息
*/
public static void login(LoginUser loginUser) {
if (loginUser == null) {
if (null == loginUser) {
return;
}

Expand All @@ -64,7 +64,7 @@ public static void login(LoginUser loginUser) {
loginUser.setLocation(IpUtils.getCityInfo(loginUser.getClientIp()));
loginUser.setBrowser(ServletUtils.getBrowser(request));
LogContext logContext = LogContextHolder.get();
loginUser.setLoginTime(logContext != null ? logContext.getCreateTime() : LocalDateTime.now());
loginUser.setLoginTime(null != logContext ? logContext.getCreateTime() : LocalDateTime.now());

// 登录保存用户信息
StpUtil.login(loginUser.getId());
Expand All @@ -79,7 +79,7 @@ public static void login(LoginUser loginUser) {
*/
public static LoginUser getLoginUser() {
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConsts.LOGIN_USER_KEY);
if (loginUser != null) {
if (null != loginUser) {
return loginUser;
}
loginUser = (LoginUser)StpUtil.getTokenSession().get(CacheConsts.LOGIN_USER_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class QueryHelper {
public static <Q, R> QueryWrapper<R> build(Q query) {
QueryWrapper<R> queryWrapper = new QueryWrapper<>();
// 没有查询条件,直接返回
if (query == null) {
if (null == query) {
return queryWrapper;
}

Expand Down Expand Up @@ -90,7 +90,7 @@ private static <Q, R> void buildQuery(Q query, Field field, QueryWrapper<R> quer
field.setAccessible(true);
// 没有 @Query,直接返回
Query queryAnnotation = field.getAnnotation(Query.class);
if (queryAnnotation == null) {
if (null == queryAnnotation) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Validator {
* 异常类型
*/
protected static void throwIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(obj == null, message, exceptionType);
throwIf(null == obj, message, exceptionType);
}

/**
Expand All @@ -61,7 +61,7 @@ protected static void throwIfNull(Object obj, String message, Class<? extends Ru
* 异常类型
*/
protected static void throwIfNotNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(obj != null, message, exceptionType);
throwIf(null != obj, message, exceptionType);
}

/**
Expand Down Expand Up @@ -219,7 +219,7 @@ protected static void throwIf(boolean condition, String message, Class<? extends
*/
protected static void throwIf(BooleanSupplier conditionSupplier, String message,
Class<? extends RuntimeException> exceptionType) {
if (conditionSupplier != null && conditionSupplier.getAsBoolean()) {
if (null != conditionSupplier && conditionSupplier.getAsBoolean()) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
Expand Down
Loading

0 comments on commit 487fa82

Please sign in to comment.