安卓高频数据过滤

作用如题,常用于自定义视图触屏事件无效记录点的删选或者传感器监听的无效数据过滤,当然也可以应用到其他场景,功能比较简单,做个记录以备之后用.



public class MathDataFilter {
    double oldValue[];
    double newValue[];
    private double mValidSpace = 0;
    private boolean needAllPass = false;

    public MathDataFilter(double validSpace) {
        this(validSpace, false);
    }

    /**
     * @param validSpace  数据判读间隔 大于等于
     * @param needAllPass 是否需要全部数据通过才通过过滤器
     */
    public MathDataFilter(double validSpace, boolean needAllPass) {
        this.mValidSpace = validSpace;
        this.needAllPass = needAllPass;
    }

    public void set(double... v) {
        if (v == null || v.length == 0 || (oldValue != null && v.length != oldValue.length)) {
            try {
                throw new Exception("MathDataFilter.set(double... values) input invalid value : " + (v == null ? "null" : v.toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.newValue = v;
        if (this.oldValue == null) {
            this.oldValue = v;
            for (int i = 0; i < v.length; i++) {
                v[i] = getValue(v[i], i);
            }
            if (mOnValidChangeListener != null) {
                mOnValidChangeListener.onChange(v);
            }
        } else if (mOnValidChangeListener != null) {
            if (isChange(this.newValue)) {
                for (int i = 0; i < v.length; i++) {
                    v[i] = getValue(v[i], i);
                }
                mOnValidChangeListener.onChange(v);
                this.oldValue = this.newValue;
            }
        }
    }

    /**
     * @param value 原始数据
     * @param index 数据下标
     * @return 修改返回结果显示格式 - 适用于接口回调模式模式
     */
    protected double getValue(double value, int index) {
        return value;
    }

    protected boolean isDataOk(double v1, double v2, int index) {
        return Math.abs(v1 - v2) >= mValidSpace;
    }

    private boolean isChange(double... value) {
        if (isArrayDifferent(value)) {
            return true;
        }
        return false;
    }

    private boolean isArrayDifferent(double... value) {
        if (needAllPass) {
            for (int i = 0; i < value.length; i++) {
                if (!isDataOk(value[i], this.oldValue[i], i)) {
                    return false;
                }
            }
            return true;
        }
        for (int i = 0; i < value.length; i++) {
            if (isDataOk(value[i], this.oldValue[i], i)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return 当前数据是否修改
     */
    public boolean isChange() {
        return isChange(this.newValue);
    }

    /**
     * @return 当前有效数据, 配合 isChange() 使用
     */
    public double[] getNewValue() {
        return newValue;
    }

    /**
     * @return 有效数据调试展示, 可更具具体应用场景删选修改去除
     */
    public String showNewValue() {
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < newValue.length; i++) {
            sb.append("   " + i + "-> ").append(getValue(newValue[i], i));
        }
        this.oldValue = this.newValue;
        return sb.toString();
    }

    OnValidChangeListener mOnValidChangeListener;

    public void setOnValidChangeListener(OnValidChangeListener listener) {
        this.mOnValidChangeListener = listener;
    }

    public interface OnValidChangeListener {
        void onChange(double... objects);
    }

}


其中我写了两种结果获取方式,一种是用接口回调(test1),一种是查询结果.具体使用可以根据具体应用场景选择.注释都写得很详细,有问题可以留言.



public static void main(String[] args) {
    final MathDataFilter mathCompare = new MathDataFilter(10);
    test1(mathCompare);
    System.out.println("\n===================================================\n");
    final MathDataFilter mathCompare2 = new MathDataFilter(10);
    test2(mathCompare2);
}

private static void test1(MathDataFilter mathCompare) {
    mathCompare.setOnValidChangeListener(new MathDataFilter.OnValidChangeListener() {
        @Override
        public void onChange(double... objects) {
            System.out.println("1->" + objects[0] + "    2->" + objects[1] + "    3->" + objects[2] + "    4->" + objects[3]);
        }
    });
    mathCompare.set(3.456, 5, 6.453, 67.345);
    mathCompare.set(345.6, 56, 45.3, 600.345);
    mathCompare.set(5.476, 5, 6.453, 55);
    mathCompare.set(5.478, 5, 6, 56);
    mathCompare.set(5.476, 5, 6.453, 57);
    mathCompare.set(342.6, 56, 45.3, 62.345);
    mathCompare.set(345.6, 56, 45.3, 62.345);
    mathCompare.set(5.476, 5, 6.453, 55);
    mathCompare.set(5.476, 6, 8, 55);
    mathCompare.set(5.476, 5, 6.453, 55);
}

private static void test2(MathDataFilter mathCompare) {
    tests(mathCompare, 3.456, 5, 6.453, 67.345);
    tests(mathCompare, 345.6, 56, 45.3, 600.345);
    tests(mathCompare, 5.476, 5, 6.453, 55);
    tests(mathCompare, 5.478, 5, 6, 56);
    tests(mathCompare, 5.476, 5, 6.453, 57);
    tests(mathCompare, 342.6, 56, 45.3, 62.345);
    tests(mathCompare, 345.6, 56, 45.3, 62.345);
    tests(mathCompare, 5.476, 5, 6.453, 55);
    tests(mathCompare, 5.476, 6, 8, 55);
    tests(mathCompare, 5.476, 5, 6.453, 55);
}

private static void tests(MathDataFilter mathCompare, double... v) {
    mathCompare.set(v);
    if (mathCompare.isChange()) {
        System.out.println(mathCompare.showNewValue());
    }
}



猜你喜欢

转载自blog.csdn.net/raoqian156/article/details/80570338