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.util.Map;
21  import java.util.concurrent.CompletableFuture;
22  
23  import com.linecorp.centraldogma.common.Entry;
24  import com.linecorp.centraldogma.common.EntryType;
25  import com.linecorp.centraldogma.common.PathPattern;
26  import com.linecorp.centraldogma.common.Revision;
27  
28  /**
29   * Prepares to send a {@link CentralDogma#getFiles(String, String, Revision, PathPattern)} or
30   * {@link CentralDogma#listFiles(String, String, Revision, PathPattern)} request to the
31   * Central Dogma repository.
32   */
33  public final class FilesRequest {
34  
35      private final CentralDogmaRepository centralDogmaRepo;
36      private final PathPattern pathPattern;
37  
38      FilesRequest(CentralDogmaRepository centralDogmaRepo, PathPattern pathPattern) {
39          this.centralDogmaRepo = centralDogmaRepo;
40          this.pathPattern = pathPattern;
41      }
42  
43      /**
44       * Retrieves the list of the files matched by the given path pattern at the {@link Revision#HEAD}.
45       *
46       * @return a {@link Map} of file path and type pairs
47       */
48      public CompletableFuture<Map<String, EntryType>> list() {
49          return list(Revision.HEAD);
50      }
51  
52      /**
53       * Retrieves the list of the files matched by the given path pattern at the {@link Revision}.
54       *
55       * @return a {@link Map} of file path and type pairs
56       */
57      public CompletableFuture<Map<String, EntryType>> list(Revision revision) {
58          requireNonNull(revision, "revision");
59          return centralDogmaRepo.centralDogma().listFiles(centralDogmaRepo.projectName(),
60                                                           centralDogmaRepo.repositoryName(),
61                                                           revision, pathPattern);
62      }
63  
64      /**
65       * Retrieves the files matched by the path pattern at the {@link Revision#HEAD}.
66       *
67       * @return a {@link Map} of file path and {@link Entry} pairs
68       */
69      public CompletableFuture<Map<String, Entry<?>>> get() {
70          return get(Revision.HEAD);
71      }
72  
73      /**
74       * Retrieves the files matched by the path pattern at the {@link Revision}.
75       *
76       * @return a {@link Map} of file path and {@link Entry} pairs
77       */
78      public CompletableFuture<Map<String, Entry<?>>> get(Revision revision) {
79          requireNonNull(revision, "revision");
80          return centralDogmaRepo.centralDogma().getFiles(centralDogmaRepo.projectName(),
81                                                          centralDogmaRepo.repositoryName(),
82                                                          revision, pathPattern);
83      }
84  }