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 com.google.common.base.Preconditions.checkArgument;
20  import static java.util.Objects.requireNonNull;
21  
22  import com.fasterxml.jackson.databind.JsonNode;
23  import com.google.common.base.Ascii;
24  
25  /**
26   * The type of a {@link Change}.
27   */
28  public enum ChangeType {
29      /**
30       * Adds a new JSON file or replaces an existing file. {@link Change#content()} will return
31       * the {@link JsonNode} that represents the content of the file.
32       */
33      UPSERT_JSON(JsonNode.class),
34  
35      /**
36       * Adds a new text file or replaces an existing file. {@link Change#content()} will return
37       * the {@link String} that represents the content of the file.
38       */
39      UPSERT_TEXT(String.class),
40  
41      /**
42       * Removes an existing file. The {@link Change#content()} of this type is always {@code null}.
43       */
44      REMOVE(Void.class),
45  
46      /**
47       * Renames an existing file. The {@link Change#content()} of this type is the new path of the renamed file.
48       */
49      RENAME(String.class),
50  
51      /**
52       * Applies a JSON patch to a JSON file. The {@link Change#content()} of this type is a JSON patch object,
53       * as defined in <a href="https://tools.ietf.org/html/rfc6902">RFC 6902</a>.
54       */
55      APPLY_JSON_PATCH(JsonNode.class),
56  
57      /**
58       * Applies a textual patch to a text file. The {@link Change#content()} of this type is a
59       * <a href="https://en.wikipedia.org/wiki/Diff_utility#Unified_format">unified format</a> string.
60       */
61      APPLY_TEXT_PATCH(String.class);
62  
63      private final Class<?> contentType;
64  
65      ChangeType(Class<?> contentType) {
66          this.contentType = contentType;
67      }
68  
69      /**
70       * Returns the type of the content returned by {@link Change#content()}.
71       */
72      public Class<?> contentType() {
73          return contentType;
74      }
75  
76      /**
77       * Returns a {@link ChangeType} from the specified {@code value} case-insensitively.
78       */
79      public static ChangeType parse(String value) {
80          requireNonNull(value, "value");
81          checkArgument(!value.isEmpty(), "the value for ChangeType should not be empty.");
82  
83          return valueOf(Ascii.toUpperCase(value));
84      }
85  }