Bump versions 9.0.4-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / identifiers / ShardIdentifier.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.identifiers;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15 import org.opendaylight.controller.cluster.access.concepts.MemberName;
16
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+)");
21
22     private final String shardName;
23     private final MemberName memberName;
24     private final String type;
25     private final String fullName;
26
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");
31
32         fullName = memberName.getName() + "-shard-" + shardName + "-" + type;
33     }
34
35     public static ShardIdentifier create(final String shardName, final MemberName memberName, final String type) {
36         return new ShardIdentifier(shardName, memberName, type);
37     }
38
39     public static ShardIdentifier fromShardIdString(final String shardIdString) {
40         final Matcher matcher = PATTERN.matcher(shardIdString);
41         checkArgument(matcher.matches(), "Invalid shard id \"%s\"", shardIdString);
42
43         return new ShardIdentifier(matcher.group(2), MemberName.forName(matcher.group(1)), matcher.group(3));
44     }
45
46     @Override
47     public boolean equals(final Object obj) {
48         if (this == obj) {
49             return true;
50         }
51         if (obj == null || getClass() != obj.getClass()) {
52             return false;
53         }
54
55         final var that = (ShardIdentifier) obj;
56         return memberName.equals(that.memberName) && shardName.equals(that.shardName) && type.equals(that.type);
57     }
58
59     @Override
60     public int hashCode() {
61         int result = shardName.hashCode();
62         result = 31 * result + memberName.hashCode();
63         result = 31 * result + type.hashCode();
64         return result;
65     }
66
67     @Override
68     public String toString() {
69         // ensure the output of toString matches the pattern above
70         return fullName;
71     }
72
73     public String getShardName() {
74         return shardName;
75     }
76
77     public MemberName getMemberName() {
78         return memberName;
79     }
80
81     public String getType() {
82         return type;
83     }
84
85     public static class Builder {
86         private String shardName;
87         private MemberName memberName;
88         private String type;
89
90         public ShardIdentifier build() {
91             return new ShardIdentifier(shardName, memberName, type);
92         }
93
94         public Builder shardName(final String newShardName) {
95             shardName = newShardName;
96             return this;
97         }
98
99         public Builder memberName(final MemberName newMemberName) {
100             memberName = newMemberName;
101             return this;
102         }
103
104         public Builder type(final String newType) {
105             type = newType;
106             return this;
107         }
108
109         public Builder fromShardIdString(final String shardId) {
110             Matcher matcher = PATTERN.matcher(shardId);
111
112             if (matcher.matches()) {
113                 memberName = MemberName.forName(matcher.group(1));
114                 shardName = matcher.group(2);
115                 type = matcher.group(3);
116             }
117             return this;
118         }
119     }
120 }