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

org.nutz.validate.impl.IntEnumValidator Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.validate.impl;

import java.util.ArrayList;
import java.util.List;

import org.nutz.castor.Castors;
import org.nutz.lang.Each;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.validate.NutValidateException;
import org.nutz.validate.NutValidator;

public class IntEnumValidator implements NutValidator {

    private int[] nbs;

    public IntEnumValidator(Object any) {
        // 拆掉字符串
        if (any instanceof CharSequence) {
            any = Strings.splitIgnoreBlank(any.toString());
        }
        // 全都变成数字
        final List list = new ArrayList();
        Lang.each(any, new Each() {
            public void invoke(int index, Object ele, int length) {
                int n = Castors.me().castTo(ele, Integer.class);
                list.add(n);
            }
        });
        // 整理成数组
        nbs = new int[list.size()];
        int i = 0;
        for (int n : list) {
            nbs[i++] = n;
        }
    }

    @Override
    public Object check(Object val) throws NutValidateException {
        if(null == val) {
            return val;
        }
        int v = Castors.me().castTo(val, Integer.class);
        for (int n : nbs) {
            if (n == v) {
                return v;
            }
        }
        throw new NutValidateException("IntOutOfEnum", nbs.toString(), val);
    }

    @Override
    public int order() {
        return 1000;
    }

}