Java:用Lambda運算式簡化代碼一例,lambda運算式

來源:互聯網
上載者:User

Java:用Lambda運算式簡化代碼一例,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");}

  這是我的第一篇Java文章。學習Java的過程中,既有驚喜,也有失望。以後會繼續來分享心得。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.