Bump upstream versions
[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         ShardIdentifier that = (ShardIdentifier) obj;
56
57         if (!memberName.equals(that.memberName)) {
58             return false;
59         }
60         if (!shardName.equals(that.shardName)) {
61             return false;
62         }
63         if (!type.equals(that.type)) {
64             return false;
65         }
66
67         return true;
68     }
69
70     @Override
71     public int hashCode() {
72         int result = shardName.hashCode();
73         result = 31 * result + memberName.hashCode();
74         result = 31 * result + type.hashCode();
75         return result;
76     }
77
78     @Override
79     public String toString() {
80         // ensure the output of toString matches the pattern above
81         return fullName;
82     }
83
84     public String getShardName() {
85         return shardName;
86     }
87
88     public MemberName getMemberName() {
89         return memberName;
90     }
91
92     public String getType() {
93         return type;
94     }
95
96     public static class Builder {
97         private String shardName;
98         private MemberName memberName;
99         private String type;
100
101         public ShardIdentifier build() {
102             return new ShardIdentifier(shardName, memberName, type);
103         }
104
105         public Builder shardName(final String newShardName) {
106             this.shardName = newShardName;
107             return this;
108         }
109
110         public Builder memberName(final MemberName newMemberName) {
111             this.memberName = newMemberName;
112             return this;
113         }
114
115         public Builder type(final String newType) {
116             this.type = newType;
117             return this;
118         }
119
120         public Builder fromShardIdString(final String shardId) {
121             Matcher matcher = PATTERN.matcher(shardId);
122
123             if (matcher.matches()) {
124                 memberName = MemberName.forName(matcher.group(1));
125                 shardName = matcher.group(2);
126                 type = matcher.group(3);
127             }
128             return this;
129         }
130     }
131 }