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

Java 如何跳过责任链中的空值?,java,Java,我理解责任链模式。我有这样一个问题。从我的职责链中可以看出,如果该方法没有将区域设置返回给我,那么它将返回null。如果链中的下一项返回null,我如何转到该项 public abstract class StandardLocaleHandler { protected StandardLocaleHandler localeHandler; public StandardLocaleHandler() {

我理解责任链模式。我有这样一个问题。从我的职责链中可以看出,如果该方法没有将区域设置返回给我,那么它将返回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;
            }
        }
公共抽象类StandardLocaleHandler{
受保护的标准localeHandler localeHandler;
公共标准localehandler(){
this.localeHandler=null;
}
受保护的抽象语言环境getTrueLocale(HttpServletRequest-req、HttpServletResponse-resp、List localeList、String defaultLocale、Integer cookieAge);
public void setNext(StandardLocaleHandler localeHandler){
this.localeHandler=localeHandler;
}
公共标准LocaleHandler getNext(){
返回localeHandler;
}
}
公共类GetLocaleByAvailable扩展了StandardLocaleHandler{
@凌驾
受保护的区域设置getTrueLocale(HttpServletRequest-req、HttpServletResponse-resp、List localeList、String defaultLocale、Integer cookieAge){
if(isNull(req.getSession().getAttribute(LANG_属性))&&isNull(req.getCookies()){
返回可用设置(req、resp、localeList、defaultLocale、cookieAge);
}
返回null;
}
}
公共类GetLocaleBySession扩展了StandardLocaleHandler{
@凌驾
受保护的区域设置getTrueLocale(HttpServletRequest-req、HttpServletResponse-resp、List localeList、String defaultLocale、Integer cookieAge){
if(非空(req.getSession().getAttribute(LANG_属性))){
LOG.debug(req.getParameter(LANG_属性));
返回新的区域设置((字符串)req.getSession().getAttribute(LANG_属性));
}
返回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);
公共类链生成器{
private List localehandler=new ArrayList();
公共无效添加过滤器列表(标准LocaleHandler过滤器){
如果(!localeHandlers.contains(筛选器)){
添加(过滤器);
}否则{
抛出新的IllegalArgumentException(“已在列表中”);
}
}
公共标准LocaleHandler创建责任链(){
对于(int i=0;i

如果链中的下一项返回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 GetLocaleByAvailable extends StandardLocaleHandler {

        @Override
        protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
            Locale result = null;
            if (isNull(req.getSession().getAttribute(LANG_ATTRIBUTE)) && isNull(req.getCookies())) {
                result = setAvailable(req, resp, localeList, defaultLocale, cookieAge);
            }
            if (result == null) {
                StandardLocaleHandler nextHandler = getNext();
                if (nextHandler == null) {
                    return nextHandler.getTrueLocale(....);
                }
            }
            return result;              
        }
    }
公共类GetLocaleByAvailable扩展了StandardLocaleHandler{
@凌驾
受保护的区域设置getTrueLocale(HttpServletRequest-req、HttpServletResponse-resp、List localeList、String defaultLocale、Integer cookieAge){
区域设置结果=null;
if(isNull(req.getSession().getAttribute(LANG_属性))&&isNull(req.getCookies()){
结果=设置可用(req、resp、localeList、defaultLocale、cookieAge);
}
如果(结果==null){
StandardLocaleHandler-nextHandler=getNext();
if(nextHandler==null){
返回nextHandler.getRueLocale(..);
}
}
返回结果;
}
}

对于另一个
处理程序
类也是如此。

您需要将代码更改为如下内容

public class GetLocaleByAvailable extends StandardLocaleHandler {

        @Override
        protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
            Locale result = null;
            if (isNull(req.getSession().getAttribute(LANG_ATTRIBUTE)) && isNull(req.getCookies())) {
                result = setAvailable(req, resp, localeList, defaultLocale, cookieAge);
            }
            if (result == null) {
                StandardLocaleHandler nextHandler = getNext();
                if (nextHandler == null) {
                    return nextHandler.getTrueLocale(....);
                }
            }
            return result;              
        }
    }
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;
    }
}
公共类GetLocaleByAvailable扩展了StandardLocaleHandler{
@凌驾
受保护的区域设置getTrueLocale(HttpServletRequest-req、HttpServletResponse-resp、List localeList、String defaultLocale、Integer cookieAge){
区域设置结果=null;
if(isNull(req.getSession().getAttribute(LANG_属性))&&isNull(req.getCookies()){
结果=设置可用(req、resp、localeList、defaultLocale、cookieAge);
}
如果(结果==null){
StandardLocaleHandler-nextHandler=getNext();
if(nextHandler==null){
返回nextHandler.getRueLocale(..);
}
}
返回结果;
}
}
对于另一个
处理程序
类也是如此

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;
    }
}