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 ShardIdentifier(final String shardName, final MemberName memberName, final String type) {
27 this.shardName = Preconditions.checkNotNull(shardName, "shardName should not be null");
28 this.memberName = Preconditions.checkNotNull(memberName, "memberName should not be null");
29 this.type = Preconditions.checkNotNull(type, "type should not be null");
31 fullName = memberName.getName() + "-shard-" + shardName + "-" + type;
34 public static ShardIdentifier create(final String shardName, final MemberName memberName, final String type) {
35 return new ShardIdentifier(shardName, memberName, type);
38 public static ShardIdentifier fromShardIdString(final String shardIdString) {
39 final Matcher matcher = PATTERN.matcher(shardIdString);
40 Preconditions.checkArgument(matcher.matches(), "Invalid shard id \"%s\"", shardIdString);
42 return new ShardIdentifier(matcher.group(2), MemberName.forName(matcher.group(1)), matcher.group(3));
46 public boolean equals(final Object obj) {
50 if (obj == null || getClass() != obj.getClass()) {
54 ShardIdentifier that = (ShardIdentifier) obj;
56 if (!memberName.equals(that.memberName)) {
59 if (!shardName.equals(that.shardName)) {
62 if (!type.equals(that.type)) {
70 public int hashCode() {
71 int result = shardName.hashCode();
72 result = 31 * result + memberName.hashCode();
73 result = 31 * result + type.hashCode();
78 public String toString() {
79 // ensure the output of toString matches the pattern above
83 public String getShardName() {
87 public MemberName getMemberName() {
91 public String getType() {
95 public static class Builder {
96 private String shardName;
97 private MemberName memberName;
100 public ShardIdentifier build() {
101 return new ShardIdentifier(shardName, memberName, type);
104 public Builder shardName(final String newShardName) {
105 this.shardName = newShardName;
109 public Builder memberName(final MemberName newMemberName) {
110 this.memberName = newMemberName;
114 public Builder type(final String newType) {
119 public Builder fromShardIdString(final String shardId) {
120 Matcher matcher = PATTERN.matcher(shardId);
122 if (matcher.matches()) {
123 memberName = MemberName.forName(matcher.group(1));
124 shardName = matcher.group(2);
125 type = matcher.group(3);