class CreateOrReplaceHelper<T extends HasMetadata> {
  public static final int CREATE_OR_REPLACE_RETRIES = 3;
  private final UnaryOperator<T> createTask;
  private final UnaryOperator<T> replaceTask;
  private final UnaryOperator<T> waitTask;
  private final UnaryOperator<T> reloadTask;
  private final KubernetesSerialization serialization;

  public CreateOrReplaceHelper(UnaryOperator<T> createTask, UnaryOperator<T> replaceTask, UnaryOperator<T> waitTask,
      UnaryOperator<T> reloadTask, KubernetesSerialization serialization) {
    this.createTask = createTask;
    this.replaceTask = replaceTask;
    this.waitTask = waitTask;
    this.reloadTask = reloadTask;
    this.serialization = serialization;
  }

  public T createOrReplace(T item) {<FILL_FUNCTION_BODY>}

  private T replace(T item, String resourceVersion) {
    KubernetesResourceUtil.setResourceVersion(item, resourceVersion);
    return replaceTask.apply(item);
  }

  private boolean shouldRetry(int responseCode) {
    return responseCode > 499;
  }
}

    String resourceVersion = KubernetesResourceUtil.getResourceVersion(item);
    final CompletableFuture<T> future = new CompletableFuture<>();
    int nTries = 0;
    item = serialization.clone(item);
    while (!future.isDone() && nTries < CREATE_OR_REPLACE_RETRIES) {
      try {
        // Create
        KubernetesResourceUtil.setResourceVersion(item, null);
        return createTask.apply(item);
      } catch (KubernetesClientException exception) {
        if (shouldRetry(exception.getCode())) {
          T itemFromServer = reloadTask.apply(item);
          if (itemFromServer == null) {
            waitTask.apply(item);
            nTries++;
            continue;
          }
        } else if (exception.getCode() != HttpURLConnection.HTTP_CONFLICT) {
          throw exception;
        }

        future.complete(replace(item, resourceVersion));
      }
    }
    return future.join();
  
<no_super_class>