Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.feilong.core.lang.StringUtil.toSingleValueMap(String, Class<T>, Class<V>) 要支持定制转换value类 #738

Closed
venusdrogon opened this issue Sep 5, 2024 · 1 comment
Milestone

Comments

@venusdrogon
Copy link
Collaborator

image

@venusdrogon venusdrogon added this to the 4.1.2 milestone Sep 28, 2024
@venusdrogon
Copy link
Collaborator Author

以前代码

  private Map<Long, ApplePodcastTrackQueryExtendParam> loadAlbumIdAndExtendParamMap(String logPrefixKey){
        String specialUploadTrackByAlbumList = applePodcastFootballConfig.getSpecialUploadTrackByAlbumList();
        if (isNullOrEmpty(specialUploadTrackByAlbumList)){
            log.info("logPrefixKey:[{}] specialUploadTrackByAlbumListIsNullOrEmpty 理论上isNeedExecute 已经判断了不会到这里", logPrefixKey);
            return emptyMap();
        }

        //---------------------------------------------------------------
        //        73034693=0-50;
        //        11487680=0-43;
        //        51099626=0-50;
        Map<Long, String> albumIdAndParamConfig = StringUtil.toSingleValueMap(specialUploadTrackByAlbumList, Long.class, String.class);
        if (isNullOrEmpty(albumIdAndParamConfig)){
            log.info("logPrefixKey:[{}] albumIdAndParamConfigIsNullOrEmpty 理论上isNeedExecute 已经判断了不会到这里", logPrefixKey);
            return emptyMap();
        }

        //---------------------------------------------------------------
        Map<Long, ApplePodcastTrackQueryExtendParam> resultMap = newLinkedHashMap();
        for (Map.Entry<Long, String> entry : albumIdAndParamConfig.entrySet()){
            Long albumId = entry.getKey();
            String value = entry.getValue();

            //必须配置 专辑id=0-9999这种格式,  避免出现误上传
            Validate.notBlank(value, formatPattern("logPrefixKey:[{}] valueIsNull entry:[{}]", logPrefixKey, entry));

            ApplePodcastTrackQueryExtendParam extendParam = parseExtendParam(logPrefixKey, value);

            Validate.notNull(resultMap, formatPattern("logPrefixKey:[{}] extendParamIsNll entry:[{}]", logPrefixKey, entry));
            resultMap.put(albumId, extendParam);
        }
        return resultMap;
    }

    private ApplePodcastTrackQueryExtendParam parseExtendParam(String logPrefixKey,String value){
        String[] albumQueryParamsArray = StringUtil.tokenizeToStringArray(value, "-");
        if (isNullOrEmpty(albumQueryParamsArray)){
            log.error("logPrefixKey:[{}] albumQueryParamsArrayIsNullOrEmpty value:[{}]", logPrefixKey, value);
            return null;
        }

        try{
            //只有1个 
            if (1 == size(albumQueryParamsArray)){
                //默认 0-x
                Integer max = toInteger(albumQueryParamsArray[0]);
                return new ApplePodcastTrackQueryExtendParam(0, max);
            }

            Integer min = toInteger(albumQueryParamsArray[0]);
            Integer max = toInteger(albumQueryParamsArray[1]);
            return new ApplePodcastTrackQueryExtendParam(min, max);

        }catch (Exception e){
            //有异常返回null 兼容
            log.error(formatPattern("logPrefixKey:[{}] value:[{}]", logPrefixKey, value), e);
            return null;
        }
    }
package com.ximalaya.openapi.content.operation.center.model.track;

import com.feilong.lib.lang3.builder.ToStringBuilder;
import com.feilong.lib.lang3.builder.ToStringStyle;

/**
 * 查询参数.
 */
public class ApplePodcastTrackQueryExtendParam{

    /** 单集编号 最小. */
    private Integer minEpisodeNumber;

    /** 单集编号 最大. */
    private Integer maxEpisodeNumber;

    //---------------------------------------------------------------

    /**
     * 
     */
    public ApplePodcastTrackQueryExtendParam(){
        super();
    }

    /**
     * @param minEpisodeNumber
     * @param maxEpisodeNumber
     */
    public ApplePodcastTrackQueryExtendParam(Integer minEpisodeNumber, Integer maxEpisodeNumber){
        super();
        this.minEpisodeNumber = minEpisodeNumber;
        this.maxEpisodeNumber = maxEpisodeNumber;
    }

    //---------------------------------------------------------------

    /**
     * To string.
     *
     * @return the string
     */
    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString(){
        return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
    }

    /**
     * Gets the 单集编号 最小.
     *
     * @return the 单集编号 最小
     */
    public Integer getMinEpisodeNumber(){
        return minEpisodeNumber;
    }

    /**
     * Sets the 单集编号 最小.
     *
     * @param minEpisodeNumber
     *            the new 单集编号 最小
     */
    public void setMinEpisodeNumber(Integer minEpisodeNumber){
        this.minEpisodeNumber = minEpisodeNumber;
    }

    /**
     * Gets the 单集编号 最大.
     *
     * @return the 单集编号 最大
     */
    public Integer getMaxEpisodeNumber(){
        return maxEpisodeNumber;
    }

    /**
     * Sets the 单集编号 最大.
     *
     * @param maxEpisodeNumber
     *            the new 单集编号 最大
     */
    public void setMaxEpisodeNumber(Integer maxEpisodeNumber){
        this.maxEpisodeNumber = maxEpisodeNumber;
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant