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;
23 private final String fullName;
25 public ShardIdentifier(String shardName, String memberName, String type) {
27 Preconditions.checkNotNull(shardName, "shardName should not be null");
28 Preconditions.checkNotNull(memberName, "memberName should not be null");
29 Preconditions.checkNotNull(type, "type should not be null");
31 this.shardName = shardName;
32 this.memberName = memberName;
35 fullName = new StringBuilder(memberName).append("-shard-").append(shardName).append("-")
36 .append(type).toString();
40 public boolean equals(Object o) {
44 if (o == null || getClass() != o.getClass()) {
48 ShardIdentifier that = (ShardIdentifier) o;
50 if (!memberName.equals(that.memberName)) {
53 if (!shardName.equals(that.shardName)) {
56 if (!type.equals(that.type)) {
64 public int hashCode() {
65 int result = shardName.hashCode();
66 result = 31 * result + memberName.hashCode();
67 result = 31 * result + type.hashCode();
72 public String toString() {
73 //ensure the output of toString matches the pattern above
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);