/*
 * Copyright (C) 2017 Republic Wireless
 *
 * 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 com.rw.legion.columncheck;

import com.google.gson.JsonObject;

/**
 * A column checker that validates a string is a valid float. The optional
 * property "floatType" in the objective JSON specifies what type of float to
 * check it against - float or double. Defaults to double.
 */

public class FloatChecker implements ColumnChecker {
    private String floatType;
    
    public FloatChecker(JsonObject json) {
        if (! (json.has("floatType"))) {
            floatType = "double";
        } else {
            floatType = json.get("floatType").getAsString();
        }
    }
    
    public boolean validates(String str) {
        try {
            if (floatType.equals("float")) Float.parseFloat(str);
            if (floatType.equals("double")) Double.parseDouble(str);
            
            return true;
        } catch(NumberFormatException e) {
            return false;
        }
    }
}

@@ -976,20 +976,183 @@
 atType;%0A
+%0A
     
+public class FloatOutOfBoundsException extends Exception %7B%0A        public FloatOutOfBoundsException(String message) %7B%0A            super(message);%0A        %7D%0A    %7D%0A
 %0A    pub
@@ -1460,16 +1460,48 @@
 float%22))
+ %7B%0A                Float value =
  Float.p
@@ -1532,64 +1532,476 @@
     
-if (floatType.equals(%22double%22)) Double.parseDouble(str);
+    if (Float.MAX_VALUE %3C value %7C%7C Float.MIN_VALUE %3E value) %7B%0A                    throw new FloatOutOfBoundsException(%22Float exceeded range.%22);%0A                %7D%0A            %7D%0A            if (floatType.equals(%22double%22)) %7B%0A                Double value = Double.parseDouble(str);%0A                if (Double.MAX_VALUE %3C value %7C%7C Double.MIN_VALUE %3E value) %7B%0A                    throw new FloatOutOfBoundsException(%22Double exceeded range.%22);%0A                %7D%0A            %7D
 %0A   
@@ -2073,16 +2073,44 @@
 ception 
+%7C FloatOutOfBoundsException 
 e) %7B%0A   

Update FloatChecker to fail validation if range exceeded