All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.n3r.eql.matrix.func.RangeFunction Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
package org.n3r.eql.matrix.func;

import com.google.common.collect.Lists;
import org.n3r.eql.matrix.MatrixTableFieldValue;

import java.util.List;

public class RangeFunction extends SingleFieldBaseFunction {
    private List> ranges = Lists.newArrayList();


    @Override
    public String apply(MatrixTableFieldValue... fieldValues) {
        long value = Long.parseLong(find(fieldValues));

        return findInRanges(value);
    }

    private String findInRanges(long value) {
        for (Tuple tuple : ranges) {
            if (value >= tuple._1 && value < tuple._2) return tuple._3;
        }

        return "";
    }

    @Override
    public void configFunctionParameters(String... realFuncParams) {
        if (realFuncParams.length == 0) {
            throw new RuntimeException("range function need at least one param");
        }

        for (String rangeStr : realFuncParams) {
            int colonPos = rangeStr.indexOf(':');
            String rangeKey = rangeStr.substring(0, colonPos);
            String rangeVal = rangeStr.substring(colonPos + 1);

            int tildePos = rangeKey.indexOf('~');
            String from = rangeKey.substring(0, tildePos);
            String to = tildePos < rangeKey.length() - 1 ? rangeKey.substring(tildePos + 1) : "";
            long fromLong = Long.MIN_VALUE;
            long toLong = Long.MAX_VALUE;
            if (!from.equals("")) {
                fromLong = Long.parseLong(from);
            }
            if (!to.equals("")) {
                toLong = Long.parseLong(to);
            }

            ranges.add(Tuple.make(fromLong, toLong, rangeVal));
        }
    }

    private static class LongRange {
    }

    private static class Tuple {
        public T1 _1;
        public T2 _2;
        public T3 _3;

        public Tuple(T1 t1, T2 t2, T3 t3) {
            _1 = t1;
            _2 = t2;
            _3 = t3;
        }

        public static  Tuple make(T1 t1, T2 t2, T3 t3) {
            return new Tuple(t1, t2, t3);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy