1   /*
2    * Copyright 2021 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.client;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static java.util.Objects.requireNonNull;
20  
21  import java.time.Duration;
22  
23  import com.linecorp.centraldogma.common.EntryNotFoundException;
24  
25  /**
26   * Options for a watch request.
27   */
28  abstract class WatchOptions {
29  
30      private long timeoutMillis = WatchConstants.DEFAULT_WATCH_TIMEOUT_MILLIS;
31      private boolean errorOnEntryNotFound = WatchConstants.DEFAULT_WATCH_ERROR_ON_ENTRY_NOT_FOUND;
32  
33      /**
34       * Sets the timeout for a watch request.
35       */
36      WatchOptions timeout(Duration timeout) {
37          requireNonNull(timeout, "timeout");
38          checkArgument(!timeout.isZero() && !timeout.isNegative(), "timeout: %s (expected: > 0)", timeout);
39          return timeoutMillis(timeout.toMillis());
40      }
41  
42      /**
43       * Sets the timeout for a watch request in milliseconds.
44       */
45      WatchOptions timeoutMillis(long timeoutMillis) {
46          checkArgument(timeoutMillis > 0, "timeoutMillis: %s (expected: > 0)", timeoutMillis);
47          this.timeoutMillis = timeoutMillis;
48          return this;
49      }
50  
51      long timeoutMillis() {
52          return timeoutMillis;
53      }
54  
55      /**
56       * Sets whether to throw an {@link EntryNotFoundException} if the watch target does not exist.
57       */
58      public WatchOptions errorOnEntryNotFound(boolean errorOnEntryNotFound) {
59          this.errorOnEntryNotFound = errorOnEntryNotFound;
60          return this;
61      }
62  
63      boolean errorOnEntryNotFound() {
64          return errorOnEntryNotFound;
65      }
66  }