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 java.util.Objects.requireNonNull;
19  
20  import java.time.Duration;
21  import java.util.concurrent.CompletableFuture;
22  
23  import com.linecorp.centraldogma.common.EntryNotFoundException;
24  import com.linecorp.centraldogma.common.PathPattern;
25  import com.linecorp.centraldogma.common.Revision;
26  
27  /**
28   * Prepares to send a {@link CentralDogma#watchRepository(String, String, Revision, PathPattern, long, boolean)}
29   * request to the Central Dogma repository.
30   */
31  public final class WatchFilesRequest extends WatchOptions {
32  
33      private final CentralDogmaRepository centralDogmaRepo;
34      private final PathPattern pathPattern;
35  
36      WatchFilesRequest(CentralDogmaRepository centralDogmaRepo, PathPattern pathPattern) {
37          this.centralDogmaRepo = centralDogmaRepo;
38          this.pathPattern = pathPattern;
39      }
40  
41      @Override
42      public WatchFilesRequest timeout(Duration timeout) {
43          return (WatchFilesRequest) super.timeout(timeout);
44      }
45  
46      @Override
47      public WatchFilesRequest timeoutMillis(long timeoutMillis) {
48          return (WatchFilesRequest) super.timeoutMillis(timeoutMillis);
49      }
50  
51      @Override
52      public WatchFilesRequest errorOnEntryNotFound(boolean errorOnEntryNotFound) {
53          return (WatchFilesRequest) super.errorOnEntryNotFound(errorOnEntryNotFound);
54      }
55  
56      /**
57       * Waits for the files matched by the {@link PathPattern} to be changed since the {@link Revision#HEAD}.
58       * If no changes were made within the {@link #timeoutMillis(long)}, the
59       * returned {@link CompletableFuture} will be completed with {@code null}.
60       *
61       * @return the latest known {@link Revision} which contains the changes for the matched files.
62       *         {@code null} if the files were not changed for {@code timeoutMillis} milliseconds
63       *         since the invocation of this method. {@link EntryNotFoundException} is raised if the
64       *         target does not exist.
65       */
66      public CompletableFuture<Revision> start() {
67          return start(Revision.HEAD);
68      }
69  
70      /**
71       * Waits for the files matched by the {@link PathPattern} to be changed since the {@code lastKnownRevision}.
72       * If no changes were made within the {@link #timeoutMillis(long)}, the
73       * returned {@link CompletableFuture} will be completed with {@code null}.
74       *
75       * @return the latest known {@link Revision} which contains the changes for the matched files.
76       *         {@code null} if the files were not changed for {@code timeoutMillis} milliseconds
77       *         since the invocation of this method. {@link EntryNotFoundException} is raised if the
78       *         target does not exist.
79       */
80      public CompletableFuture<Revision> start(Revision lastKnownRevision) {
81          requireNonNull(lastKnownRevision, "lastKnownRevision");
82          return centralDogmaRepo.centralDogma().watchRepository(centralDogmaRepo.projectName(),
83                                                                 centralDogmaRepo.repositoryName(),
84                                                                 lastKnownRevision, pathPattern,
85                                                                 timeoutMillis(), errorOnEntryNotFound());
86      }
87  }