BUG-5280: use MemberName instead of String
[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
9 package org.opendaylight.controller.cluster.datastore.identifiers;
10
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;
15
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+)");
20
21     private final String shardName;
22     private final MemberName memberName;
23     private final String type;
24     private final String fullName;
25
26     public ShardIdentifier(String shardName, MemberName memberName, String type) {
27
28         Preconditions.checkNotNull(shardName, "shardName should not be null");
29         Preconditions.checkNotNull(memberName, "memberName should not be null");
30         Preconditions.checkNotNull(type, "type should not be null");
31
32         this.shardName = shardName;
33         this.memberName = memberName;
34         this.type = type;
35
36         fullName = new StringBuilder(memberName.getName()).append("-shard-").append(shardName).append("-")
37                 .append(type).toString();
38     }
39
40     @Override
41     public boolean equals(Object o) {
42         if (this == o) {
43             return true;
44         }
45         if (o == null || getClass() != o.getClass()) {
46             return false;
47         }
48
49         ShardIdentifier that = (ShardIdentifier) o;
50
51         if (!memberName.equals(that.memberName)) {
52             return false;
53         }
54         if (!shardName.equals(that.shardName)) {
55             return false;
56         }
57         if (!type.equals(that.type)) {
58             return false;
59         }
60
61         return true;
62     }
63
64     @Override
65     public int hashCode() {
66         int result = shardName.hashCode();
67         result = 31 * result + memberName.hashCode();
68         result = 31 * result + type.hashCode();
69         return result;
70     }
71
72     @Override
73     public String toString() {
74         //ensure the output of toString matches the pattern above
75         return fullName;
76     }
77
78     public static Builder builder(){
79         return new Builder();
80     }
81
82     public String getShardName() {
83         return shardName;
84     }
85
86     public MemberName getMemberName() {
87         return memberName;
88     }
89
90     public String getType() {
91         return type;
92     }
93
94     public static class Builder {
95         private String shardName;
96         private MemberName memberName;
97         private String type;
98
99         public ShardIdentifier build(){
100             return new ShardIdentifier(shardName, memberName, type);
101         }
102
103         public Builder shardName(String shardName){
104             this.shardName = shardName;
105             return this;
106         }
107
108         public Builder memberName(MemberName memberName){
109             this.memberName = memberName;
110             return this;
111         }
112
113         public Builder type(String type){
114             this.type = type;
115             return this;
116         }
117
118         public Builder fromShardIdString(String shardId){
119             Matcher matcher = PATTERN.matcher(shardId);
120
121             if (matcher.matches()) {
122                 memberName = MemberName.forName(matcher.group(1));
123                 shardName = matcher.group(2);
124                 type = matcher.group(3);
125             }
126             return this;
127         }
128     }
129 }