BUG-5626: Eliminate ShardIdentifier.Builder
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / identifiers / ShardIdentifier.java
index 03bae2d99dedec37619e63c8b2924c1c7acddc20..fc3aa592f97ce44063149d449e320073d330c8ed 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.controller.cluster.datastore.identifiers;
 import com.google.common.base.Preconditions;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import org.opendaylight.controller.cluster.access.concepts.MemberName;
 
 public class ShardIdentifier {
     // This pattern needs to remain in sync with toString(), which produces
@@ -18,22 +19,27 @@ public class ShardIdentifier {
     private static final Pattern PATTERN = Pattern.compile("(\\S+)-shard-(\\S+)-(\\S+)");
 
     private final String shardName;
-    private final String memberName;
+    private final MemberName memberName;
     private final String type;
     private final String fullName;
 
-    public ShardIdentifier(String shardName, String memberName, String type) {
+    private ShardIdentifier(String shardName, MemberName memberName, String type) {
+        this.shardName = Preconditions.checkNotNull(shardName, "shardName should not be null");
+        this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
+        this.type = Preconditions.checkNotNull(type, "type should not be null");
 
-        Preconditions.checkNotNull(shardName, "shardName should not be null");
-        Preconditions.checkNotNull(memberName, "memberName should not be null");
-        Preconditions.checkNotNull(type, "type should not be null");
+        fullName = memberName.getName() + "-shard-" + shardName + "-" + type;
+    }
+
+    public static ShardIdentifier create(final String shardName, final MemberName memberName, final String type) {
+        return new ShardIdentifier(shardName, memberName, type);
+    }
 
-        this.shardName = shardName;
-        this.memberName = memberName;
-        this.type = type;
+    public static ShardIdentifier fromShardIdString(final String shardIdString) {
+        final Matcher matcher = PATTERN.matcher(shardIdString);
+        Preconditions.checkArgument(matcher.matches(), "Invalid shard id \"%s\"", shardIdString);
 
-        fullName = new StringBuilder(memberName).append("-shard-").append(shardName).append("-")
-                .append(type).toString();
+        return new ShardIdentifier(matcher.group(2), MemberName.forName(matcher.group(1)), matcher.group(3));
     }
 
     @Override
@@ -70,19 +76,15 @@ public class ShardIdentifier {
 
     @Override
     public String toString() {
-        //ensure the output of toString matches the pattern above
+        // ensure the output of toString matches the pattern above
         return fullName;
     }
 
-    public static Builder builder(){
-        return new Builder();
-    }
-
     public String getShardName() {
         return shardName;
     }
 
-    public String getMemberName() {
+    public MemberName getMemberName() {
         return memberName;
     }
 
@@ -92,7 +94,7 @@ public class ShardIdentifier {
 
     public static class Builder {
         private String shardName;
-        private String memberName;
+        private MemberName memberName;
         private String type;
 
         public ShardIdentifier build(){
@@ -104,7 +106,7 @@ public class ShardIdentifier {
             return this;
         }
 
-        public Builder memberName(String memberName){
+        public Builder memberName(MemberName memberName){
             this.memberName = memberName;
             return this;
         }
@@ -118,7 +120,7 @@ public class ShardIdentifier {
             Matcher matcher = PATTERN.matcher(shardId);
 
             if (matcher.matches()) {
-                memberName = matcher.group(1);
+                memberName = MemberName.forName(matcher.group(1));
                 shardName = matcher.group(2);
                 type = matcher.group(3);
             }