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));
}
Guava Stopwatch&Functions
最新推荐文章于 2024-08-04 11:07:52 发布