/*
 * Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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 org.hawkular.metrics.api.jaxrs.influx.write.validation;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Predicates.or;

import java.math.BigDecimal;
import java.util.List;
import java.util.ListIterator;

import org.hawkular.metrics.api.jaxrs.influx.InfluxObject;

import com.google.common.base.Predicate;

/**
 * @author Thomas Segismont
 */
public class DataTypesRule implements InfluxObjectValidationRule {
    private static final Predicate<Object> IS_INTEGRAL = //
    and( //
        instanceOf(Number.class), //
        not( //
            or( //
                instanceOf(BigDecimal.class), //
                instanceOf(Double.class), //
                instanceOf(Float.class) //
            ) //
        ) //
    );

    @Override
    public void checkInfluxObject(InfluxObject influxObject) throws InvalidObjectException {
        List<List<?>> points = influxObject.getPoints();
        if (points == null) {
            return;
        }
        List<String> columns = influxObject.getColumns();
        for (List<?> point : points) {
            if (point.size() < columns.size()) {
                throw new InvalidObjectException("Object has not enough data in point to match columns");
            }
            if (columns.size() == 1) {
                if (!(point.get(0) instanceof Number)) {
                    throw new InvalidObjectException("Point 'value' is not numerical");
                }
            } else {
                int valueIndex = columns.indexOf("value");
                for (ListIterator<?> dataIterator = point.listIterator(); dataIterator.hasNext();) {
                    Object data = dataIterator.next();
                    if (valueIndex == dataIterator.nextIndex() - 1) {
                        if (!(data instanceof Number)) {
                            throw new InvalidObjectException("Point 'value' is not numerical");
                        }
                    } else {
                        if (!IS_INTEGRAL.apply(data)) {
                            throw new InvalidObjectException("Point 'time' is not integral");
                        }
                    }
                }
            }
        }
    }
}

@@ -752,301 +752,22 @@
 ort 
-static com.google.common.base.Predicates.and;%0Aimport static com.google.common.base.Predicates.instanceOf;%0Aimport static com.google.common.base.Predicates.not;%0Aimport static com.google.common.base.Predicates.or;%0A%0Aimport java.math.BigDecimal;%0Aimport java.util.List;%0Aimport java.util.ListIterator
+java.util.List
 ;%0A%0Ai
@@ -829,50 +829,8 @@
 t;%0A%0A
-import com.google.common.base.Predicate;%0A%0A
 /**%0A
@@ -931,326 +931,8 @@
 le %7B
-%0A    private static final Predicate%3CObject%3E IS_INTEGRAL = //%0A    and( //%0A        instanceOf(Number.class), //%0A        not( //%0A            or( //%0A                instanceOf(BigDecimal.class), //%0A                instanceOf(Double.class), //%0A                instanceOf(Float.class) //%0A            ) //%0A        ) //%0A    );
 %0A%0A  
@@ -1435,512 +1435,36 @@
     
-if (columns.size() == 1) %7B%0A                if (!(point.get(0) instanceof Number)) %7B%0A                    throw new InvalidObjectException(%22Point 'value' is not numerical%22);%0A                %7D%0A            %7D else %7B%0A                int valueIndex = columns.indexOf(%22value%22);%0A                for (ListIterator%3C?%3E dataIterator = point.listIterator(); dataIterator.hasNext();) %7B%0A                    Object data = dataIterator.next();%0A                    if (valueIndex == dataIterator.nextIndex() - 1) %7B%0A        
+for (Object data : point) %7B%0A
     
@@ -1504,32 +1504,24 @@
  Number)) %7B%0A
-        
             
@@ -1565,295 +1565,36 @@
 ion(
-%22Point 'value' is not numerical%22);%0A                        %7D%0A                    %7D else %7B%0A                        if (!IS_INTEGRAL.apply(data)) %7B%0A                            throw new InvalidObjectException(%22Point 'time' is not integral%22);%0A                        %7D%0A                    %7D
+data + %22 is not numerical%22);
 %0A   

Simplify data checking code