2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore.identifiers;
11 import com.google.common.base.Preconditions;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
15 public class ShardIdentifier {
16 // This pattern needs to remain in sync with toString(), which produces
17 // strings with corresponding to "%s-shard-%s-%s"
18 private static final Pattern PATTERN = Pattern.compile("(\\S+)-shard-(\\S+)-(\\S+)");
20 private final String shardName;
21 private final String memberName;
22 private final String type;
24 public ShardIdentifier(String shardName, String memberName, String type) {
26 Preconditions.checkNotNull(shardName, "shardName should not be null");
27 Preconditions.checkNotNull(memberName, "memberName should not be null");
28 Preconditions.checkNotNull(type, "type should not be null");
30 this.shardName = shardName;
31 this.memberName = memberName;
36 public boolean equals(Object o) {
40 if (o == null || getClass() != o.getClass()) {
44 ShardIdentifier that = (ShardIdentifier) o;
46 if (!memberName.equals(that.memberName)) {
49 if (!shardName.equals(that.shardName)) {
52 if (!type.equals(that.type)) {
60 public int hashCode() {
61 int result = shardName.hashCode();
62 result = 31 * result + memberName.hashCode();
63 result = 31 * result + type.hashCode();
67 @Override public String toString() {
68 //ensure the output of toString matches the pattern above
69 return new StringBuilder(memberName)
77 public static Builder builder(){
81 public String getShardName() {
85 public String getMemberName() {
89 public String getType() {
93 public static class Builder {
94 private String shardName;
95 private String memberName;
98 public ShardIdentifier build(){
99 return new ShardIdentifier(shardName, memberName, type);
102 public Builder shardName(String shardName){
103 this.shardName = shardName;
107 public Builder memberName(String memberName){
108 this.memberName = memberName;
112 public Builder type(String type){
117 public Builder fromShardIdString(String shardId){
118 Matcher matcher = PATTERN.matcher(shardId);
120 if (matcher.matches()) {
121 memberName = matcher.group(1);
122 shardName = matcher.group(2);
123 type = matcher.group(3);