Skip to content

Commit

Permalink
polishing code with lambdas or method reference (apolloconfig#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhenxu94 authored and nobodyiam committed Jan 31, 2019
1 parent 7e4ba42 commit 5ca5a63
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,21 @@ public ConfigFileController(
final GrayReleaseRulesHolder grayReleaseRulesHolder) {
localCache = CacheBuilder.newBuilder()
.expireAfterWrite(EXPIRE_AFTER_WRITE, TimeUnit.MINUTES)
.weigher(new Weigher<String, String>() {
@Override
public int weigh(String key, String value) {
return value == null ? 0 : value.length();
}
})
.weigher((Weigher<String, String>) (key, value) -> value == null ? 0 : value.length())
.maximumWeight(MAX_CACHE_SIZE)
.removalListener(new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> notification) {
String cacheKey = notification.getKey();
logger.debug("removing cache key: {}", cacheKey);
if (!cacheKey2WatchedKeys.containsKey(cacheKey)) {
return;
}
//create a new list to avoid ConcurrentModificationException
List<String> watchedKeys = new ArrayList<>(cacheKey2WatchedKeys.get(cacheKey));
for (String watchedKey : watchedKeys) {
watchedKeys2CacheKey.remove(watchedKey, cacheKey);
}
cacheKey2WatchedKeys.removeAll(cacheKey);
logger.debug("removed cache key: {}", cacheKey);
.removalListener(notification -> {
String cacheKey = notification.getKey();
logger.debug("removing cache key: {}", cacheKey);
if (!cacheKey2WatchedKeys.containsKey(cacheKey)) {
return;
}
//create a new list to avoid ConcurrentModificationException
List<String> watchedKeys = new ArrayList<>(cacheKey2WatchedKeys.get(cacheKey));
for (String watchedKey : watchedKeys) {
watchedKeys2CacheKey.remove(watchedKey, cacheKey);
}
cacheKey2WatchedKeys.removeAll(cacheKey);
logger.debug("removed cache key: {}", cacheKey);
})
.build();
propertiesResponseHeaders = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public boolean consumerHasPermission(long consumerId, String permissionType, Str
}

Set<Long> roleIds =
FluentIterable.from(consumerRoles).transform(consumerRole -> consumerRole.getRoleId())
.toSet();
FluentIterable.from(consumerRoles).transform(ConsumerRole::getRoleId).toSet();
List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
if (CollectionUtils.isEmpty(rolePermissions)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ public ReleaseDTO createGrayDeletionRelease(String appId, Env env, String cluste
parameters.add("comment", releaseComment);
parameters.add("operator", operator);
parameters.add("isEmergencyPublish", String.valueOf(isEmergencyPublish));
grayDelKeys.forEach(key ->{
parameters.add("grayDelKeys",key);
});
grayDelKeys.forEach(key -> parameters.add("grayDelKeys",key));
HttpEntity<MultiValueMap<String, String>> entity =
new HttpEntity<>(parameters, headers);
ReleaseDTO response = restTemplate.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@ public WebContextConfiguration(final PortalConfig portalConfig, final UserInfoHo

@Bean
public ServletContextInitializer servletContextInitializer() {

return new ServletContextInitializer() {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
String loggingServerIP = portalConfig.cloggingUrl();
String loggingServerPort = portalConfig.cloggingPort();
String credisServiceUrl = portalConfig.credisServiceUrl();

servletContext.setInitParameter("loggingServerIP",
Strings.isNullOrEmpty(loggingServerIP) ? "" : loggingServerIP);
servletContext.setInitParameter("loggingServerPort",
Strings.isNullOrEmpty(loggingServerPort) ? "" : loggingServerPort);
servletContext.setInitParameter("credisServiceUrl",
Strings.isNullOrEmpty(credisServiceUrl) ? "" : credisServiceUrl);
}
return servletContext -> {
String loggingServerIP = portalConfig.cloggingUrl();
String loggingServerPort = portalConfig.cloggingPort();
String credisServiceUrl = portalConfig.credisServiceUrl();

servletContext.setInitParameter("loggingServerIP",
Strings.isNullOrEmpty(loggingServerIP) ? "" : loggingServerIP);
servletContext.setInitParameter("loggingServerPort",
Strings.isNullOrEmpty(loggingServerPort) ? "" : loggingServerPort);
servletContext.setInitParameter("credisServiceUrl",
Strings.isNullOrEmpty(credisServiceUrl) ? "" : credisServiceUrl);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.ctrip.framework.apollo.portal.spi.defaultimpl;

import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.entity.BaseEntity;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.constant.PermissionType;
import com.ctrip.framework.apollo.portal.constant.RoleType;
import com.ctrip.framework.apollo.portal.entity.po.Permission;
Expand All @@ -16,11 +13,15 @@
import com.ctrip.framework.apollo.portal.service.RolePermissionService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.util.RoleUtils;

import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Created by timothy on 2017/4/26.
Expand Down Expand Up @@ -114,7 +115,7 @@ private void createAppMasterRole(String appId, String operator) {
Set<Permission> createdAppPermissions = rolePermissionService.createPermissions(appPermissions);
Set<Long>
appPermissionIds =
FluentIterable.from(createdAppPermissions).transform(permission -> permission.getId()).toSet();
createdAppPermissions.stream().map(BaseEntity::getId).collect(Collectors.toSet());

//create app master role
Role appMasterRole = createRole(RoleUtils.buildAppMasterRoleName(appId), operator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
* Created by timothy on 2017/4/26.
Expand Down Expand Up @@ -85,7 +86,7 @@ public Set<String> assignRoleToUsers(String roleName, Set<String> userIds,
List<UserRole> existedUserRoles =
userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
Set<String> existedUserIds =
FluentIterable.from(existedUserRoles).transform(userRole -> userRole.getUserId()).toSet();
existedUserRoles.stream().map(UserRole::getUserId).collect(Collectors.toSet());

Set<String> toAssignUserIds = Sets.difference(userIds, existedUserIds);

Expand Down Expand Up @@ -170,7 +171,7 @@ public boolean userHasPermission(String userId, String permissionType, String ta
}

Set<Long> roleIds =
FluentIterable.from(userRoles).transform(userRole -> userRole.getRoleId()).toSet();
userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
if (CollectionUtils.isEmpty(rolePermissions)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ConfigToFileUtils {
public static void itemsToFile(OutputStream os, List<String> items) {
try {
PrintWriter printWriter = new PrintWriter(os);
items.forEach(item -> printWriter.println(item));
items.forEach(printWriter::println);
printWriter.close();
} catch (Exception e) {
throw e;
Expand Down

0 comments on commit 5ca5a63

Please sign in to comment.