/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.validate;

import com.google.common.collect.ImmutableList;

/**
 * A constraint that is applicable to numeric fields only.
 *
 * @param <V>
 *         a type of values that this constraint is applicable to.
 */
public abstract class NumericFieldConstraint<V extends Number & Comparable, T>
        extends FieldValueConstraint<V, T> {

    NumericFieldConstraint(T optionValue) {
        super(optionValue);
    }

    @Override
    public ImmutableList<ConstraintViolation> check(FieldValue<V> value) {
        if (!satisfies(value)) {
            return constraintViolated(value);
        }
        return ImmutableList.of();
    }

    /** Returns a number of type V based on its string representation. */
    @SuppressWarnings("unchecked") // Safe since double is both a Number and Comparable.
    static <V extends Number & Comparable> V fromOption(String numericValue) {
        Double doubleValue = Double.parseDouble(numericValue);
        V result = (V) doubleValue;
        return result;
    }

    /**
     * Whether the actual value of the field satisfies this constraint.
     *
     * @param value
     *         a value of the field.
     * @return {@code true} the specified does not satisfy this constraint
     */
    abstract boolean satisfies(FieldValue<V> value);

    /** Violations that should be produced if this constraint is not satisfied. */
    abstract ImmutableList<ConstraintViolation> constraintViolated(FieldValue<V> value);
}

@@ -1083,16 +1083,147 @@
 eList;%0A%0A
+import java.text.NumberFormat;%0Aimport java.text.ParseException;%0A%0Aimport static io.spine.util.Exceptions.newIllegalStateException;%0A%0A
 /**%0A * A
@@ -1365,16 +1365,162 @@
 to.%0A */%0A
+@SuppressWarnings(%22TypeParameterUnusedInFormals%22)%0A// Numbers are not comparable, so having a V that is both a Number and Comparable is necessary.%0A
 public a
@@ -2005,24 +2005,417 @@
 ntation. */%0A
+    static %3CV extends Number & Comparable%3E V fromOption(String numericValue) %7B%0A        try %7B%0A            V result = fromString(numericValue);%0A            return result;%0A        %7D catch (ParseException e) %7B%0A            String errorMessage = %22Could not extract numeric value from an option. Value: %25s%22;%0A            throw newIllegalStateException(e, errorMessage, numericValue);%0A        %7D%0A    %7D%0A%0A
     @Suppres
@@ -2491,24 +2491,32 @@
 arable.%0A    
+private 
 static %3CV ex
@@ -2544,38 +2544,38 @@
 able%3E V from
-Option
+String
 (String 
 numericValue
@@ -2566,84 +2566,350 @@
 ing 
-n
+input)%0A            throws ParseException %7B%0A        N
 um
+b
 er
-icValue) %7B%0A        Double doubleValue = Double.parseDouble(
+Format instance = NumberFormat.getNumberInstance();%0A        Number number = instance.parse(input);%0A        if (fitsIntoInt(number) && whole(number)) %7B%0A            @SuppressWarnings(%22WrapperTypeMayBePrimitive%22) // Primitives are uncastable.%0A            Integer result = 
 num
+b
 er
-ic
+.int
 Value
+(
 );%0A 
@@ -2919,57 +2919,288 @@
     
+   
-V result = (V) doubleValue
+ return (V) result;%0A        %7D%0A        return (V) number;%0A    %7D%0A%0A    private static boolean fitsIntoInt(Number number) %7B%0A        return number.longValue() %3C= Integer.MAX_VALUE
 ;%0A    
+%7D%0A%0A
     
-return result
+private static boolean whole(Number number) %7B%0A        return number.doubleValue() %25 1 == 0
 ;%0A  

Fix the way strings are resolved to number types.