Merge "Cleanup RpcRoutingStrategy definition"
[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
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16 public class ShardIdentifier {
17     private final String shardName;
18     private final String memberName;
19     private final String type;
20
21     //format and pattern should be in sync
22     private final String format = "%s-shard-%s-%s";
23     private static final Pattern pattern = Pattern.compile("(\\S+)-shard-(\\S+)-(\\S+)");
24
25     public ShardIdentifier(String shardName, String memberName, String type) {
26
27         Preconditions.checkNotNull(shardName, "shardName should not be null");
28         Preconditions.checkNotNull(memberName, "memberName should not be null");
29         Preconditions.checkNotNull(type, "type should not be null");
30
31         this.shardName = shardName;
32         this.memberName = memberName;
33         this.type = type;
34     }
35
36     @Override
37     public boolean equals(Object o) {
38         if (this == o) {
39             return true;
40         }
41         if (o == null || getClass() != o.getClass()) {
42             return false;
43         }
44
45         ShardIdentifier that = (ShardIdentifier) o;
46
47         if (!memberName.equals(that.memberName)) {
48             return false;
49         }
50         if (!shardName.equals(that.shardName)) {
51             return false;
52         }
53         if (!type.equals(that.type)) {
54             return false;
55         }
56
57         return true;
58     }
59
60     @Override
61     public int hashCode() {
62         int result = shardName.hashCode();
63         result = 31 * result + memberName.hashCode();
64         result = 31 * result + type.hashCode();
65         return result;
66     }
67
68     @Override public String toString() {
69         //ensure the output of toString matches the pattern above
70         return new StringBuilder(memberName)
71                     .append("-shard-")
72                     .append(shardName)
73                     .append("-")
74                     .append(type)
75                     .toString();
76     }
77
78     public static Builder builder(){
79         return new Builder();
80     }
81
82     public String getShardName() {
83         return shardName;
84     }
85
86     public String 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 String 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(String 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 = matcher.group(1);
123                 shardName = matcher.group(2);
124                 type = matcher.group(3);
125             }
126             return this;
127         }
128     }
129 }