package org.opendaylight.controller.cluster.datastore.identifiers;
import com.google.common.base.Preconditions;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
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 String type;
-
+ private final String fullName;
public ShardIdentifier(String shardName, String memberName, String type) {
this.shardName = shardName;
this.memberName = memberName;
this.type = type;
+
+ fullName = new StringBuilder(memberName).append("-shard-").append(shardName).append("-")
+ .append(type).toString();
}
@Override
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 static Builder builder(){
return new Builder();
}
+ public String getShardName() {
+ return shardName;
+ }
+
+ public String getMemberName() {
+ return memberName;
+ }
+
+ public String getType() {
+ return type;
+ }
+
public static class Builder {
private String shardName;
private String memberName;
return this;
}
+ public Builder fromShardIdString(String shardId){
+ Matcher matcher = PATTERN.matcher(shardId);
+
+ if (matcher.matches()) {
+ memberName = matcher.group(1);
+ shardName = matcher.group(2);
+ type = matcher.group(3);
+ }
+ return this;
+ }
}
}