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.checkNotNull;
18  
19  import java.util.Locale;
20  import java.util.function.Supplier;
21  
22  import com.google.common.flogger.FluentLogger;
23  
24  public enum IgnoreWarnFail {
25      IGNORE, WARN, FAIL;
26  
27      private static final FluentLogger LOG = FluentLogger.forEnclosingClass();
28  
29      public static IgnoreWarnFail forString(final String value) {
30          checkNotNull(value, "the value can not be null");
31          return Enum.valueOf(IgnoreWarnFail.class, value.toUpperCase(Locale.getDefault()));
32      }
33  
34      /**
35       * Ensure that a given element exists. If it does not exist, react based on the {@link IgnoreWarnFail} attribute:
36       * <ul>
37       *     <li>IGNORE - do nothing</li>
38       *     <li>WARN - warn that an element does not exist</li>
39       *     <li>FAIL - throw an exception</li>
40       * </ul>
41       *
42       * @param check Should be true
43       * @param iwf    What to do
44       * @return True if the thing exists, false otherwise
45       */
46      public static boolean checkIgnoreWarnFailState(final boolean check, final IgnoreWarnFail iwf,
47          final Supplier<String> checkMessage, final Supplier<String> errorMessage) {
48  
49          if (check) {
50              LOG.atFine().log(checkMessage.get());
51              return true;
52          }
53  
54          switch (iwf) {
55              case IGNORE:
56                  LOG.atFine().log(errorMessage.get());
57                  break;
58              case WARN:
59                  LOG.atWarning().log(errorMessage.get());
60                  break;
61              case FAIL:
62                  throw new IllegalStateException(errorMessage.get());
63              default:
64                  throw new IllegalStateException("Unknown state: " + iwf);
65          }
66  
67          return false;
68      }
69  }