1   /*
2    * Copyright 2017 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  
17  package com.linecorp.centraldogma.common;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import com.fasterxml.jackson.databind.JsonNode;
22  import com.google.common.base.Ascii;
23  
24  /**
25   * The type of an {@link Entry}.
26   */
27  public enum EntryType {
28      /**
29       * A UTF-8 encoded JSON file.
30       */
31      JSON(JsonNode.class),
32      /**
33       * A UTF-8 encoded text file.
34       */
35      TEXT(String.class),
36      /**
37       * A directory.
38       */
39      DIRECTORY(Void.class);
40  
41      /**
42       * Guesses the {@link EntryType} from the specified {@code path}.
43       */
44      public static EntryType guessFromPath(String path) {
45          requireNonNull(path, "path");
46          if (path.isEmpty()) {
47              throw new IllegalArgumentException("empty path");
48          }
49  
50          if (path.charAt(path.length() - 1) == '/') {
51              return DIRECTORY;
52          }
53  
54          if (Ascii.toLowerCase(path).endsWith(".json")) {
55              return JSON;
56          }
57  
58          return TEXT;
59      }
60  
61      private final Class<?> type;
62  
63      EntryType(Class<?> type) {
64          this.type = type;
65      }
66  
67      /**
68       * Returns the type of the content returned by {@link Entry#content()}.
69       */
70      public Class<?> type() {
71          return type;
72      }
73  }