Project

General

Profile

« Previous | Next » 

Revision 8ab5e47c

Added by Andreas Müller almost 4 years ago

fix #9147 use StringBuffer in concat()

View differences:

cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/CdmUtils.java
169 169

  
170 170
    /**
171 171
     * Concatenates an array of strings using the defined separator.<BR>
172
     * <code>Null</code> values are interpreted as empty strings.<BR>
172
     * <code>Null</code> values and empty strings are handled as if they
173
     * do not exist. So <BR><BR>
174
     *
175
     * concat(":", "a", "", null, "b") results in "a:b"<BR><BR>
176
     *
173 177
     * If all strings are <code>null</code> then <code>null</code> is returned.
174
     * @param strings
175
     * @param seperator
176
     * @return String
177 178
     *
178
     * @deprecated Use `String.join()` or one of the <code>org.apache.commons.lang3.StringUtils.join(..)</code> methods instead. These methods
179
     * are making explicit use of the <code>StringBuffer</code> class. This method here can not be optimized by the jit, so
180
     * the for each item the new String object needs to be extended to new length, which is known to be a performance penalty.
179
     * @see #concat(CharSequence, String, String)
180
     * @param strings the strings to concatenate
181
     * @param seperator the separator for concatenation
182
     * @return String the concatenation result
181 183
     */
182
    @Deprecated
183 184
    static public String concat(CharSequence separator, String... strings){
184
        String result = "";
185
        StringBuffer result = new StringBuffer();
185 186
        boolean allNull = true;
186 187
        for (String string : strings){
187 188
            if (string != null){
188 189
                if (result.length() > 0 && string.length() > 0){
189
                    result += separator;
190
                    result.append(separator);
190 191
                }
191
                result += string;
192
                result.append(string);
192 193
                allNull = false;
193 194
            }
194 195
        }
......
196 197
        if (allNull){
197 198
            return null;
198 199
        }else {
199
            return result;
200
            return result.toString();
200 201
        }
201 202
    }
202 203

  
204

  
203 205
    /**
204
     * Concatenates two strings, using the defined seperator.<BR>
205
     * <code>Null</code> values are interpreted as empty Strings.<BR>
206
     * Concatenates two strings, using the defined separator.<BR>
207
     * <code>Null</code> values are interpreted as empty strings.<BR>
208
     * Empty strings are not included in concatenation so concat(":", "a", "")
209
     * results in "a", not "a:".<BR>
210
     *
206 211
     * If both strings are <code>null</code> then <code>null</code> is returned.
207
     * @see #concat(CharSequence, String[])
208
     * @param seperator
209
     * @param string1
210
     * @param string2
211
     * @return String
212 212
     *
213
     * @deprecated Use `String.join()` or one of the <code>org.apache.commons.lang3.StringUtils.join(..)</code> methods instead. These methods
214
     * are making explicit use of the <code>StringBuffer</code> class. This method here can not be optimized by the jit, so
215
     * the for each item the new String object needs to be extended to new length, which is known to be a performance penalty.
213
     * @see #concat(CharSequence, String[])
214
     * @param sepearator the separator
215
     * @param string1 first string to concatenate
216
     * @param string2 second string to concatenate
217
     * @return String the concatenated string
216 218
     */
217
    @Deprecated
218 219
    static public String concat(CharSequence separator, String string1, String string2){
219 220
        String[] strings = {string1, string2};
220 221
        return concat(separator, strings);

Also available in: Unified diff