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
8 package org.opendaylight.controller.cluster.datastore.identifiers;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15 import org.opendaylight.controller.cluster.access.concepts.MemberName;
17 public class ShardIdentifier {
18 // This pattern needs to remain in sync with toString(), which produces
19 // strings with corresponding to "%s-shard-%s-%s"
20 private static final Pattern PATTERN = Pattern.compile("(\\S+)-shard-(\\S+)-(\\S+)");
22 private final String shardName;
23 private final MemberName memberName;
24 private final String type;
25 private final String fullName;
27 ShardIdentifier(final String shardName, final MemberName memberName, final String type) {
28 this.shardName = requireNonNull(shardName, "shardName should not be null");
29 this.memberName = requireNonNull(memberName, "memberName should not be null");
30 this.type = requireNonNull(type, "type should not be null");
32 fullName = memberName.getName() + "-shard-" + shardName + "-" + type;
35 public static ShardIdentifier create(final String shardName, final MemberName memberName, final String type) {
36 return new ShardIdentifier(shardName, memberName, type);
39 public static ShardIdentifier fromShardIdString(final String shardIdString) {
40 final Matcher matcher = PATTERN.matcher(shardIdString);
41 checkArgument(matcher.matches(), "Invalid shard id \"%s\"", shardIdString);
43 return new ShardIdentifier(matcher.group(2), MemberName.forName(matcher.group(1)), matcher.group(3));
47 public boolean equals(final Object obj) {
51 if (obj == null || getClass() != obj.getClass()) {
55 ShardIdentifier that = (ShardIdentifier) obj;
57 if (!memberName.equals(that.memberName)) {
60 if (!shardName.equals(that.shardName)) {
63 if (!type.equals(that.type)) {
71 public int hashCode() {
72 int result = shardName.hashCode();
73 result = 31 * result + memberName.hashCode();
74 result = 31 * result + type.hashCode();
79 public String toString() {
80 // ensure the output of toString matches the pattern above
84 public String getShardName() {
88 public MemberName getMemberName() {
92 public String getType() {
96 public static class Builder {
97 private String shardName;
98 private MemberName memberName;
101 public ShardIdentifier build() {
102 return new ShardIdentifier(shardName, memberName, type);
105 public Builder shardName(final String newShardName) {
106 this.shardName = newShardName;
110 public Builder memberName(final MemberName newMemberName) {
111 this.memberName = newMemberName;
115 public Builder type(final String newType) {
120 public Builder fromShardIdString(final String shardId) {
121 Matcher matcher = PATTERN.matcher(shardId);
123 if (matcher.matches()) {
124 memberName = MemberName.forName(matcher.group(1));
125 shardName = matcher.group(2);
126 type = matcher.group(3);