Added requuired-capabilities to the impl/.../config/default-config.xml and added...
[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
15 public class ShardIdentifier {
16     // This pattern needs to remain in sync with toString(), which produces
17     // strings with corresponding to "%s-shard-%s-%s"
18     private static final Pattern PATTERN = Pattern.compile("(\\S+)-shard-(\\S+)-(\\S+)");
19
20     private final String shardName;
21     private final String memberName;
22     private final String type;
23
24     public ShardIdentifier(String shardName, String memberName, String type) {
25
26         Preconditions.checkNotNull(shardName, "shardName should not be null");
27         Preconditions.checkNotNull(memberName, "memberName should not be null");
28         Preconditions.checkNotNull(type, "type should not be null");
29
30         this.shardName = shardName;
31         this.memberName = memberName;
32         this.type = type;
33     }
34
35     @Override
36     public boolean equals(Object o) {
37         if (this == o) {
38             return true;
39         }
40         if (o == null || getClass() != o.getClass()) {
41             return false;
42         }
43
44         ShardIdentifier that = (ShardIdentifier) o;
45
46         if (!memberName.equals(that.memberName)) {
47             return false;
48         }
49         if (!shardName.equals(that.shardName)) {
50             return false;
51         }
52         if (!type.equals(that.type)) {
53             return false;
54         }
55
56         return true;
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 public String toString() {
68         //ensure the output of toString matches the pattern above
69         return new StringBuilder(memberName)
70                     .append("-shard-")
71                     .append(shardName)
72                     .append("-")
73                     .append(type)
74                     .toString();
75     }
76
77     public static Builder builder(){
78         return new Builder();
79     }
80
81     public String getShardName() {
82         return shardName;
83     }
84
85     public String getMemberName() {
86         return memberName;
87     }
88
89     public String getType() {
90         return type;
91     }
92
93     public static class Builder {
94         private String shardName;
95         private String memberName;
96         private String type;
97
98         public ShardIdentifier build(){
99             return new ShardIdentifier(shardName, memberName, type);
100         }
101
102         public Builder shardName(String shardName){
103             this.shardName = shardName;
104             return this;
105         }
106
107         public Builder memberName(String memberName){
108             this.memberName = memberName;
109             return this;
110         }
111
112         public Builder type(String type){
113             this.type = type;
114             return this;
115         }
116
117         public Builder fromShardIdString(String shardId){
118             Matcher matcher = PATTERN.matcher(shardId);
119
120             if (matcher.matches()) {
121                 memberName = matcher.group(1);
122                 shardName = matcher.group(2);
123                 type = matcher.group(3);
124             }
125             return this;
126         }
127     }
128 }