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;
14 import org.opendaylight.controller.cluster.access.concepts.MemberName;
16 public class ShardIdentifier {
17 // This pattern needs to remain in sync with toString(), which produces
18 // strings with corresponding to "%s-shard-%s-%s"
19 private static final Pattern PATTERN = Pattern.compile("(\\S+)-shard-(\\S+)-(\\S+)");
21 private final String shardName;
22 private final MemberName memberName;
23 private final String type;
24 private final String fullName;
26 public ShardIdentifier(String shardName, MemberName memberName, String type) {
28 Preconditions.checkNotNull(shardName, "shardName should not be null");
29 Preconditions.checkNotNull(memberName, "memberName should not be null");
30 Preconditions.checkNotNull(type, "type should not be null");
32 this.shardName = shardName;
33 this.memberName = memberName;
36 fullName = new StringBuilder(memberName.getName()).append("-shard-").append(shardName).append("-")
37 .append(type).toString();
41 public boolean equals(Object o) {
45 if (o == null || getClass() != o.getClass()) {
49 ShardIdentifier that = (ShardIdentifier) o;
51 if (!memberName.equals(that.memberName)) {
54 if (!shardName.equals(that.shardName)) {
57 if (!type.equals(that.type)) {
65 public int hashCode() {
66 int result = shardName.hashCode();
67 result = 31 * result + memberName.hashCode();
68 result = 31 * result + type.hashCode();
73 public String toString() {
74 //ensure the output of toString matches the pattern above
78 public static Builder builder(){
82 public String getShardName() {
86 public MemberName getMemberName() {
90 public String getType() {
94 public static class Builder {
95 private String shardName;
96 private MemberName memberName;
99 public ShardIdentifier build(){
100 return new ShardIdentifier(shardName, memberName, type);
103 public Builder shardName(String shardName){
104 this.shardName = shardName;
108 public Builder memberName(MemberName memberName){
109 this.memberName = memberName;
113 public Builder type(String type){
118 public Builder fromShardIdString(String shardId){
119 Matcher matcher = PATTERN.matcher(shardId);
121 if (matcher.matches()) {
122 memberName = MemberName.forName(matcher.group(1));
123 shardName = matcher.group(2);
124 type = matcher.group(3);