guava 基础工具类

2017-08-02  本文已影响0人  bitingwind

Optional类

目的: 多用于返回值,明确null的语义,使用或者避免null
关键点: You (and others) are far more likely to forget that other.method(a, b) could return a null value than you're likely to forget that a could be null when you're implementing other.method.

  1. or(default T) 用默认值代替null
  2. isPresent() 判断方法返回值, 函数式编程stream流的异常判断
    ArrayList<String> sList = new ArrayList<String>(16);
    sList.add(null);

        Optional<String> ops = Optional.fromNullable(sList.get(0));
        ops.isPresent();
        ops.of(null);
        String def = "无";
        System.out.println(ops.or(def));

Preconditions 类

应用场景: 检测方法参数或者运行方法时的状态的合理性
方式: 在不满足条件的情况下,抛出非受检异,抛出IllegalArgumentException
IllegalStateException,NPE 等,给出对应message
局限: 抛出均为包装后的非受检异常,无法捕获处理,程序会终止

  1. Preconditions.checkArgument()
  2. Preconditions.checkState()
  3. Preconditions.checkNotNull()
    private  void arguementCheck(int i, int j){
        Preconditions.checkArgument( i > j, "参数异常,期望参数: i > j:传入参数 i:%s < j:%s ",i,j);
    }
    @Test
    public void testArguement(){
        arguementCheck(2,5);
    }

Ordering 类(collect包)

应用场景: 获取多个最大最小值,多个比较,有序拷贝,反向,链式排序

  1. reverse()
  2. greatestOf()
  3. leastOf()
  4. List<> sortCopy()
  5. onResultOf(Function<T,R>)
private Ordering<People> nameOrder
            = Ordering.natural().onResultOf(new Function<People,Integer>(){
        @Override
        public Integer apply(People p){
            return p.height + p.weight;
        }
    });
    @Test
    public void testHeightWeightOrder(){
        List<People> pl = Lists.newArrayList(
                new People("ren",16,15),
                new People("zhang",15,17),
                new People("wang",17,13)
        );
        System.out.println(nameOrder.sortedCopy(pl));
        System.out.println(nameOrder.reverse().sortedCopy(pl));
        System.out.println(nameOrder.greatestOf(pl,2));
        System.out.println(nameOrder.leastOf(pl,2));
    }

MoreObject.ToStringHelper 类

应用场景: key value格式化 toString()函数

  1. 构造器 MoreObject.toStringHelper()
  2. add(String V value)
class People{
    String name;
    int height;
    int weight;
    public People(String name,int height,int weight){
        this.name = name;
        this.height = height;
        this.weight = weight;
    }
    public String toString(){
        return MoreObjects.toStringHelper(this)
                .add("名字",name)
                .add("身高",height)
                .add("体重",weight)
                .toString();
    }
}

Splitter类

应用场景: 条件分割,去除空格,去除空值

  1. on() 字符,字符串,Pattern
  2. trimResult()
  3. omitEmptyStrings()
  4. fixedLength()
  5. Iterable<> split()
  6. List<> splitToList()
    @Test
    public void testStringSplit(){
        print(Splitter
                .on("|")
                .split("cat filename   | grep '.*'   ||   wc -l|"));
        print(Splitter
                .on("|")
                .trimResults()
                .split("cat filename   || grep '.*'   |   wc -l|"));
        print(Splitter
                .on("|")
                .trimResults()
                .omitEmptyStrings()
                .split("cat filename   || grep '.*'   |   wc -l|"));
    }
    @Test
    public void testImmutable(){
        Splitter splitter = Splitter.on('/');
        splitter.trimResults(); // does nothing!
        // splitter = splitter.trimResults();
        print(splitter.split("wrong / wrong / wrong"));
    }
上一篇下一篇

猜你喜欢

热点阅读