View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper;
16  
17  import static com.google.common.base.Preconditions.checkArgument;
18  
19  import java.util.Locale;
20  import java.util.function.Supplier;
21  
22  import com.google.common.flogger.FluentLogger;
23  
24  public enum IgnoreWarnFailCreate {
25      IGNORE, WARN, FAIL, CREATE;
26  
27      private static final FluentLogger LOG = FluentLogger.forEnclosingClass();
28  
29      public static IgnoreWarnFailCreate forString(final String value) {
30          checkArgument(value != null, "the value can not be null");
31          return Enum.valueOf(IgnoreWarnFailCreate.class, value.toUpperCase(Locale.getDefault()));
32      }
33  
34      public static boolean checkIgnoreWarnFailCreateState(final boolean check, final IgnoreWarnFailCreate iwfc,
35          final Supplier<String> checkMessage, final Supplier<String> errorMessage) {
36  
37          if (check) {
38              LOG.atFine().log(checkMessage.get());
39              return false;
40          }
41  
42          switch (iwfc) {
43              case IGNORE:
44                  return false;
45              case WARN:
46                  LOG.atWarning().log(errorMessage.get());
47                  return false;
48              case FAIL:
49                  throw new IllegalStateException(errorMessage.get());
50              case CREATE:
51                  LOG.atFine().log(errorMessage.get());
52                  return true;
53              default:
54                  throw new IllegalStateException("Unknown state: " + iwfc);
55          }
56      }
57  }