private void mergeLocalToBuilder() {
   if (this.containersToCleanup != null) {
     addContainersToCleanupToProto();
   }
   if (this.applicationsToCleanup != null) {
     addApplicationsToCleanupToProto();
   }
   if (this.containersToBeRemovedFromNM != null) {
     addContainersToBeRemovedFromNMToProto();
   }
   if (this.containerTokenMasterKey != null) {
     builder.setContainerTokenMasterKey(convertToProtoFormat(this.containerTokenMasterKey));
   }
   if (this.nmTokenMasterKey != null) {
     builder.setNmTokenMasterKey(convertToProtoFormat(this.nmTokenMasterKey));
   }
   if (this.systemCredentials != null) {
     addSystemCredentialsToProto();
   }
   if (this.containersToDecrease != null) {
     addContainersToDecreaseToProto();
   }
   if (this.containersToSignal != null) {
     addContainersToSignalToProto();
   }
 }
  private void addContainersToSignalToProto() {
    maybeInitBuilder();
    builder.clearContainersToSignal();
    if (containersToSignal == null) return;

    Iterable<SignalContainerRequestProto> iterable =
        new Iterable<SignalContainerRequestProto>() {
          @Override
          public Iterator<SignalContainerRequestProto> iterator() {
            return new Iterator<SignalContainerRequestProto>() {
              Iterator<SignalContainerRequest> iter = containersToSignal.iterator();

              @Override
              public boolean hasNext() {
                return iter.hasNext();
              }

              @Override
              public SignalContainerRequestProto next() {
                return convertToProtoFormat(iter.next());
              }

              @Override
              public void remove() {
                throw new UnsupportedOperationException();
              }
            };
          }
        };
    builder.addAllContainersToSignal(iterable);
  }
  private void addContainersToDecreaseToProto() {
    maybeInitBuilder();
    builder.clearContainersToDecrease();
    if (this.containersToDecrease == null) {
      return;
    }
    Iterable<ContainerProto> iterable =
        new Iterable<ContainerProto>() {
          @Override
          public Iterator<ContainerProto> iterator() {
            return new Iterator<ContainerProto>() {
              private Iterator<Container> iter = containersToDecrease.iterator();

              @Override
              public boolean hasNext() {
                return iter.hasNext();
              }

              @Override
              public ContainerProto next() {
                return convertToProtoFormat(iter.next());
              }

              @Override
              public void remove() {
                throw new UnsupportedOperationException();
              }
            };
          }
        };
    builder.addAllContainersToDecrease(iterable);
  }
  private void addContainersToBeRemovedFromNMToProto() {
    maybeInitBuilder();
    builder.clearContainersToBeRemovedFromNm();
    if (containersToBeRemovedFromNM == null) return;
    Iterable<ContainerIdProto> iterable =
        new Iterable<ContainerIdProto>() {

          @Override
          public Iterator<ContainerIdProto> iterator() {
            return new Iterator<ContainerIdProto>() {

              Iterator<ContainerId> iter = containersToBeRemovedFromNM.iterator();

              @Override
              public boolean hasNext() {
                return iter.hasNext();
              }

              @Override
              public ContainerIdProto next() {
                return convertToProtoFormat(iter.next());
              }

              @Override
              public void remove() {
                throw new UnsupportedOperationException();
              }
            };
          }
        };
    builder.addAllContainersToBeRemovedFromNm(iterable);
  }
  private void addApplicationsToCleanupToProto() {
    maybeInitBuilder();
    builder.clearApplicationsToCleanup();
    if (applicationsToCleanup == null) return;
    Iterable<ApplicationIdProto> iterable =
        new Iterable<ApplicationIdProto>() {

          @Override
          public Iterator<ApplicationIdProto> iterator() {
            return new Iterator<ApplicationIdProto>() {

              Iterator<ApplicationId> iter = applicationsToCleanup.iterator();

              @Override
              public boolean hasNext() {
                return iter.hasNext();
              }

              @Override
              public ApplicationIdProto next() {
                return convertToProtoFormat(iter.next());
              }

              @Override
              public void remove() {
                throw new UnsupportedOperationException();
              }
            };
          }
        };
    builder.addAllApplicationsToCleanup(iterable);
  }
 @Override
 public void setNodeAction(NodeAction nodeAction) {
   maybeInitBuilder();
   if (nodeAction == null) {
     builder.clearNodeAction();
     return;
   }
   builder.setNodeAction(convertToProtoFormat(nodeAction));
 }
 @Override
 public void setDiagnosticsMessage(String diagnosticsMessage) {
   maybeInitBuilder();
   if (diagnosticsMessage == null) {
     builder.clearDiagnosticsMessage();
     return;
   }
   builder.setDiagnosticsMessage((diagnosticsMessage));
 }
 private void addSystemCredentialsToProto() {
   maybeInitBuilder();
   builder.clearSystemCredentialsForApps();
   for (Map.Entry<ApplicationId, ByteBuffer> entry : systemCredentials.entrySet()) {
     builder.addSystemCredentialsForApps(
         SystemCredentialsForAppsProto.newBuilder()
             .setAppId(convertToProtoFormat(entry.getKey()))
             .setCredentialsForApp(ProtoUtils.convertToProtoFormat(entry.getValue().duplicate())));
   }
 }
 public NodeHeartbeatResponseProto getProto() {
   mergeLocalToProto();
   proto = viaProto ? proto : builder.build();
   viaProto = true;
   return proto;
 }
 @Override
 public void setNextHeartBeatInterval(long nextHeartBeatInterval) {
   maybeInitBuilder();
   builder.setNextHeartBeatInterval(nextHeartBeatInterval);
 }
 @Override
 public void setNMTokenMasterKey(MasterKey masterKey) {
   maybeInitBuilder();
   if (masterKey == null) builder.clearNmTokenMasterKey();
   this.nmTokenMasterKey = masterKey;
 }
 @Override
 public void setContainerTokenMasterKey(MasterKey masterKey) {
   maybeInitBuilder();
   if (masterKey == null) builder.clearContainerTokenMasterKey();
   this.containerTokenMasterKey = masterKey;
 }
 @Override
 public void setResponseId(int responseId) {
   maybeInitBuilder();
   builder.setResponseId((responseId));
 }
 private void mergeLocalToProto() {
   if (viaProto) maybeInitBuilder();
   mergeLocalToBuilder();
   proto = builder.build();
   viaProto = true;
 }