package ink.wgink.gateway;

import java.util.function.*;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: LambdaTest
 * @Description:
 * @Author: wanggeng
 * @Date: 2021/4/16 10:05 上午
 * @Version: 1.0
 */
public class LambdaTest {

    public static class Dog {
        private String name = "单身狗";
        private int food = 10;

        public Dog() {
        }

        public Dog(String name) {
            this.name = name;
        }

        public static void bark(Dog dog) {
            System.out.println(dog + "叫了");
        }

        public int eat(int num) {
            System.out.println(this.name + "吃了" + num + "斤");
            this.food -= num;
            return this.food;
        }

        @Override
        public String toString() {
            return this.name;
        }
    }

    public static void main(String[] args) {
        Dog dog = new Dog();

        // 静态方法
        Consumer<Dog> consume = Dog::bark;
        consume.accept(dog);

        // 通过实例得到方法的引用
        Function<Integer, Integer> function = dog::eat;
        System.out.println("还剩下" + function.apply(2) + "斤");

        UnaryOperator<Integer> unaryOperator = dog::eat;
        IntUnaryOperator intUnaryOperator = dog::eat;

        System.out.println("还剩下" + intUnaryOperator.applyAsInt(2) + "斤");

        // 使用类名得到非静态方法的引用，输入两个值，输出一个值
        BiFunction<Dog, Integer, Integer> biFunction = Dog::eat;
        System.out.println("还剩下" + biFunction.apply(dog, 1) + "斤");

        // 得到没有构造参数的引用
        Supplier<Dog> supplierDog = Dog::new;
        System.out.println("还剩下" + supplierDog.get().eat(1) + "斤");

        // 得到一个参数的
        Function<String, Dog> functionDog = Dog::new;
        System.out.println("还剩下" + functionDog.apply("李四").eat(1) + "斤");
    }

}
