Make ModuleConfig immutable
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / ModuleConfig.java
index b54946774d2a4bc7cd266bf93370a91f5ccf0d77..97be5622a423ed307facecc9e21d70abde8f4cca 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.controller.cluster.datastore.config;
 
-import com.google.common.collect.ImmutableSet;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
 
 /**
@@ -21,61 +23,98 @@ import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy
  */
 public class ModuleConfig {
     private final String name;
-    private String nameSpace;
-    private ShardStrategy shardStrategy;
-    private final Map<String, ShardConfig> shardConfigs = new HashMap<>();
+    private final String nameSpace;
+    private final ShardStrategy shardStrategy;
+    private final Map<String, ShardConfig> shardConfigs;
 
-    public ModuleConfig(final String name) {
+    private ModuleConfig(String name, String nameSpace, ShardStrategy shardStrategy,
+            Map<String, ShardConfig> shardConfigs) {
         this.name = name;
+        this.nameSpace = nameSpace;
+        this.shardStrategy = shardStrategy;
+        this.shardConfigs = shardConfigs;
     }
 
-    public ModuleConfig(ModuleConfig moduleConfig) {
-        this.name = moduleConfig.getName();
-        this.nameSpace = moduleConfig.getNameSpace();
-        this.shardStrategy = moduleConfig.getShardStrategy();
-        for (ShardConfig shardConfig : moduleConfig.getShardConfigs()) {
-            shardConfigs.put(shardConfig.getName(), new ShardConfig(shardConfig.getName(),
-                ImmutableSet.copyOf(shardConfig.getReplicas())));
-        }
-    }
-
+    @Nonnull
     public String getName() {
         return name;
     }
 
+    @Nullable
     public String getNameSpace() {
         return nameSpace;
     }
 
+    @Nullable
     public ShardStrategy getShardStrategy() {
         return shardStrategy;
     }
 
+    @Nullable
     public ShardConfig getShardConfig(String name) {
         return shardConfigs.get(name);
     }
 
+    @Nonnull
     public Collection<ShardConfig> getShardConfigs() {
         return shardConfigs.values();
     }
 
+    @Nonnull
     public Collection<String> getShardNames() {
         return shardConfigs.keySet();
     }
 
-    public void addShardConfig(String name, Set<String> replicas) {
-        shardConfigs.put(name, new ShardConfig(name, replicas));
+    public static Builder builder(String name) {
+        return new Builder(name);
     }
 
-    public void setNameSpace(String nameSpace) {
-        this.nameSpace = nameSpace;
+    public static Builder builder(ModuleConfig moduleConfig) {
+        return new Builder(moduleConfig);
     }
 
-    public void setShardStrategy(ShardStrategy shardStrategy) {
-        this.shardStrategy = shardStrategy;
-    }
+    public static class Builder {
+        private String name;
+        private String nameSpace;
+        private ShardStrategy shardStrategy;
+        private final Map<String, ShardConfig> shardConfigs = new HashMap<>();
 
-    public ShardConfig removeShardConfig(String name) {
-        return shardConfigs.remove(name);
+        private Builder(String name) {
+            this.name = name;
+        }
+
+        private Builder(ModuleConfig moduleConfig) {
+            this.name = moduleConfig.getName();
+            this.nameSpace = moduleConfig.getNameSpace();
+            this.shardStrategy = moduleConfig.getShardStrategy();
+            for (ShardConfig shardConfig : moduleConfig.getShardConfigs()) {
+                shardConfigs.put(shardConfig.getName(), shardConfig);
+            }
+        }
+
+        public Builder name(String name) {
+            this.name = name;
+            return this;
+        }
+
+        public Builder nameSpace(String nameSpace) {
+            this.nameSpace = nameSpace;
+            return this;
+        }
+
+        public Builder shardStrategy(ShardStrategy shardStrategy) {
+            this.shardStrategy = shardStrategy;
+            return this;
+        }
+
+        public Builder shardConfig(String name, Collection<String> replicas) {
+            shardConfigs.put(name, new ShardConfig(name, replicas));
+            return this;
+        }
+
+        public ModuleConfig build() {
+            return new ModuleConfig(Preconditions.checkNotNull(name), nameSpace, shardStrategy,
+                    ImmutableMap.copyOf(shardConfigs));
+        }
     }
 }