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.beans;
16  
17  import static com.google.common.base.Preconditions.checkArgument;
18  import static java.lang.String.format;
19  
20  import org.basepom.mojo.propertyhelper.util.Log;
21  
22  import java.util.Locale;
23  import javax.annotation.Nonnull;
24  
25  public enum IgnoreWarnFailCreate {
26      IGNORE, WARN, FAIL, CREATE;
27  
28      private static final Log LOG = Log.findLog();
29  
30      public static IgnoreWarnFailCreate forString(final String value) {
31          checkArgument(value != null, "the value can not be null");
32          return Enum.valueOf(IgnoreWarnFailCreate.class, value.toUpperCase(Locale.ENGLISH));
33      }
34  
35      /**
36       * Reacts on a given thing existing or not existing.
37       * <p>
38       * IGNORE: Do nothing. WARN: Display a warning message if the thing does not exist, otherwise do nothing. FAIL: Throws an exception if the thing does not
39       * exist. CREATE: Suggest creation of the thing.
40       * <p>
41       * Returns true if the thing should be create, false otherwise.
42       */
43      public static boolean checkState(@Nonnull final IgnoreWarnFailCreate iwfc, final boolean exists, final String thing) {
44          if (exists) {
45              return false;
46          }
47  
48          switch (iwfc) {
49              case IGNORE:
50                  return false;
51              case WARN:
52                  LOG.warn("'%s' does not exist!", thing);
53                  return false;
54              case FAIL:
55                  throw new IllegalStateException(format("'%s' does not exist!", thing));
56              case CREATE:
57                  LOG.debug("'%s' does not exist, suggesting creation.", thing);
58                  return true;
59              default:
60                  throw new IllegalStateException("Unknown state: " + iwfc);
61          }
62      }
63  }