Java通过Lambda表达式实现简化代码
java通过lambda表达式实现简化代码
之前,调用第3方服务,每个方法都差不多“长”这样, 写起来啰嗦, 改起来麻烦, 还容易改漏。
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; } }
我经过学习和提取封装, 将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); }
现在用起来是如此简洁。像这种无返回值的:
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"); }
后来发现以上2个方法还不够用, 原因是有一些方法会抛出 checked 异常, 于是又再添加了一个能处理异常的, 这次意外发现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; }
为了不至于维护两份catch集, 将原来的带返回值的trycatch改为调用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
不确定会不会出现“无返回值,有throws“的情况。后来真的出现了!依样画葫芦,弄出了这个:
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; } }
关于java通过lambda表达式实现简化代码的文章就介绍至此,更多相关java简化代码内容请搜索硕编程以前的文章,希望以后支持硕编程!