/**
 * Copyright (c)  2014-2020 Gaoxiaobang, Inc.
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information of Gaoxiaobang,
 * Inc. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with Gaoxiaobang.
 */
package com.energy.manage.common.util;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.lang3.ArrayUtils;

import java.util.*;

public class CollectionTools {

  public static boolean isEmpty(final Object[] array) {
    return ArrayUtils.isEmpty(array);
  }

  public static boolean isEmpty(Collection<?> coll) {
    return CollectionUtils.isEmpty(coll);
  }

  public static boolean isNotEmpty(Collection<?> coll) {
    return !CollectionUtils.isEmpty(coll);
  }

  public static <T> void sort(List<T> list, String property, boolean asc) {
    Comparator<?> comparator = ComparableComparator.getInstance();
    comparator = ComparatorUtils.nullLowComparator(comparator);
    if (!asc) {
      comparator = ComparatorUtils.reversedComparator(comparator);
    }
    Collections.sort(list, new BeanComparator(property, comparator));
  }

  public static <T> List<T> page(List<T> list, Integer curPage, Integer pageSize) {
    if (curPage < 1 || pageSize <= 0)
      return null;
    int startIndex = (curPage - 1) * pageSize;
    int endIndex = startIndex + pageSize;
    if (startIndex <= list.size() - 1) {
      return list.subList(startIndex, endIndex > list.size() ? list.size() : endIndex);
    }
    return null;
  }


  /**
   * 数据去除重复元素
   * @param list
   */
  public static void removeDuplicate(List list) {
    HashSet h = new HashSet(list);
    list.clear();
    list.addAll(h);
  }


  /**
   * Returns the size of the specified collection or {@code 0} if the collection is {@code null}.
   *
   * @param c the collection to check
   * @return the size of the specified collection or {@code 0} if the collection is {@code null}.
   * @since 1.2
   */
  public static int size(Collection c) {
    return c != null ? c.size() : 0;
  }

  /**
   * Returns the size of the specified map or {@code 0} if the map is {@code null}.
   *
   * @param m the map to check
   * @return the size of the specified map or {@code 0} if the map is {@code null}.
   * @since 1.2
   */
  public static int size(Map m) {
    return m != null ? m.size() : 0;
  }


  public static <E> List<E> asList(E... elements) {
    if (elements == null || elements.length == 0) {
      return Collections.emptyList();
    }

    // Integer overflow does not occur when a large array is passed in because the list array already exists
    return Arrays.asList(elements);
  }

  public static <E> Set<E> asSet(E... elements) {
    if (elements == null || elements.length == 0) {
      return Collections.emptySet();
    }

    if (elements.length == 1) {
      return Collections.singleton(elements[0]);
    }

    LinkedHashSet<E> set = new LinkedHashSet<E>(elements.length * 4 / 3 + 1);
    Collections.addAll(set, elements);
    return set;
  }


}
