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.server.storage.repository;
18  
19  import java.util.Map;
20  
21  import javax.annotation.Nullable;
22  
23  import com.linecorp.centraldogma.internal.Util;
24  
25  /**
26   * An option which is specified when retrieving one or more files.
27   *
28   * @param <T> the type of the value
29   */
30  public class FindOption<T> {
31  
32      /**
33       * Whether to fetch the content of the found files. The default value is {@code true}.
34       */
35      public static final FindOption<Boolean> FETCH_CONTENT = new FindOption<>("FETCH_CONTENT", true);
36  
37      /**
38       * The maximum number of the fetched files. The default value is {@link Integer#MAX_VALUE}.
39       */
40      public static final FindOption<Integer> MAX_ENTRIES =
41              new FindOption<Integer>("MAX_ENTRIES", Integer.MAX_VALUE) {
42                  @Override
43                  boolean isValid(Integer value) {
44                      return value > 0;
45                  }
46              };
47  
48      private final String name;
49      private final T defaultValue;
50      private final String fullName;
51  
52      FindOption(String name, T defaultValue) {
53          this.name = name;
54          this.defaultValue = defaultValue;
55          fullName = Util.simpleTypeName(FindOption.class) + '.' + name;
56      }
57  
58      /**
59       * Returns the name of this option.
60       */
61      public String name() {
62          return name;
63      }
64  
65      /**
66       * Returns the default value of this option.
67       */
68      public T defaultValue() {
69          return defaultValue;
70      }
71  
72      /**
73       * Returns {@code true} if the specified {@code value} is valid.
74       */
75      boolean isValid(T value) {
76          return true;
77      }
78  
79      /**
80       * Returns the value if this option exists in the specified {@code options} map.
81       * Otherwise, the default value would be returned.
82       */
83      public T get(@Nullable Map<FindOption<?>, ?> options) {
84          if (options == null) {
85              return defaultValue();
86          }
87  
88          @SuppressWarnings("unchecked")
89          final T value = (T) options.get(this);
90          if (value == null) {
91              return defaultValue();
92          }
93  
94          return value;
95      }
96  
97      @Override
98      public String toString() {
99          return name();
100     }
101 }