Guava Stopwatch&Functions

    public static void main(String[] args) throws InterruptedException {
        process("1212121");
    }



    private static void process(String orderNo) throws InterruptedException {
        System.out.printf("start process the order %s\n",orderNo);
        Stopwatch stopwatch=Stopwatch.createStarted();
        TimeUnit.SECONDS.sleep(1);
        System.out.printf("the order %s process succcessful and elapsed %d\n",orderNo,stopwatch.stop().elapsed(TimeUnit.NANOSECONDS));
        //the order 1212121 process succcessful and elapsed 1005297400
    }


-----------------------------------------------------------------------
 public static void main(String[] args) {


        Function<String, Integer> function = new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                Preconditions.checkNotNull(s,"not be null.");
                return s.length();
            }
        };
        System.out.println(function.apply("hello"));//5
        process("hello",new Handler.LengthDoubleHandler());//10


        Function<String, Double> compose = Functions.compose(new Function<Integer, Double>() {
            @Override
            public Double apply(Integer integer) {
                return integer * 1.0D;
            }
        }, new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return s.length();
            }
        });


        System.out.println(compose.apply("hello"));//5.0



    }

    interface  Handler<IN,OUT>{
        OUT handle(IN input);

        class LengthDoubleHandler implements Handler<String,Integer>{

            @Override
            public Integer handle(String input) {
                return input.length()*2;
            }
        }
    }


    private static void process(String text,Handler<String,Integer> handler){
        System.out.println(handler.handle(text));
    }


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值