浏览代码

wrap EnumSet into unmodifiableSet directly, plus minor changes

javanna 9 年之前
父节点
当前提交
f9a5e1a03a

+ 10 - 9
core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java

@@ -38,6 +38,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Predicate;
 
 import static org.elasticsearch.common.transport.TransportAddressSerializers.addressToStream;
 
@@ -88,7 +89,7 @@ public class DiscoveryNode implements Writeable<DiscoveryNode>, ToXContent {
     private final TransportAddress address;
     private final Map<String, String> attributes;
     private final Version version;
-    private final EnumSet<Role> roles;
+    private final Set<Role> roles;
 
     /**
      * Creates a new {@link DiscoveryNode} by reading from the stream provided as argument
@@ -211,16 +212,16 @@ public class DiscoveryNode implements Writeable<DiscoveryNode>, ToXContent {
         }
         this.attributes = Collections.unmodifiableMap(attributes);
         //verify that no node roles are being provided as attributes
-        boolean assertEnabled = false;
-        assert assertEnabled = true;
-        if (assertEnabled) {
+        Predicate<Map<String, String>> predicate =  (attrs) -> {
             for (Role role : Role.values()) {
-                assert attributes.containsKey(role.getRoleName()) == false : "role name [" + role.getRoleName() + "] found in attributes";
+                assert attrs.containsKey(role.getRoleName()) == false;
             }
-        }
-        Set<Role> rolesSet = Collections.unmodifiableSet(roles);
-        this.roles = EnumSet.noneOf(Role.class);
-        this.roles.addAll(rolesSet);
+            return true;
+        };
+        assert predicate.test(attributes);
+        Set<Role> rolesSet = EnumSet.noneOf(Role.class);
+        rolesSet.addAll(roles);
+        this.roles = Collections.unmodifiableSet(rolesSet);
     }
 
     /**

+ 1 - 4
core/src/main/java/org/elasticsearch/tribe/TribeService.java

@@ -364,10 +364,7 @@ public class TribeService extends AbstractLifecycleComponent<TribeService> {
             for (DiscoveryNode tribe : tribeState.nodes()) {
                 if (currentState.nodes().get(tribe.id()) == null) {
                     // a new node, add it, but also add the tribe name to the attributes
-                    Map<String, String> tribeAttr = new HashMap<>();
-                    for (Map.Entry<String, String> entry : tribe.getAttributes().entrySet()) {
-                        tribeAttr.put(entry.getKey(), entry.getValue());
-                    }
+                    Map<String, String> tribeAttr = new HashMap<>(tribe.getAttributes());
                     tribeAttr.put(TRIBE_NAME_SETTING.getKey(), tribeName);
                     DiscoveryNode discoNode = new DiscoveryNode(tribe.name(), tribe.id(), tribe.getHostName(), tribe.getHostAddress(),
                             tribe.address(), unmodifiableMap(tribeAttr), tribe.getRoles(), tribe.version());