package com.energy.manage.service.util;

import com.energy.manage.common.constant.Constants;
import org.springframework.util.StringUtils;

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

/**
 * @author lwz
 * @createTime 2020-07-31 10:44
 * @desc
 */
public class SplitUtil {

  /**
   * 直接类型 数据split工具
   *
   * @param data
   * @param <T>
   * @return
   */
  public static <T> List split2List(String data, Class<T> clazz) {
    return split2List(data, Constants.GLOBAL_SEGMENTATION_SYMBOLS, clazz);
  }


  public static <T> List split2List(String data, String segmentation, Class<T> clazz) {
    if (StringUtils.isEmpty(data)) {
      return Collections.EMPTY_LIST;
    }
    if (StringUtils.isEmpty(segmentation)) {
      return Collections.EMPTY_LIST;
    }
    String[] arr = data.split(segmentation);
    if (null == arr || arr.length == 0) {
      return Collections.EMPTY_LIST;
    }
    List list = new ArrayList<>(arr.length);
    for (String item : arr) {
      if (clazz.getName().equals(Long.class.getName())) {
        list.add(Long.parseLong(item));
        continue;
      } else if (clazz.getName().equals(Integer.class.getName())) {
        list.add(Integer.parseInt(item));
        continue;
      } else if (clazz.getName().equals(String.class.getName())) {
        list.add(item);
        continue;
      } else {
        list.add(item);
        continue;
      }
    }
    return list;
  }


  public static String[] split(String data) {
    if (StringUtils.isEmpty(data)) {
      return null;
    }
    String[] arr = data.split(Constants.GLOBAL_SEGMENTATION_SYMBOLS);
    return arr;
  }

  public static String[] split(String data, String segmentation) {
    if (StringUtils.isEmpty(data)) {
      return null;
    }
    if (StringUtils.isEmpty(segmentation)) {
      return null;
    }
    String[] arr = data.split(segmentation);
    return arr;
  }
}
