/** Clears the post allocation flag for the specified shard */
 public Builder clearPostAllocationFlag(ShardId shardId) {
   assert this.index.equals(shardId.index().name());
   IndexShardRoutingTable indexShard = shards.get(shardId.id());
   shards.put(
       indexShard.shardId().id(),
       new IndexShardRoutingTable(indexShard.shardId(), indexShard.shards(), false));
   return this;
 }
 public Builder addReplica() {
   for (IntCursor cursor : shards.keys()) {
     int shardId = cursor.value;
     // version 0, will get updated when reroute will happen
     ImmutableShardRouting shard =
         new ImmutableShardRouting(index, shardId, null, false, ShardRoutingState.UNASSIGNED, 0);
     shards.put(
         shardId,
         new IndexShardRoutingTable.Builder(shards.get(shard.id())).addShard(shard).build());
   }
   return this;
 }
 /**
  * Adds a new shard routing (makes a copy of it), with reference data used from the index shard
  * routing table if it needs to be created.
  */
 public Builder addShard(IndexShardRoutingTable refData, ShardRouting shard) {
   IndexShardRoutingTable indexShard = shards.get(shard.id());
   if (indexShard == null) {
     indexShard =
         new IndexShardRoutingTable.Builder(refData.shardId(), refData.primaryAllocatedPostApi())
             .addShard(new ImmutableShardRouting(shard))
             .build();
   } else {
     indexShard =
         new IndexShardRoutingTable.Builder(indexShard)
             .addShard(new ImmutableShardRouting(shard))
             .build();
   }
   shards.put(indexShard.shardId().id(), indexShard);
   return this;
 }
 public Builder removeReplica() {
   for (IntCursor cursor : shards.keys()) {
     int shardId = cursor.value;
     IndexShardRoutingTable indexShard = shards.get(shardId);
     if (indexShard.replicaShards().isEmpty()) {
       // nothing to do here!
       return this;
     }
     // re-add all the current ones
     IndexShardRoutingTable.Builder builder =
         new IndexShardRoutingTable.Builder(
             indexShard.shardId(), indexShard.primaryAllocatedPostApi());
     for (ShardRouting shardRouting : indexShard) {
       builder.addShard(new ImmutableShardRouting(shardRouting));
     }
     // first check if there is one that is not assigned to a node, and remove it
     boolean removed = false;
     for (ShardRouting shardRouting : indexShard) {
       if (!shardRouting.primary() && !shardRouting.assignedToNode()) {
         builder.removeShard(shardRouting);
         removed = true;
         break;
       }
     }
     if (!removed) {
       for (ShardRouting shardRouting : indexShard) {
         if (!shardRouting.primary()) {
           builder.removeShard(shardRouting);
           removed = true;
           break;
         }
       }
     }
     shards.put(shardId, builder.build());
   }
   return this;
 }