package com.sysu.admin.utils;

import com.google.common.collect.Sets;
import lombok.NonNull;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class CopyUtil {

    public static String[] notNullNames(@NonNull Object source) {
        final BeanWrapper beanWrapper = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = beanWrapper.getPropertyDescriptors();
        Set<String> noValuePropertySet = Sets.newHashSet();
        Arrays.stream(pds).forEach(pd -> {
            Object propertyValue = beanWrapper.getPropertyValue(pd.getName());
            if (ObjectUtils.isNotEmpty(propertyValue)) {
                noValuePropertySet.add(pd.getName());
            }
        });
        String[] result = new String[noValuePropertySet.size()];
        return noValuePropertySet.toArray(result);
    }


    public static Long[] toLongArray(String[] a){
        Long[] ls = new Long[a.length];
        for(int i=0;i<a.length;i++){
            ls[i] = Long.valueOf(a[i]);
        }
        return ls;
    }

    public static Long[] clear(Long[] a){
        Set<Long> set = new HashSet<>();
        for(Long l : a){
            set.add(l);
        }
        return set.toArray(new Long[0]);
    }
}
