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 final var that = (ShardIdentifier) obj;
56 return memberName.equals(that.memberName) && shardName.equals(that.shardName) && 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();
68 public String toString() {
69 // ensure the output of toString matches the pattern above
73 public String getShardName() {
77 public MemberName getMemberName() {
81 public String getType() {
85 public static class Builder {
86 private String shardName;
87 private MemberName memberName;
90 public ShardIdentifier build() {
91 return new ShardIdentifier(shardName, memberName, type);
94 public Builder shardName(final String newShardName) {
95 shardName = newShardName;
99 public Builder memberName(final MemberName newMemberName) {
100 memberName = newMemberName;
104 public Builder type(final String newType) {
109 public Builder fromShardIdString(final String shardId) {
110 Matcher matcher = PATTERN.matcher(shardId);
112 if (matcher.matches()) {
113 memberName = MemberName.forName(matcher.group(1));
114 shardName = matcher.group(2);
115 type = matcher.group(3);