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