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.common;
17  
18  import static com.linecorp.centraldogma.common.DefaultPathPattern.ALL;
19  import static com.linecorp.centraldogma.common.DefaultPathPattern.allPattern;
20  import static java.util.Objects.requireNonNull;
21  
22  import com.google.common.collect.ImmutableSet;
23  import com.google.common.collect.Streams;
24  
25  /**
26   * A path pattern that represents a variant of glob. For example:
27   * <ul>
28   *   <li>{@code "/**"} - all files</li>
29   *   <li>{@code "*.json"} - all JSON files</li>
30   *   <li>{@code "/foo/*.json"} - all JSON files under the directory {@code /foo}</li>
31   *   <li><code>"/&#42;/foo.txt"</code> - all files named {@code foo.txt} at the second depth level</li>
32   *   <li>{@code "*.json","/bar/*.txt"} - if you have more than one pattern you can supply them as
33   *                                       {@code varargs} or {@link Iterable}.
34   *                                       A file will be matched if <em>any</em> pattern matches.</li>
35   * </ul>
36   */
37  public interface PathPattern {
38  
39      /**
40       * Returns the path pattern that represents all files.
41       */
42      static PathPattern all() {
43          return allPattern;
44      }
45  
46      /**
47       * Creates a path pattern with the {@code patterns}.
48       */
49      static PathPattern of(String... patterns) {
50          return of(ImmutableSet.copyOf(requireNonNull(patterns, "patterns")));
51      }
52  
53      /**
54       * Creates a path pattern with the {@code patterns}.
55       */
56      static PathPattern of(Iterable<String> patterns) {
57          requireNonNull(patterns, "patterns");
58          if (Streams.stream(patterns).anyMatch(ALL::equals)) {
59              return allPattern;
60          }
61  
62          return new DefaultPathPattern(ImmutableSet.copyOf(patterns));
63      }
64  
65      /**
66       * Returns the path pattern that concatenates the {@code patterns} using ','.
67       */
68      String patternString();
69  
70      /**
71       * Returns the encoded {@link #patternString()} which just encodes a space to '%20'.
72       */
73      String encoded();
74  }