1   /*
2    * Copyright 2018 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.linecorp.centraldogma.internal.Util.validateFilePath;
20  
21  import com.google.common.base.MoreObjects;
22  
23  /**
24   * A merge source that contains a {@code path} and {@code isOptional} which indicates whether the path
25   * is required or not.
26   */
27  public final class MergeSource {
28  
29      /**
30       * Returns a newly-created {@link MergeSource} which contains a required path.
31       */
32      public static MergeSource ofRequired(String path) {
33          return new MergeSource(path, false);
34      }
35  
36      /**
37       * Returns a newly-created {@link MergeSource} which contains an optional path.
38       */
39      public static MergeSource ofOptional(String path) {
40          return new MergeSource(path, true);
41      }
42  
43      private final String path;
44  
45      private final boolean optional;
46  
47      /**
48       * Creates a new instance.
49       */
50      private MergeSource(String path, boolean optional) {
51          this.path = validateFilePath(path, "path");
52          this.optional = optional;
53      }
54  
55      /**
56       * Returns the path.
57       */
58      public String path() {
59          return path;
60      }
61  
62      /**
63       * Returns {@code true} if the path is optional.
64       */
65      public boolean isOptional() {
66          return optional;
67      }
68  
69      @Override
70      public int hashCode() {
71          return path.hashCode() * 31 + Boolean.hashCode(optional);
72      }
73  
74      @Override
75      public boolean equals(Object o) {
76          if (this == o) {
77              return true;
78          }
79          if (!(o instanceof MergeSource)) {
80              return false;
81          }
82  
83          @SuppressWarnings("unchecked")
84          final MergeSource that = (MergeSource) o;
85          return path.equals(that.path) && optional == that.optional;
86      }
87  
88      @Override
89      public String toString() {
90          return MoreObjects.toStringHelper(this)
91                            .add("path", path)
92                            .add("optional", optional)
93                            .toString();
94      }
95  }