package com.sifang.modules.sys.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ListUtils {

    public static List<String> extractList(Object val) {
        List<String> ret = new ArrayList<>();
        if (isArray(val)) {
            for (int i = 0, n = Array.getLength(val); i < n; i++) {
                Object o = Array.get(val, i);
                if (o != null) {
                    ret.add(o.toString());
                }
            }
        } else if (val instanceof List) {
            List list = (List) val;
            for (Object el : list) {
                if (el != null) {
                    ret.add(el.toString());
                }
            }
        } else if (val instanceof Set) {
            Set set = (Set) val;
            for (Object el : set) {
                if (el != null) {
                    ret.add(el.toString());
                }
            }
        }
        return ret;
    }

    public static boolean isArray(Object obj) {
        return obj != null && obj.getClass().isArray();
    }

}
