java - 如何跳过责任链中的空值?

标签 java

我了解责任链模式。我有这样一个问题。从我的责任链中可以看出,如果该方法没有将语言环境返回给我,那么它会返回 null。如果返回 null,如何转到链中的下一项?

public abstract class StandardLocaleHandler {

            protected StandardLocaleHandler localeHandler;


            public StandardLocaleHandler() {
                this.localeHandler = null;
            }

            protected abstract Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge);

            public void setNext(StandardLocaleHandler localeHandler) {
                this.localeHandler = localeHandler;
            }

            public StandardLocaleHandler getNext() {
                return localeHandler;
            }
        }

        public class GetLocaleByAvailable extends StandardLocaleHandler {

            @Override
            protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
                if (isNull(req.getSession().getAttribute(LANG_ATTRIBUTE)) && isNull(req.getCookies())) {
                    return setAvailable(req, resp, localeList, defaultLocale, cookieAge);
                }
                return null;
            }
        }

    public class GetLocaleBySession extends StandardLocaleHandler {

            @Override
            protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
                if (nonNull(req.getSession().getAttribute(LANG_ATTRIBUTE))) {
                    LOG.debug(req.getParameter(LANG_ATTRIBUTE));
                    return new Locale((String) req.getSession().getAttribute(LANG_ATTRIBUTE));
                }
                return null;
            }
        }

我是这样形成我的责任链的:

public class ChainBuilder {

    private List<StandardLocaleHandler> localeHandlers = new ArrayList<>();

    public void addToFilterList(StandardLocaleHandler filter) {
        if (!localeHandlers.contains(filter)) {
            localeHandlers.add(filter);
        } else {
            throw new IllegalArgumentException("Already in the list");
        }
    }

    public StandardLocaleHandler createChainOfResponsibility() {
        for (int i = 0; i < localeHandlers.size() - 1; i++) {
            localeHandlers.get(i).setNext(localeHandlers.get(i + 1));
        }
        return localeHandlers.get(0);
    }
}

ChainBuilder builder = new ChainBuilder();
        builder.addToFilterList(new GetLocaleByAvailable());
        builder.addToFilterList(new GetLocaleByParam());
        builder.addToFilterList(new GetLocaleBySession());
        builder.addToFilterList(new GetLocaleByCookie());

        StandardLocaleHandler handler = builder.createChainOfResponsibility();
        return handler.getTrueLocale(req, resp, localeList, defaultLocale, cookieAge);

如果返回 null,如何转到链中的下一项?

最佳答案

public abstract class StandardLocaleHandler {
    public final Locale getTrueLocale() {
        Locale local = getTrueLocaleInternal();
        return local == null && localeHandler != null ? localeHandler.getTrueLocale() : local;
    }

    protected abstract Locale getTrueLocaleInternal();
}

public class GetLocaleByAvailable extends StandardLocaleHandler {

    @Override
    protected Locale getTrueLocaleInternal() {
        // TODO logic
        return null;
    }
}

关于java - 如何跳过责任链中的空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53047580/

相关文章:

java - Android如何在android中获取两个时区之间的时差?

java - 尝试下载文件时,Apache HttpClient 在握手异常期间抛出远程主机关闭连接

Java:执行器与队列的关系

java - Spring异步RMI调用

java - 使用 Mockito 测试 Spring 环境配置文件

java - 在 jetty 中部署 webapps 和 websockets

java - GWT 基于字符串排序

java - 解析钛合金中的xml

java - 在 URL 中附加特殊字符

java - 排序键是 HashMap 中的日期条目