| 1 | /*
|
|---|
| 2 | * Copyright 2002-2012 Drew Noakes
|
|---|
| 3 | *
|
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 5 | * you may not use this file except in compliance with the License.
|
|---|
| 6 | * You may obtain a copy of the License at
|
|---|
| 7 | *
|
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 9 | *
|
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 13 | * See the License for the specific language governing permissions and
|
|---|
| 14 | * limitations under the License.
|
|---|
| 15 | *
|
|---|
| 16 | * More information about this project is available at:
|
|---|
| 17 | *
|
|---|
| 18 | * http://drewnoakes.com/code/exif/
|
|---|
| 19 | * http://code.google.com/p/metadata-extractor/
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | package com.drew.lang;
|
|---|
| 23 |
|
|---|
| 24 | import com.drew.lang.annotations.NotNull;
|
|---|
| 25 |
|
|---|
| 26 | import java.util.Iterator;
|
|---|
| 27 |
|
|---|
| 28 | /** @author Drew Noakes http://drewnoakes.com */
|
|---|
| 29 | public class StringUtil
|
|---|
| 30 | {
|
|---|
| 31 | public static String join(@NotNull Iterable<? extends CharSequence> strings, @NotNull String delimiter)
|
|---|
| 32 | {
|
|---|
| 33 | int capacity = 0;
|
|---|
| 34 | int delimLength = delimiter.length();
|
|---|
| 35 |
|
|---|
| 36 | Iterator<? extends CharSequence> iter = strings.iterator();
|
|---|
| 37 | if (iter.hasNext())
|
|---|
| 38 | capacity += iter.next().length() + delimLength;
|
|---|
| 39 |
|
|---|
| 40 | StringBuilder buffer = new StringBuilder(capacity);
|
|---|
| 41 | iter = strings.iterator();
|
|---|
| 42 | if (iter.hasNext()) {
|
|---|
| 43 | buffer.append(iter.next());
|
|---|
| 44 | while (iter.hasNext()) {
|
|---|
| 45 | buffer.append(delimiter);
|
|---|
| 46 | buffer.append(iter.next());
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | return buffer.toString();
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | public static <T extends CharSequence> String join(@NotNull T[] strings, @NotNull String delimiter)
|
|---|
| 53 | {
|
|---|
| 54 | int capacity = 0;
|
|---|
| 55 | int delimLength = delimiter.length();
|
|---|
| 56 | for (T value : strings)
|
|---|
| 57 | capacity += value.length() + delimLength;
|
|---|
| 58 |
|
|---|
| 59 | StringBuilder buffer = new StringBuilder(capacity);
|
|---|
| 60 | boolean first = true;
|
|---|
| 61 | for (T value : strings) {
|
|---|
| 62 | if (!first) {
|
|---|
| 63 | buffer.append(delimiter);
|
|---|
| 64 | } else {
|
|---|
| 65 | first = false;
|
|---|
| 66 | }
|
|---|
| 67 | buffer.append(value);
|
|---|
| 68 | }
|
|---|
| 69 | return buffer.toString();
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|