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.checkNotNull;
18  import static java.lang.String.format;
19  
20  import org.basepom.mojo.propertyhelper.util.Log;
21  
22  import java.util.Locale;
23  
24  public enum IgnoreWarnFail {
25      IGNORE, WARN, FAIL;
26  
27      private static final Log LOG = Log.findLog();
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.ENGLISH));
32      }
33  
34      /**
35       * Reacts on a given thing existing or not existing.
36       * <p>
37       * 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
38       * exist.
39       * <p>
40       * Returns true if the thing should be create, false otherwise.
41       */
42      public static void checkState(final IgnoreWarnFail iwf, final boolean exists, final String thing) {
43          if (exists) {
44              return;
45          }
46  
47          switch (iwf) {
48              case IGNORE:
49                  return;
50              case WARN:
51                  LOG.warn("'%s' does not exist!", thing);
52                  break;
53              case FAIL:
54                  throw new IllegalStateException(format("'%s' does not exist!", thing));
55              default:
56                  throw new IllegalStateException("Unknown state: " + iwf);
57          }
58      }
59  }