Skip to content

Commit

Permalink
refactor: Use an empty collection instead of null return value
Browse files Browse the repository at this point in the history
  • Loading branch information
youngzil committed Dec 10, 2024
1 parent 47dd5d3 commit c7ec8a1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.ctrip.framework.apollo.audit.dto.ApolloAuditLogDataInfluenceDTO;
import com.ctrip.framework.apollo.audit.dto.ApolloAuditLogDetailsDTO;
import com.ctrip.framework.apollo.audit.dto.ApolloAuditLogDTO;
import java.util.Collections;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -50,28 +51,28 @@ public void appendDataInfluences(List<Object> entities, Class<?> beanDefinition)

@Override
public List<ApolloAuditLogDTO> queryLogs(int page, int size) {
return null;
return Collections.emptyList();
}

@Override
public List<ApolloAuditLogDTO> queryLogsByOpName(String opName, Date startDate,
Date endDate, int page, int size) {
return null;
return Collections.emptyList();
}

@Override
public List<ApolloAuditLogDetailsDTO> queryTraceDetails(String traceId) {
return null;
return Collections.emptyList();
}

@Override
public List<ApolloAuditLogDataInfluenceDTO> queryDataInfluencesByField(String entityName,
String entityId, String fieldName, int page, int size) {
return null;
return Collections.emptyList();
}

@Override
public List<ApolloAuditLogDTO> searchLogByNameOrTypeOrOperator(String query, int page, int size) {
return null;
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.ctrip.framework.apollo.core.ConfigConsts;
import com.google.common.base.Splitter;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -42,7 +43,7 @@ public static List<String> messageToList(String message) {
//message should be appId+cluster+namespace
if (keys.size() != 3) {
logger.error("message format invalid - {}", message);
return null;
return Collections.emptyList();
}
return keys;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
Expand Down Expand Up @@ -101,7 +102,7 @@ private static List<String> resolveLocations(Collection<String> locations,
private static Collection<String> convertRepositoryLocations(Collection<String> locations,
DataSource dataSource) {
if (CollectionUtils.isEmpty(locations)) {
return null;
return Collections.emptyList();
}
String repositoryDir = findRepositoryDirectory();
String suffix = findSuffix(dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public Set<UserInfo> queryUsersWithRole(String roleName) {
List<UserRole> userRoles = userRoleRepository.findByRoleId(role.getId());
List<UserInfo> userInfos = userService.findByUserIds(userRoles.stream().map(UserRole::getUserId).collect(Collectors.toList()));

if(userInfos == null){
if (CollectionUtils.isEmpty(userInfos)) {
return Collections.emptySet();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,44 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.springframework.util.CollectionUtils;

/**
* @author Jason Song([email protected])
*/
public class DefaultUserService implements UserService {

private static final String DEFAULT_USER_ID = "apollo";

@Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit, boolean includeInactiveUsers) {
return Collections.singletonList(assembleDefaultUser());
}

@Override
public UserInfo findByUserId(String userId) {
if (Objects.equals(userId, "apollo")) {
if (Objects.equals(userId, DEFAULT_USER_ID)) {
return assembleDefaultUser();
}
return null;
}

@Override
public List<UserInfo> findByUserIds(List<String> userIds) {
if (userIds.contains("apollo")) {
if (CollectionUtils.isEmpty(userIds)) {
return Collections.emptyList();
}

if (userIds.contains(DEFAULT_USER_ID)) {
return Lists.newArrayList(assembleDefaultUser());
}
return null;
return Collections.emptyList();
}

private UserInfo assembleDefaultUser() {
UserInfo defaultUser = new UserInfo();
defaultUser.setUserId("apollo");
defaultUser.setName("apollo");
defaultUser.setUserId(DEFAULT_USER_ID);
defaultUser.setName(DEFAULT_USER_ID);
defaultUser.setEmail("[email protected]");

return defaultUser;
Expand Down

0 comments on commit c7ec8a1

Please sign in to comment.