2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.identifiers;
11 import com.google.common.base.Preconditions;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
16 public class ShardIdentifier {
17 private final String shardName;
18 private final String memberName;
19 private final String type;
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+)");
25 public ShardIdentifier(String shardName, String memberName, String type) {
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");
31 this.shardName = shardName;
32 this.memberName = memberName;
37 public boolean equals(Object o) {
41 if (o == null || getClass() != o.getClass()) {
45 ShardIdentifier that = (ShardIdentifier) o;
47 if (!memberName.equals(that.memberName)) {
50 if (!shardName.equals(that.shardName)) {
53 if (!type.equals(that.type)) {
61 public int hashCode() {
62 int result = shardName.hashCode();
63 result = 31 * result + memberName.hashCode();
64 result = 31 * result + type.hashCode();
68 @Override public String toString() {
69 //ensure the output of toString matches the pattern above
70 return new StringBuilder(memberName)
78 public static Builder builder(){
82 public String getShardName() {
86 public String getMemberName() {
90 public String getType() {
94 public static class Builder {
95 private String shardName;
96 private String memberName;
99 public ShardIdentifier build(){
100 return new ShardIdentifier(shardName, memberName, type);
103 public Builder shardName(String shardName){
104 this.shardName = shardName;
108 public Builder memberName(String memberName){
109 this.memberName = memberName;
113 public Builder type(String type){
118 public Builder fromShardIdString(String shardId){
119 Matcher matcher = pattern.matcher(shardId);
121 if (matcher.matches()) {
122 memberName = matcher.group(1);
123 shardName = matcher.group(2);
124 type = matcher.group(3);