Browse Source

polishing code with lambdas or method reference (#1929)

kezhenxu94 6 years ago
parent
commit
5ca5a63f7d

+ 13 - 21
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigFileController.java

@@ -77,29 +77,21 @@ public class ConfigFileController implements ReleaseMessageListener {
       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();

+ 1 - 2
apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerRolePermissionService.java

@@ -47,8 +47,7 @@ public class ConsumerRolePermissionService {
     }
 
     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;

+ 1 - 3
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java

@@ -295,9 +295,7 @@ public class AdminServiceAPI {
       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(

+ 11 - 16
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/ctrip/WebContextConfiguration.java

@@ -27,22 +27,17 @@ public class WebContextConfiguration {
 
   @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);
     };
   }
 

+ 10 - 9
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRoleInitializationService.java

@@ -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;
@@ -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.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.
@@ -114,7 +115,7 @@ public class DefaultRoleInitializationService implements RoleInitializationServi
     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);

+ 6 - 5
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java

@@ -18,15 +18,16 @@ import com.google.common.collect.HashMultimap;
 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.
@@ -85,7 +86,7 @@ public class DefaultRolePermissionService implements RolePermissionService {
         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);
 
@@ -170,7 +171,7 @@ public class DefaultRolePermissionService implements RolePermissionService {
         }
 
         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;

+ 1 - 1
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/ConfigToFileUtils.java

@@ -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;