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 c6928815938058dbddc7bdb8b920c60fa2e0f7da..fc3aa592f97ce44063149d449e320073d330c8ed 100644 (file)
@@ -9,22 +9,37 @@
 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
+    // strings with corresponding to "%s-shard-%s-%s"
+    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;
+
+    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");
 
+        fullName = memberName.getName() + "-shard-" + shardName + "-" + type;
+    }
 
-    public ShardIdentifier(String shardName, String memberName, String type) {
+    public static ShardIdentifier create(final String shardName, final MemberName memberName, final String type) {
+        return new ShardIdentifier(shardName, memberName, type);
+    }
 
-        Preconditions.checkNotNull(shardName, "shardName should not be null");
-        Preconditions.checkNotNull(memberName, "memberName should not be null");
-        Preconditions.checkNotNull(type, "type should not be null");
+    public static ShardIdentifier fromShardIdString(final String shardIdString) {
+        final Matcher matcher = PATTERN.matcher(shardIdString);
+        Preconditions.checkArgument(matcher.matches(), "Invalid shard id \"%s\"", shardIdString);
 
-        this.shardName = shardName;
-        this.memberName = memberName;
-        this.type = type;
+        return new ShardIdentifier(matcher.group(2), MemberName.forName(matcher.group(1)), matcher.group(3));
     }
 
     @Override
@@ -59,19 +74,27 @@ public class ShardIdentifier {
         return result;
     }
 
-    @Override public String toString() {
-        StringBuilder builder = new StringBuilder();
-        builder.append(memberName).append("-shard-").append(shardName).append("-").append(type);
-        return builder.toString();
+    @Override
+    public String toString() {
+        // ensure the output of toString matches the pattern above
+        return fullName;
+    }
+
+    public String getShardName() {
+        return shardName;
+    }
+
+    public MemberName getMemberName() {
+        return memberName;
     }
 
-    public static Builder builder(){
-        return new Builder();
+    public String getType() {
+        return type;
     }
 
     public static class Builder {
         private String shardName;
-        private String memberName;
+        private MemberName memberName;
         private String type;
 
         public ShardIdentifier build(){
@@ -83,7 +106,7 @@ public class ShardIdentifier {
             return this;
         }
 
-        public Builder memberName(String memberName){
+        public Builder memberName(MemberName memberName){
             this.memberName = memberName;
             return this;
         }
@@ -93,5 +116,15 @@ public class ShardIdentifier {
             return this;
         }
 
+        public Builder fromShardIdString(String shardId){
+            Matcher matcher = PATTERN.matcher(shardId);
+
+            if (matcher.matches()) {
+                memberName = MemberName.forName(matcher.group(1));
+                shardName = matcher.group(2);
+                type = matcher.group(3);
+            }
+            return this;
+        }
     }
 }