Commit 5ca5a63f authored by kezhenxu94's avatar kezhenxu94 Committed by Jason Song

polishing code with lambdas or method reference (#1929)

parent 7e4ba422
...@@ -77,29 +77,21 @@ public class ConfigFileController implements ReleaseMessageListener { ...@@ -77,29 +77,21 @@ public class ConfigFileController implements ReleaseMessageListener {
final GrayReleaseRulesHolder grayReleaseRulesHolder) { final GrayReleaseRulesHolder grayReleaseRulesHolder) {
localCache = CacheBuilder.newBuilder() localCache = CacheBuilder.newBuilder()
.expireAfterWrite(EXPIRE_AFTER_WRITE, TimeUnit.MINUTES) .expireAfterWrite(EXPIRE_AFTER_WRITE, TimeUnit.MINUTES)
.weigher(new Weigher<String, String>() { .weigher((Weigher<String, String>) (key, value) -> value == null ? 0 : value.length())
@Override
public int weigh(String key, String value) {
return value == null ? 0 : value.length();
}
})
.maximumWeight(MAX_CACHE_SIZE) .maximumWeight(MAX_CACHE_SIZE)
.removalListener(new RemovalListener<String, String>() { .removalListener(notification -> {
@Override String cacheKey = notification.getKey();
public void onRemoval(RemovalNotification<String, String> notification) { logger.debug("removing cache key: {}", cacheKey);
String cacheKey = notification.getKey(); if (!cacheKey2WatchedKeys.containsKey(cacheKey)) {
logger.debug("removing cache key: {}", cacheKey); return;
if (!cacheKey2WatchedKeys.containsKey(cacheKey)) { }
return; //create a new list to avoid ConcurrentModificationException
} List<String> watchedKeys = new ArrayList<>(cacheKey2WatchedKeys.get(cacheKey));
//create a new list to avoid ConcurrentModificationException for (String watchedKey : watchedKeys) {
List<String> watchedKeys = new ArrayList<>(cacheKey2WatchedKeys.get(cacheKey)); watchedKeys2CacheKey.remove(watchedKey, cacheKey);
for (String watchedKey : watchedKeys) {
watchedKeys2CacheKey.remove(watchedKey, cacheKey);
}
cacheKey2WatchedKeys.removeAll(cacheKey);
logger.debug("removed cache key: {}", cacheKey);
} }
cacheKey2WatchedKeys.removeAll(cacheKey);
logger.debug("removed cache key: {}", cacheKey);
}) })
.build(); .build();
propertiesResponseHeaders = new HttpHeaders(); propertiesResponseHeaders = new HttpHeaders();
......
...@@ -47,8 +47,7 @@ public class ConsumerRolePermissionService { ...@@ -47,8 +47,7 @@ public class ConsumerRolePermissionService {
} }
Set<Long> roleIds = Set<Long> roleIds =
FluentIterable.from(consumerRoles).transform(consumerRole -> consumerRole.getRoleId()) FluentIterable.from(consumerRoles).transform(ConsumerRole::getRoleId).toSet();
.toSet();
List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds); List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
if (CollectionUtils.isEmpty(rolePermissions)) { if (CollectionUtils.isEmpty(rolePermissions)) {
return false; return false;
......
...@@ -295,9 +295,7 @@ public class AdminServiceAPI { ...@@ -295,9 +295,7 @@ public class AdminServiceAPI {
parameters.add("comment", releaseComment); parameters.add("comment", releaseComment);
parameters.add("operator", operator); parameters.add("operator", operator);
parameters.add("isEmergencyPublish", String.valueOf(isEmergencyPublish)); parameters.add("isEmergencyPublish", String.valueOf(isEmergencyPublish));
grayDelKeys.forEach(key ->{ grayDelKeys.forEach(key -> parameters.add("grayDelKeys",key));
parameters.add("grayDelKeys",key);
});
HttpEntity<MultiValueMap<String, String>> entity = HttpEntity<MultiValueMap<String, String>> entity =
new HttpEntity<>(parameters, headers); new HttpEntity<>(parameters, headers);
ReleaseDTO response = restTemplate.post( ReleaseDTO response = restTemplate.post(
......
...@@ -27,22 +27,17 @@ public class WebContextConfiguration { ...@@ -27,22 +27,17 @@ public class WebContextConfiguration {
@Bean @Bean
public ServletContextInitializer servletContextInitializer() { public ServletContextInitializer servletContextInitializer() {
return servletContext -> {
return new ServletContextInitializer() { String loggingServerIP = portalConfig.cloggingUrl();
String loggingServerPort = portalConfig.cloggingPort();
@Override String credisServiceUrl = portalConfig.credisServiceUrl();
public void onStartup(ServletContext servletContext) throws ServletException {
String loggingServerIP = portalConfig.cloggingUrl(); servletContext.setInitParameter("loggingServerIP",
String loggingServerPort = portalConfig.cloggingPort(); Strings.isNullOrEmpty(loggingServerIP) ? "" : loggingServerIP);
String credisServiceUrl = portalConfig.credisServiceUrl(); servletContext.setInitParameter("loggingServerPort",
Strings.isNullOrEmpty(loggingServerPort) ? "" : loggingServerPort);
servletContext.setInitParameter("loggingServerIP", servletContext.setInitParameter("credisServiceUrl",
Strings.isNullOrEmpty(loggingServerIP) ? "" : loggingServerIP); Strings.isNullOrEmpty(credisServiceUrl) ? "" : credisServiceUrl);
servletContext.setInitParameter("loggingServerPort",
Strings.isNullOrEmpty(loggingServerPort) ? "" : loggingServerPort);
servletContext.setInitParameter("credisServiceUrl",
Strings.isNullOrEmpty(credisServiceUrl) ? "" : credisServiceUrl);
}
}; };
} }
......
package com.ctrip.framework.apollo.portal.spi.defaultimpl; 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.App;
import com.ctrip.framework.apollo.common.entity.BaseEntity;
import com.ctrip.framework.apollo.core.ConfigConsts; 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.PermissionType;
import com.ctrip.framework.apollo.portal.constant.RoleType; import com.ctrip.framework.apollo.portal.constant.RoleType;
import com.ctrip.framework.apollo.portal.entity.po.Permission; import com.ctrip.framework.apollo.portal.entity.po.Permission;
...@@ -16,11 +13,15 @@ import com.ctrip.framework.apollo.portal.service.RoleInitializationService; ...@@ -16,11 +13,15 @@ import com.ctrip.framework.apollo.portal.service.RoleInitializationService;
import com.ctrip.framework.apollo.portal.service.RolePermissionService; import com.ctrip.framework.apollo.portal.service.RolePermissionService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder; import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.util.RoleUtils; 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.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; 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. * Created by timothy on 2017/4/26.
...@@ -114,7 +115,7 @@ public class DefaultRoleInitializationService implements RoleInitializationServi ...@@ -114,7 +115,7 @@ public class DefaultRoleInitializationService implements RoleInitializationServi
Set<Permission> createdAppPermissions = rolePermissionService.createPermissions(appPermissions); Set<Permission> createdAppPermissions = rolePermissionService.createPermissions(appPermissions);
Set<Long> Set<Long>
appPermissionIds = appPermissionIds =
FluentIterable.from(createdAppPermissions).transform(permission -> permission.getId()).toSet(); createdAppPermissions.stream().map(BaseEntity::getId).collect(Collectors.toSet());
//create app master role //create app master role
Role appMasterRole = createRole(RoleUtils.buildAppMasterRoleName(appId), operator); Role appMasterRole = createRole(RoleUtils.buildAppMasterRoleName(appId), operator);
......
...@@ -18,15 +18,16 @@ import com.google.common.collect.HashMultimap; ...@@ -18,15 +18,16 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Sets; 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.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; 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. * Created by timothy on 2017/4/26.
...@@ -85,7 +86,7 @@ public class DefaultRolePermissionService implements RolePermissionService { ...@@ -85,7 +86,7 @@ public class DefaultRolePermissionService implements RolePermissionService {
List<UserRole> existedUserRoles = List<UserRole> existedUserRoles =
userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId()); userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
Set<String> existedUserIds = 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); Set<String> toAssignUserIds = Sets.difference(userIds, existedUserIds);
...@@ -170,7 +171,7 @@ public class DefaultRolePermissionService implements RolePermissionService { ...@@ -170,7 +171,7 @@ public class DefaultRolePermissionService implements RolePermissionService {
} }
Set<Long> roleIds = 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); List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
if (CollectionUtils.isEmpty(rolePermissions)) { if (CollectionUtils.isEmpty(rolePermissions)) {
return false; return false;
......
...@@ -16,7 +16,7 @@ public class ConfigToFileUtils { ...@@ -16,7 +16,7 @@ public class ConfigToFileUtils {
public static void itemsToFile(OutputStream os, List<String> items) { public static void itemsToFile(OutputStream os, List<String> items) {
try { try {
PrintWriter printWriter = new PrintWriter(os); PrintWriter printWriter = new PrintWriter(os);
items.forEach(item -> printWriter.println(item)); items.forEach(printWriter::println);
printWriter.close(); printWriter.close();
} catch (Exception e) { } catch (Exception e) {
throw e; throw e;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment