// Copyright 2018 takahashikzn
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jp.root42.indolently;

import java.math.BigDecimal;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;


/**
 * @author takahashikzn
 */
public final class Numeric {

    /** non private for subtyping. */
    protected Numeric() {}

    public static int asInt(final String s) { return Integer.valueOf(s); }

    public static long asLong(final String s) { return Long.valueOf(s); }

    public static short asShort(final String s) { return Short.valueOf(s); }

    public static byte asByte(final String s) { return Byte.valueOf(s); }

    public static float asFloat(final String s) { return Float.valueOf(s); }

    public static double asDouble(final String s) { return Double.valueOf(s); }

    public static boolean asBool(final String s) { return Boolean.valueOf(s); }

    // public static char asChar(final String s) { return s.charAt(0); }

    public static BigDecimal decimal(final String s) { return new BigDecimal(s); }

    public static BigDecimal decimal(final long l) { return BigDecimal.valueOf(l); }

    public static BigDecimal decimal(final double d) { return BigDecimal.valueOf(d); }

    public static Function<String, Integer> asInt() { return Numeric::asInt; }

    public static ToIntFunction<String> toInt() { return Numeric::asInt; }

    public static Function<String, Long> asLong() { return Numeric::asLong; }

    public static ToLongFunction<String> toLong() { return Numeric::asLong; }

    public static Function<String, Short> asShort() { return Numeric::asShort; }

    public static Function<String, Byte> asByte() { return Numeric::asByte; }

    public static Function<String, Float> asFloat() { return Numeric::asFloat; }

    public static Function<String, Double> asDouble() { return Numeric::asDouble; }

    public static ToDoubleFunction<String> toDouble() { return Numeric::asDouble; }

    public static Function<String, Boolean> asBool() { return Numeric::asBool; }

    public static Predicate<String> toBool() { return Numeric::asBool; }

    // public static Function<String, Character> toChar() { return x -> asChar(x); }

    public static Function<String, BigDecimal> decimal() { return x -> decimal(x); }
}

@@ -638,16 +638,43 @@
 ecimal;%0A
+import java.util.Optional;%0A
 import j
@@ -693,32 +693,32 @@
 ction.Function;%0A
-
 import java.util
@@ -995,16 +995,59 @@
 c() %7B%7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     publ
@@ -1106,32 +1106,75 @@
 .valueOf(s); %7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     public stati
@@ -1224,32 +1224,75 @@
 .valueOf(s); %7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     public stati
@@ -1345,32 +1345,75 @@
 .valueOf(s); %7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     public stati
@@ -1463,32 +1463,75 @@
 .valueOf(s); %7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     public stati
@@ -1584,32 +1584,75 @@
 .valueOf(s); %7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     public stati
@@ -1708,32 +1708,75 @@
 .valueOf(s); %7D%0A%0A
+    @SuppressWarnings(%22UnnecessaryBoxing%22)%0A
     public stati
@@ -1836,24 +1836,1033 @@
 ueOf(s); %7D%0A%0A
+    private static %3CT%3E Optional%3CT%3E parseX(final String s, final Function%3CString, T%3E f) %7B%0A        try %7B%0A            return (s == null) ? Optional.empty() : Optional.of(f.apply(s));%0A        %7D catch (final IllegalArgumentException e) %7B%0A            return Optional.empty();%0A        %7D%0A    %7D%0A%0A    public static Optional%3CInteger%3E parseInt(final String s) %7B return parseX(s, Integer::parseInt); %7D%0A%0A    public static Optional%3CLong%3E parseLong(final String s) %7B return parseX(s, Long::parseLong); %7D%0A%0A    public static Optional%3CShort%3E parseShort(final String s) %7B return parseX(s, Short::parseShort); %7D%0A%0A    public static Optional%3CByte%3E parseByte(final String s) %7B return parseX(s, Byte::parseByte); %7D%0A%0A    public static Optional%3CFloat%3E parseFloat(final String s) %7B return parseX(s, Float::parseFloat); %7D%0A%0A    public static Optional%3CDouble%3E parseDouble(final String s) %7B return parseX(s, Double::parseDouble); %7D%0A%0A    public static Optional%3CBoolean%3E parseBool(final String s) %7B return parseX(s, Boolean::parseBoolean); %7D%0A%0A
     // publi

add parseInt and companions