Bläddra i källkod

改写枚举类 消除ifelse

liuchuang 4 år sedan
förälder
incheckning
7d3b4592b3

+ 47 - 22
src/main/java/io/renren/common/utils/Constant.java

@@ -1,21 +1,30 @@
 /**
  * Copyright (c) 2016-2019 人人开源 All rights reserved.
- *
+ * <p>
  * https://www.renren.io
- *
+ * <p>
  * 版权所有,侵权必究!
  */
 
 package io.renren.common.utils;
 
+import io.renren.common.validator.group.AliyunGroup;
+import io.renren.common.validator.group.QcloudGroup;
+import io.renren.common.validator.group.QiniuGroup;
+
+import java.util.Optional;
+import java.util.stream.Stream;
+
 /**
  * 常量
  *
  * @author Mark sunlightcs@gmail.com
  */
 public class Constant {
-	/** 超级管理员ID */
-	public static final int SUPER_ADMIN = 1;
+    /**
+     * 超级管理员ID
+     */
+    public static final int SUPER_ADMIN = 1;
     /**
      * 当前页码
      */
@@ -33,21 +42,22 @@ public class Constant {
      */
     public static final String ORDER = "order";
     /**
-     *  升序
+     * 升序
      */
     public static final String ASC = "asc";
-	/**
-	 * 菜单类型
-	 * 
-	 * @author chenshun
-	 * @email sunlightcs@gmail.com
-	 * @date 2016年11月15日 下午1:24:29
-	 */
+
+    /**
+     * 菜单类型
+     *
+     * @author chenshun
+     * @email sunlightcs@gmail.com
+     * @date 2016年11月15日 下午1:24:29
+     */
     public enum MenuType {
         /**
          * 目录
          */
-    	CATALOG(0),
+        CATALOG(0),
         /**
          * 菜单
          */
@@ -67,10 +77,10 @@ public class Constant {
             return value;
         }
     }
-    
+
     /**
      * 定时任务状态
-     * 
+     *
      * @author chenshun
      * @email sunlightcs@gmail.com
      * @date 2016年12月3日 上午12:07:22
@@ -79,18 +89,18 @@ public class Constant {
         /**
          * 正常
          */
-    	NORMAL(0),
+        NORMAL(0),
         /**
          * 暂停
          */
-    	PAUSE(1);
+        PAUSE(1);
 
         private int value;
 
         ScheduleStatus(int value) {
             this.value = value;
         }
-        
+
         public int getValue() {
             return value;
         }
@@ -103,25 +113,40 @@ public class Constant {
         /**
          * 七牛云
          */
-        QINIU(1),
+        QINIU(1, QiniuGroup.class),
         /**
          * 阿里云
          */
-        ALIYUN(2),
+        ALIYUN(2, AliyunGroup.class),
         /**
          * 腾讯云
          */
-        QCLOUD(3);
+        QCLOUD(3, QcloudGroup.class);
 
         private int value;
 
-        CloudService(int value) {
+        private Class<?> validatorGroupClass;
+
+        CloudService(int value, Class<?> validatorGroupClass) {
             this.value = value;
+            this.validatorGroupClass = validatorGroupClass;
         }
 
         public int getValue() {
             return value;
         }
+
+        public Class<?> getValidatorGroupClass() {
+            return this.validatorGroupClass;
+        }
+
+        public static CloudService getByValue(Integer value) {
+            Optional<CloudService> first = Stream.of(CloudService.values()).filter(cs -> value.equals(cs.value)).findFirst();
+            if (!first.isPresent()) {
+                throw new IllegalArgumentException("非法的枚举值:" + value);
+            }
+            return first.get();
+        }
     }
 
 }

+ 8 - 3
src/main/java/io/renren/common/validator/ValidatorUtils.java

@@ -1,14 +1,15 @@
 /**
  * Copyright (c) 2016-2019 人人开源 All rights reserved.
- *
+ * <p>
  * https://www.renren.io
- *
+ * <p>
  * 版权所有,侵权必究!
  */
 
 package io.renren.common.validator;
 
 import io.renren.common.exception.RRException;
+import io.renren.common.utils.Constant;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.Validation;
@@ -40,10 +41,14 @@ public class ValidatorUtils {
         Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
         if (!constraintViolations.isEmpty()) {
             StringBuilder msg = new StringBuilder();
-            for(ConstraintViolation<Object> constraint:  constraintViolations){
+            for (ConstraintViolation<Object> constraint : constraintViolations) {
                 msg.append(constraint.getMessage()).append("<br>");
             }
             throw new RRException(msg.toString());
         }
     }
+
+    public static void validateEntity(Object object, Constant.CloudService type) {
+        validateEntity(object, type.getValidatorGroupClass());
+    }
 }

+ 1 - 11
src/main/java/io/renren/modules/oss/controller/SysOssController.java

@@ -79,17 +79,7 @@ public class SysOssController {
 	public R saveConfig(@RequestBody CloudStorageConfig config){
 		//校验类型
 		ValidatorUtils.validateEntity(config);
-
-		if(config.getType() == Constant.CloudService.QINIU.getValue()){
-			//校验七牛数据
-			ValidatorUtils.validateEntity(config, QiniuGroup.class);
-		}else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
-			//校验阿里云数据
-			ValidatorUtils.validateEntity(config, AliyunGroup.class);
-		}else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
-			//校验腾讯云数据
-			ValidatorUtils.validateEntity(config, QcloudGroup.class);
-		}
+		ValidatorUtils.validateEntity(config, Constant.CloudService.getByValue(config.getType()));
 
         sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));