001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper;
016
017import static com.google.common.base.Preconditions.checkArgument;
018
019import java.util.Locale;
020import java.util.function.Supplier;
021
022import com.google.common.flogger.FluentLogger;
023
024public enum IgnoreWarnFailCreate {
025    IGNORE, WARN, FAIL, CREATE;
026
027    private static final FluentLogger LOG = FluentLogger.forEnclosingClass();
028
029    public static IgnoreWarnFailCreate forString(final String value) {
030        checkArgument(value != null, "the value can not be null");
031        return Enum.valueOf(IgnoreWarnFailCreate.class, value.toUpperCase(Locale.getDefault()));
032    }
033
034    public static boolean checkIgnoreWarnFailCreateState(final boolean check, final IgnoreWarnFailCreate iwfc,
035        final Supplier<String> checkMessage, final Supplier<String> errorMessage) {
036
037        if (check) {
038            LOG.atFine().log(checkMessage.get());
039            return false;
040        }
041
042        switch (iwfc) {
043            case IGNORE:
044                return false;
045            case WARN:
046                LOG.atWarning().log(errorMessage.get());
047                return false;
048            case FAIL:
049                throw new IllegalStateException(errorMessage.get());
050            case CREATE:
051                LOG.atFine().log(errorMessage.get());
052                return true;
053            default:
054                throw new IllegalStateException("Unknown state: " + iwfc);
055        }
056    }
057}