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