Java通過Lambda表達(dá)式實現(xiàn)簡化代碼

java通過lambda表達(dá)式實現(xiàn)簡化代碼

之前,調(diào)用第3方服務(wù),每個方法都差不多“長”這樣, 寫起來啰嗦, 改起來麻煩, 還容易改漏。

public void authorizeroletouser(long userid, list<long> roleids) {
  try {
      power.authorizeroletouser(userid, roleids);
  } catch (motancustomexception ex) {
      if (ex.getcode().equals(msuserexception.notlogin().getcode()))
          throw userexception.notlogin();
      if (ex.getcode().equals(mspowerexception.havenopower().getcode()))
          throw powerexception.havenopower();
      throw ex;
  } catch (motanserviceexception ex) {
      cathelper.logeventservice("power-authorizeroletouser", "authorizeroletouser", ex.getstatus(),
              ex.geterrorcode(), ex.getmessage());
      throw ex;
  } catch (motanabstractexception ex) {
      cathelper.logeventservice("power-authorizeroletouser", "authorizeroletouser", ex.getstatus(),
              ex.geterrorcode(), ex.getmessage());
      throw ex;
  } catch (exception ex) {
      cathelper.logerror(ex);
      throw ex;
  }
}

我經(jīng)過學(xué)習(xí)和提取封裝, 將try ... catch ... catch .. 提取為公用, 得到這2個方法:

import java.util.function.supplier;
public static <t> t trycatch(supplier<t> supplier, string servicename, string methodname) {
  try {
      return supplier.get();
  } catch (motancustomexception ex) {
      if (ex.getcode().equals(msuserexception.notlogin().getcode()))
          throw userexception.notlogin();
      if (ex.getcode().equals(mspowerexception.havenopower().getcode()))
          throw powerexception.havenopower();
      throw ex;
  } catch (motanserviceexception ex) {
      cathelper.logeventservice(servicename + "-" + methodname, methodname, ex.getstatus(), ex.geterrorcode(),
              ex.getmessage());
      throw ex;
  } catch (motanabstractexception ex) {
      cathelper.logeventservice(servicename + "-" + methodname, methodname, ex.getstatus(), ex.geterrorcode(),
              ex.getmessage());
      throw ex;
  } catch (exception ex) {
      cathelper.logerror(ex);
      throw ex;
  }
}
public static void trycatch(runnable runnable, string servicename, string methodname) {
  trycatch(() -> {
      runnable.run();
      return null;
  }, servicename, methodname);
}

現(xiàn)在用起來是如此簡潔。像這種無返回值的:

public void authorizeroletouser(long userid, list<long> roleids) {
  trycatch(() -> { power.authorizeroletouser(userid, roleids); }, "power", "authorizeroletouser");
}

還有這種有返回值的:

public list<roledto> listrolebyuser(long userid) {
  return trycatch(() -> power.listrolebyuser(userid), "power", "listrolebyuser");
}

后來發(fā)現(xiàn)以上2個方法還不夠用, 原因是有一些方法會拋出 checked 異常, 于是又再添加了一個能處理異常的, 這次意外發(fā)現(xiàn)java的throws也支持泛型, 贊一個:

public static <t, e extends throwable> t trycatchexception(supplierexception<t, e> supplier, string servicename,
      string methodname) throws e {
  try {
      return supplier.get();
  } catch (motancustomexception ex) {
      if (ex.getcode().equals(msuserexception.notlogin().getcode()))
          throw userexception.notlogin();
      if (ex.getcode().equals(mspowerexception.havenopower().getcode()))
          throw powerexception.havenopower();
      throw ex;
  } catch (motanserviceexception ex) {
      cathelper.logeventservice(servicename + "-" + methodname, methodname, ex.getstatus(), ex.geterrorcode(),
              ex.getmessage());
      throw ex;
  } catch (motanabstractexception ex) {
      cathelper.logeventservice(servicename + "-" + methodname, methodname, ex.getstatus(), ex.geterrorcode(),
              ex.getmessage());
      throw ex;
  } catch (exception ex) {
      cathelper.logerror(ex);
      throw ex;
  }
}
@functionalinterface
public interface supplierexception<t, e extends throwable> {
  /**
   * gets a result.
   *
   * @return a result
   */
  t get() throws e;
}

為了不至于維護(hù)兩份catch集, 將原來的帶返回值的trycatch改為調(diào)用trycatchexception:

public static <t> t trycatch(supplier<t> supplier, string servicename, string methodname) {
  return trycatchexception(() -> supplier.get(), servicename, methodname);
}

這個世界又完善了一步。

前面制作了3種情況:

1.無返回值,無 throws

2.有返回值,無 throws

3.有返回值,有 throws

不確定會不會出現(xiàn)“無返回值,有throws“的情況。后來真的出現(xiàn)了!依樣畫葫蘆,弄出了這個:

public static <e extends throwable> void trycatchexception(runnableexception<e> runnable, string servicename,
      string methodname) throws e {
  trycatchexception(() -> {
      runnable.run();
      return null;
  }, servicename, methodname);
}
@functionalinterface
public interface runnableexception<e extends throwable> {
  void run() throws e;
}

完整代碼

package com.company.system.util;
import java.util.function.supplier;
import org.springframework.beans.factory.beancreationexception;
import com.company.cat.monitor.cathelper;
import com.company.system.customexception.powerexception;
import com.company.system.customexception.serviceexception;
import com.company.system.customexception.userexception;
import com.company.hyhis.ms.user.custom.exception.mspowerexception;
import com.company.hyhis.ms.user.custom.exception.msuserexception;
import com.company.hyhis.ms.user.custom.exception.motancustomexception;
import com.weibo.api.motan.exception.motanabstractexception;
import com.weibo.api.motan.exception.motanserviceexception;
public class thirdparty {
  public static void trycatch(runnable runnable, string servicename, string methodname) {
      trycatch(() -> {
          runnable.run();
          return null;
      }, servicename, methodname);
  }
  public static <t> t trycatch(supplier<t> supplier, string servicename, string methodname) {
      return trycatchexception(() -> supplier.get(), servicename, methodname);
  }
  public static <e extends throwable> void trycatchexception(runnableexception<e> runnable, string servicename,
          string methodname) throws e {
      trycatchexception(() -> {
          runnable.run();
          return null;
      }, servicename, methodname);
  }
  public static <t, e extends throwable> t trycatchexception(supplierexception<t, e> supplier, string servicename,
          string methodname) throws e {
      try {
          return supplier.get();
      } catch (motancustomexception ex) {
          if (ex.getcode().equals(msuserexception.notlogin().getcode()))
              throw userexception.notlogin();
          if (ex.getcode().equals(mspowerexception.havenopower().getcode()))
              throw powerexception.havenopower();
          throw ex;
      } catch (motanserviceexception ex) {
          cathelper.logeventservice(servicename + "-" + methodname, methodname, ex.getstatus(), ex.geterrorcode(),
                  ex.getmessage());
          throw ex;
      } catch (motanabstractexception ex) {
          cathelper.logeventservice(servicename + "-" + methodname, methodname, ex.getstatus(), ex.geterrorcode(),
                  ex.getmessage());
          throw ex;
      } catch (exception ex) {
          cathelper.logerror(ex);
          throw ex;
      }
  }
  @functionalinterface
  public interface runnableexception<e extends throwable> {
      void run() throws e;
  }
  @functionalinterface
  public interface supplierexception<t, e extends throwable> {
      t get() throws e;
  }
}

關(guān)于java通過lambda表達(dá)式實現(xiàn)簡化代碼的文章就介紹至此,更多相關(guān)java簡化代碼內(nèi)容請搜索碩編程以前的文章,希望以后支持碩編程!

下一節(jié):使用idea反編譯沒有擦除泛型的原因解析

java編程技術(shù)

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频