// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.feed.client;

import java.time.Duration;
import java.util.Optional;
import java.util.OptionalInt;

/**
 * @author bjorncs
 * @author jonmv
 */
public class OperationParameters {

    public static Builder builder() { return new Builder(); }

    private OperationParameters(Builder builder) {

    }

    public boolean createIfNonExistent() { return false; }
    public Optional<String> testAndSetCondition() { return Optional.empty(); }
    public Optional<Duration> timeout() { return Optional.empty(); }
    public Optional<String> route() { return Optional.empty(); }
    public OptionalInt tracelevel() { return OptionalInt.empty(); }

    public static class Builder {

        private Builder() {}

        public OperationParameters build() { return new OperationParameters(this); }

    }
}

@@ -317,185 +317,791 @@
     
-public static Builder builder() %7B return new Builder(); %7D%0A%0A    private OperationParameters(Builder builder) %7B%0A%0A    %7D%0A%0A    public boolean createIfNonExistent() %7B return false;
+static final OperationParameters empty = new OperationParameters(false, null, null, null, 0);%0A%0A    private final boolean create;%0A    private final String condition;%0A    private final Duration timeout;%0A    private final String route;%0A    private final int tracelevel;%0A%0A    private OperationParameters(boolean create, String condition, Duration timeout, String route, int tracelevel) %7B%0A        this.create = create;%0A        this.condition = condition;%0A        this.timeout = timeout;%0A        this.route = route;%0A        this.tracelevel = tracelevel;%0A    %7D%0A%0A    public static OperationParameters empty() %7B return empty; %7D%0A%0A    public OperationParameters createIfNonExistent(boolean create) %7B%0A        return new OperationParameters(create, condition, timeout, route, tracelevel);%0A   
  %7D%0A
+%0A
     
@@ -1113,74 +1113,283 @@
 c Op
+era
 tion
-al%3CString%3E testAndSetCondition() %7B return Optional.empty();
+Parameters testAndSetCondition(String condition) %7B%0A        if (condition.isEmpty())%0A            throw new IllegalArgumentException(%22TestAndSetCondition must be non-empty%22);%0A%0A        return new OperationParameters(create, condition, timeout, route, tracelevel);%0A   
  %7D%0A
+%0A
     
@@ -1401,23 +1401,42 @@
 c Op
+era
 tion
-al%3C
+Parameters timeout(
 Duration
 %3E ti
@@ -1435,17 +1435,16 @@
 tion
-%3E
  timeout
 () %7B
@@ -1443,40 +1443,257 @@
 eout
-() %7B return Optional.empty();
+) %7B%0A        if (timeout.isNegative() %7C%7C timeout.isZero())%0A            throw new IllegalArgumentException(%22Timeout must be positive, but was %22 + timeout);%0A%0A        return new OperationParameters(create, condition, timeout, route, tracelevel);%0A   
  %7D%0A
+%0A
     
@@ -1705,60 +1705,247 @@
 c Op
+era
 tion
-al%3C
+Parameters route(
 String
-%3E
  route
-(
 ) %7B
- return Optional.empty();
+%0A        if (route.isEmpty())%0A            throw new IllegalArgumentException(%22Route must be non-empty%22);%0A%0A        return new OperationParameters(create, condition, timeout, route, tracelevel);%0A   
  %7D%0A
+%0A
     
@@ -1953,23 +1953,46 @@
 ublic Op
+era
 tion
-alI
+Parameters tracelevel(i
 nt trace
@@ -2000,113 +2000,386 @@
 evel
-(
 ) %7B
- return OptionalInt.empty(); %7D%0A%0A    public static class Builder %7B%0A%0A        private Builder() %7B%7D%0A%0A    
+%0A        if (tracelevel %3C 1 %7C%7C tracelevel %3E 9)%0A            throw new IllegalArgumentException(%22Tracelevel must be in %5B1, 9%5D%22);%0A%0A        return new OperationParameters(create, condition, timeout, route, tracelevel);%0A    %7D%0A%0A    public boolean createIfNonExistent() %7B return create; %7D%0A    public Optional%3CString%3E testAndSetCondition() %7B return Optional.ofNullable(condition); %7D%0A
     
@@ -2391,81 +2391,265 @@
 c Op
-era
 tion
-Parameters build() %7B return new OperationParameters(this); %7D%0A%0A    %7D
+al%3CDuration%3E timeout() %7B return Optional.ofNullable(timeout); %7D%0A    public Optional%3CString%3E route() %7B return Optional.ofNullable(route); %7D%0A    public OptionalInt tracelevel() %7B return tracelevel == 0 ? OptionalInt.empty() : OptionalInt.of(tracelevel); %7D%0A
 %0A%7D%0A

Implement OperationParameters