package com.elab.test.utils;

import com.google.common.base.*;
import com.google.common.collect.*;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;

import java.util.*;

/**
 * 工具类研究
 *
 * @author ： liukx
 * @time ： 2020/4/9 - 19:02
 */
public class GuavaUtilsCase {


    @Test
    public void ListTest() {
        // 普通集合
        ArrayList<Object> objects = Lists.newArrayList("a", "b", "c", "d", "e", "f");
        // 数据分片
        List<List<Object>> partition = Lists.partition(objects, 2);
        System.out.println("");
        // 取交集
//        Sets.intersection(objects,objects)
        String str = "1-2-3-4-5-6";
        List<String> list = Splitter.on("-").splitToList(str);
    }

    /**
     * 当我们在统计一个字符串中每个单词出现的次数时，通常的做法是分割字符串，
     * 遍历字符串，然后放到一个map里面，来进行统计，Guava中提供了类似功能的集合，Multiset
     */
    @Test
    public void strCount() {
        String strWorld = "wer dffd ddsa dfd dreg de dr ce ghrt cf gt ser tg ghrt cf gt " +
                "ser tg gt kldf dfg vcd fg gt ls lser dfr wer dffd ddsa dfd dreg de dr " +
                "ce ghrt cf gt ser tg gt kldf dfg vcd fg gt ls lser dfr";
        List<String> stringList = Splitter.on(" ")
                .trimResults()
                .splitToList(strWorld);//把字符串转换为集合
        HashMultiset<String> multisets = HashMultiset.create();//创建一个Multiset集合
        multisets.addAll(stringList);
        Iterator<String> iterator = multisets.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next + " count: " + multisets.count(next));
        }
    }

    @Test
    public void MapTest() {
        /**
         * 允许一个key对应多个值，相当于Map<String,List<String>>
         */
        ArrayListMultimap<Object, Object> objectObjectArrayListMultimap = ArrayListMultimap.create();
        objectObjectArrayListMultimap.put("aaa", "a");
        objectObjectArrayListMultimap.put("aaa", "b");
        objectObjectArrayListMultimap.put("aaa", "c");
        System.out.println("===");
        List<Object> aaa = objectObjectArrayListMultimap.get("aaa");

        HashBiMap<String, String> objectObjectHashBiMap = HashBiMap.create();
        objectObjectHashBiMap.put("a", "b");
        objectObjectHashBiMap.put("c", "d");
        objectObjectHashBiMap.put("e", "e");
        System.out.println("====");

        Table<String, String, Integer> tables = HashBasedTable.create();
        tables.put("l", "k", 1);

        Map<String, Integer> map = Maps.newHashMap();
        map.put("xiaoming", 12);
        map.put("xiaohong", 13);
        String result = Joiner.on(",").withKeyValueSeparator("=").join(map);

        String str = "xiaoming=11,xiaohong=23";
        Map<String, String> strMap = Splitter.on(",").withKeyValueSeparator("=").split(str);

        Preconditions.checkArgument(true, "asdfasdfd");
    }

    @Test
    public void strTest() {
        List<String> strings = Arrays.asList("A", "B", "C", "D");
        Joiner joiner1 = Joiner.on(",").skipNulls();
        String join = Joiner.on(",").join(strings);
        System.out.println(join);
    }


    @Test
    public void checkTest() {
        Preconditions.checkArgument(true, "条件不能为true");
    }

    @Test
    public void sortTest() {
        // 按照自然排序
        List<Integer> integerList = Arrays.asList(1, 2, 3, 45);
        Ordering.natural().nullsFirst().onResultOf(new Function<Object, Comparable>() {
            @Override
            public @Nullable Comparable apply(@Nullable Object input) {
                return null;
            }
        });
    }

    @Test
    public void functionTest() {
        Function<Object, Object> objectObjectFunction = Functions.forMap(null);
        boolean b1 = FluentIterable.of("a", "b").allMatch((o) -> {
            return false;
        });
        boolean b = FluentIterable.of().allMatch(new Predicate() {
            @Override
            public boolean apply(Object input) {
                return false;
            }
        });
    }

}
