Merge "Cleanup root pom "name"."
[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     private final String fullName;
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         fullName = new StringBuilder(memberName).append("-shard-").append(shardName).append("-")
36                 .append(type).toString();
37     }
38
39     @Override
40     public boolean equals(Object o) {
41         if (this == o) {
42             return true;
43         }
44         if (o == null || getClass() != o.getClass()) {
45             return false;
46         }
47
48         ShardIdentifier that = (ShardIdentifier) o;
49
50         if (!memberName.equals(that.memberName)) {
51             return false;
52         }
53         if (!shardName.equals(that.shardName)) {
54             return false;
55         }
56         if (!type.equals(that.type)) {
57             return false;
58         }
59
60         return true;
61     }
62
63     @Override
64     public int hashCode() {
65         int result = shardName.hashCode();
66         result = 31 * result + memberName.hashCode();
67         result = 31 * result + type.hashCode();
68         return result;
69     }
70
71     @Override
72     public String toString() {
73         //ensure the output of toString matches the pattern above
74         return fullName;
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 }