zoukankan      html  css  js  c++  java
  • StringJoiner

    StringJoiner是Java8新出的一个类,用于构造由分隔符分隔的字符序列,并可选择性地从提供的前缀开始和以提供的后缀结尾

     

    package java.util;
    
    public final class StringJoiner {
        private final String prefix;//前缀
        private final String delimiter;//间隔符
        private final String suffix;//后缀
        private StringBuilder value;//
    
        private String emptyValue;//空值
    
        public StringJoiner(CharSequence delimiter) {
            this(delimiter, "", "");//默认前缀和后缀为"",重载调用
        }
    
        public StringJoiner(CharSequence delimiter,
                            CharSequence prefix,
                            CharSequence suffix) {
            //间隔符,前缀和后缀判断是否为null,null将抛出异常
            Objects.requireNonNull(prefix, "The prefix must not be null");
            Objects.requireNonNull(delimiter, "The delimiter must not be null");
            Objects.requireNonNull(suffix, "The suffix must not be null"); 
            // 成员变量赋值
            this.prefix = prefix.toString();
            this.delimiter = delimiter.toString();
            this.suffix = suffix.toString();
            this.emptyValue = this.prefix + this.suffix;//空值被设置为只有前后缀
        }
        //设置空值,检查是否为null
        public StringJoiner setEmptyValue(CharSequence emptyValue) {
            this.emptyValue = Objects.requireNonNull(emptyValue,
                "The empty value must not be null").toString();
            return this;
        }
    
        @Override
        public String toString() {
            if (value == null) {
                return emptyValue;//没有值将返回空值或者后续设置的空值
            } else {
                if (suffix.equals("")) {
                    return value.toString();//后缀为""直接返回字符串,不用添加
                } else {
                    //后缀不为"",添加后缀,然后直接返回字符串,修改长度
                    int initialLength = value.length();
                    String result = value.append(suffix).toString();
                    // reset value to pre-append initialLength
                    value.setLength(initialLength);
                    return result;
                }
            }
        }
        初始化,先添加前缀,有了之后每次先添加间隔符,StringBuilder后续append字符串
        public StringJoiner add(CharSequence newElement) {
            prepareBuilder().append(newElement);
            return this;
        }
        //合并StringJoiner,注意后面StringJoiner 的前缀就不要了,后面的appen进来
        public StringJoiner merge(StringJoiner other) {
            Objects.requireNonNull(other);
            if (other.value != null) {
                final int length = other.value.length();
                // lock the length so that we can seize the data to be appended
                // before initiate copying to avoid interference, especially when
                // merge 'this'
                StringBuilder builder = prepareBuilder();
                builder.append(other.value, other.prefix.length(), length);
            }
            return this;
        }
        //初始化,先添加前缀,有了之后每次先添加间隔符
        private StringBuilder prepareBuilder() {
            if (value != null) {
                value.append(delimiter);
            } else {
                value = new StringBuilder().append(prefix);
            }
            return value;
        }
    
        public int length() {
            // Remember that we never actually append the suffix unless we return
            // the full (present) value or some sub-string or length of it, so that
            // we can add on more if we need to.
            //不忘添加后缀的长度
            return (value != null ? value.length() + suffix.length() :
                    emptyValue.length());
        }
    }
    加油,愿被这世界温柔以待 ^_^
  • 相关阅读:
    [RK3288][Android6.0] U-boot 启动流程小结【转】
    学习笔记二十三——字符函数库cctype【转】
    【Git学习笔记】用git pull取回远程仓库某个分支的更新,再与本地的指定分支自动merge【转】
    Git 少用 Pull 多用 Fetch 和 Merge 【已翻译100%】【转】
    git 拉取和获取 pull 和 fetch 区别【转】
    setprecision、fixed、showpoint的用法总结(经典!!超经典!!)【转】
    Android休眠唤醒机制简介(二)
    获取元素个数的函数
    返回两个时间范围内的一个随机时间
    全角半角转换函数
  • 原文地址:https://www.cnblogs.com/liruilong/p/14581763.html
Copyright © 2011-2022 走看看