package de.deverado.framework.core;

import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * This makes reading config parsing logic so much easier.
 */
@ParametersAreNonnullByDefault
public class ParsingUtil {

    private static final Logger log = LoggerFactory.getLogger(ParsingUtil.class);

    /**
     * Accepts true,>0 as true values.
     *
     */
    public static boolean parseAsBoolean(@Nullable String s) {
        boolean retval = false;
        if (!Strings.isNullOrEmpty(s)) {
            s = s.trim();
            if (Strings2.equalsIgnoreCaseEnglishLowercasing("yes", s)) {
                retval = true;
            } else {
                try {
                    retval = Integer.parseInt(s) > 0;
                } catch (NumberFormatException e) {
                    retval = Boolean.parseBoolean(s);
                }
            }
        }
        return retval;
    }

    public static int parseAsInt(@Nullable String s, @Nullable Integer defaultVal) {
        try {
            return Integer.parseInt(s.trim());
        } catch (RuntimeException nfe) {
            if (defaultVal == null) {
                throw nfe;
            }
            return defaultVal;
        }
    }

    public static int parseAsEnglishInt(@Nullable String s, @Nullable Integer defaultVal) {
        try {
            return parseAsEnglishInt(s);
        } catch (NumberFormatException nfe) {
            if (defaultVal == null) {
                throw nfe;
            }
            return defaultVal;
        }
    }

    public static int parseAsEnglishInt(String s) {

        s = Strings.nullToEmpty(s).replace(",", "").trim();
        if (StringUtils.isBlank(s)) {
            throw new NumberFormatException("null");
        }
        return Integer.parseInt(s);
    }

    public static Number parseAsEnglishNumber(String s)
            throws NumberFormatException {
        try {
            return NumberFormat.getInstance(Locale.ENGLISH).parse(s);
        } catch (ParseException e) {
            throw new NumberFormatException(e.getMessage());
        }
    }

    public static Date dateIn(long distance, TimeUnit unit) {
        Date now = new Date();
        return new Date(now.getTime() + unit.toMillis(distance));
    }

}

@@ -1217,32 +1217,1044 @@
  retval;%0A    %7D%0A%0A
+    public static Long parseAsLong(@Nullable String s, @Nullable Long defaultVal) %7B%0A        try %7B%0A            return Long.parseLong(s.trim());%0A        %7D catch (RuntimeException nfe) %7B%0A            if (defaultVal == null) %7B%0A                throw nfe;%0A            %7D%0A            return defaultVal;%0A        %7D%0A    %7D%0A%0A    public static Long parseAsEnglishLong(@Nullable String s, @Nullable Long defaultVal) %7B%0A        try %7B%0A            return parseAsEnglishLong(s);%0A        %7D catch (NumberFormatException nfe) %7B%0A            if (defaultVal == null) %7B%0A                throw nfe;%0A            %7D%0A            return defaultVal;%0A        %7D%0A    %7D%0A%0A    public static long parseAsEnglishLong(String s) %7B%0A%0A        s = makeEnglishLongOrIntParseable(s);%0A        if (StringUtils.isBlank(s)) %7B%0A            throw new NumberFormatException(%22null%22);%0A        %7D%0A        return Long.parseLong(s);%0A    %7D%0A%0A    private static String makeEnglishLongOrIntParseable(String s) %7B%0A        return Strings.nullToEmpty(s).replace(%22,%22, %22%22).trim();%0A    %7D%0A%0A
     public stati
@@ -2314,32 +2314,32 @@
 r defaultVal) %7B%0A
-
         try %7B%0A  
@@ -2940,53 +2940,39 @@
 s = 
-Strings.nullToEmpty(s).replace(%22,%22, %22%22).trim(
+makeEnglishLongOrIntParseable(s
 );%0A 

Add some parsing helpers.