美文网首页
Guava之Function

Guava之Function

作者: 神豪VS勇士赢 | 来源:发表于2020-08-24 01:15 被阅读0次
public class FunctionExample {
    public static void main(String[] args) throws IOException {
        Function<String, Integer> function = new Function<String, Integer>() {

            @Nullable
            @Override
            public Integer apply(@Nullable String input) {
                Preconditions.checkNotNull(input, "The input Stream should not be null.");
                return input.length();
            }
        };

        System.out.println(function.apply("Hello"));

        process("Hello", new Handler.LengthDoubleHandler());
        System.out.println(Functions.toStringFunction().apply(new ServerSocket(8888)));


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

            }
        }, new Function<String, Integer>() {

            @Nullable
            @Override
            public Integer apply(@Nullable String input) {
                return input.length();
            }
        });

        System.out.println(compose.apply("Hello"));

    }

    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之Function

      本文链接:https://www.haomeiwen.com/subject/rprgjktx.html