Javaプログラマはどのようにlambada式の花を使いますか?

20893 ワード

目次
  • 概要
  • 実現
  • エンディング
  • 概要
    このブログはlamda表現と三眼演算子シミュレーションif-elseIf-else形式を反面教材として説明しています。このような花のスタイルは死として娯楽だけに提供しています。皆さんの笑顔を見てください。  の最終的な効果は以下の通りです。
    cIf(cond1, () -> {				//    if
        //do action 1
    }).cElseIf(() -> cond2, () -> {		//  cond2       else if
        //do action 2
    }).cElseIf(cond3, () -> {			//  cond3        else if
        //do action 3
    }).cElse(() -> {					//    else
        //do action 4
    });
    
    実現する
      はコンストラクタの配合方法チェーンを採用して実現し、空の指針の検出を省略し、主な種類は以下の通りである。
    /**
     *          
     */
    class IfBuilder {
        /*        */
        private IfBuilder() {
        }
    
        /**
         * @param predicate         
         * @param action          
         * @return     cIf
         */
        public static IfBuilder cIf(boolean predicate, Runnable action) {
            return cIf(() -> predicate, action);                //         ,       cIf
        }
    
        /**
         * @param predicate        
         * @param action          
         * @return         true,             builder,         elseIf   else     builder
         */
        public static IfBuilder cIf(Supplier<Boolean> predicate, Runnable action) {
            return predicate.get()                  //     
                    ?
                    ((Supplier<IfBuilder>) () -> {  //       true,    action,              builder
                        action.run();
                        return new IfBuilder();
                    }).get()
                    :
                    new IfBuilder() {               //       false,    action,          elseIf   else     builder
                        /*           true       */
                        private boolean elseIfFlag = false;
    
                        @Override
                        public IfBuilder cElseIf(Supplier<Boolean> b, Runnable r) {
                            /*       true     r,     elseIfFlag    true,       builder;   ,       */
                            return b.get() ? ((Supplier<IfBuilder>) () -> {
                                r.run();
                                elseIfFlag = true;
                                return new IfBuilder();
                            }).get() : this;
                        }
    
                        @Override
                        public IfBuilder cElseIf(boolean b, Runnable r) {
                            return cElseIf(() -> b, r);
                        }
    
                        @Override
                        public void cElse(Runnable r) {
                            /*          elseIf       ,   r */
                            ((Supplier<IfBuilder>) () -> elseIfFlag ? this : ((Supplier<IfBuilder>) () -> {
                                r.run();
                                return this;
                            }).get()).get();
                        }
                    };
        }
    
        /*            else if,    */
        public IfBuilder cElseIf(Supplier<Boolean> b, Runnable r) {
            return this;
        }
    
        /*             else if,    */
        public IfBuilder cElseIf(boolean b, Runnable r) {
            return this;
        }
    
        /*    else,    */
        public void cElse(Runnable r) {
        }
    }
    

    例のコードは以下の通りであり、iとjは乱数で生成される。
    for (int x = 0; x < 10; x++) {
        int i = new Random().nextInt() % 4;
        int j = new Random().nextInt() % 4;
    
        cIf(i == 0 || j == 0, () -> {
            System.out.println(i + " " + j + " : i == 0 || j == 0");
        }).cElseIf(() -> i < j, () -> {
            System.out.println(i + " " + j + " : i < j");
        }).cElseIf(i > j, () -> {
            System.out.println(i + " " + j + " : i > j");
        }).cElse(() -> {
            System.out.println(i + " " + j + " : i = j");
        });
    }
    
    出力例:
    -3 -1 : i < j
    3 -3 : i > j
    2 -2 : i > j
    0 -1 : i == 0 || j == 0
    -1 -2 : i > j
    -1 -1 : i = j
    -1 -2 : i > j
    0 0 : i == 0 || j == 0
    2 1 : i > j
    2 1 : i > j
    
    締めくくりをつける
    遊びのコードが書いてありますので、書き終わったら自分でもツッコミを入れたいです。いくつかの高級な書き方を使っているのを見ていますが、全く問題が解決されていないだけでなく、コードの複雑さも大きいです。