Browse Source

优化数据字典

zhou-hao 6 years ago
parent
commit
084caf8bd4

+ 18 - 0
hsweb-core/src/main/java/org/hswebframework/web/dict/EnumDict.java

@@ -96,6 +96,10 @@ public interface EnumDict<V> extends JSONSerializable {
         return (mask & getMask()) != 0;
     }
 
+    default boolean in(EnumDict<V>... dict) {
+        return in(toMask(dict));
+    }
+
     /**
      * 枚举选项的描述,对一个选项进行详细的描述有时候是必要的.默认值为{@link this#getText()}
      *
@@ -163,6 +167,20 @@ public interface EnumDict<V> extends JSONSerializable {
         return value;
     }
 
+
+    @SafeVarargs
+    static <T extends Enum & EnumDict> boolean in(T target, T... t) {
+        Enum[] all = target.getClass().getEnumConstants();
+
+        if (all.length >= 64) {
+            List<T> list = Arrays.asList(t);
+            return Arrays.stream(all)
+                    .map(EnumDict.class::cast)
+                    .anyMatch(list::contains);
+        }
+        return maskIn(toMask(t), target);
+    }
+
     @SafeVarargs
     static <T extends EnumDict> boolean maskIn(long mask, T... t) {
         long value = toMask(t);

+ 7 - 0
hsweb-core/src/test/java/org/hswebframework/web/dict/DictDefineTest.java

@@ -56,6 +56,13 @@ public class DictDefineTest {
             Assert.assertTrue(userCode.in(bit));
         }
 
+        Assert.assertTrue(UserCode.SIMPLE.in(UserCode.SIMPLE,UserCode.CODE1,UserCode.CODE2));
+        Assert.assertFalse(UserCode.CODE6.in(UserCode.SIMPLE,UserCode.CODE1,UserCode.CODE2));
+
+        Assert.assertTrue(EnumDict.in(UserCode.SIMPLE,UserCode.SIMPLE,UserCode.CODE1,UserCode.CODE2));
+        Assert.assertFalse(EnumDict.in(UserCode.CODE7,UserCode.SIMPLE,UserCode.CODE1,UserCode.CODE2));
+
+
         List<UserCode> codes = getByMask(UserCode.class, bit);