[6127] | 1 | /*
|
---|
[13061] | 2 | * Copyright 2002-2017 Drew Noakes
|
---|
[6127] | 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 | *
|
---|
[8132] | 18 | * https://drewnoakes.com/code/exif/
|
---|
| 19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
[6127] | 20 | */
|
---|
| 21 |
|
---|
| 22 | package com.drew.lang;
|
---|
| 23 |
|
---|
| 24 | import com.drew.lang.annotations.NotNull;
|
---|
[8132] | 25 | import com.drew.lang.annotations.Nullable;
|
---|
[6127] | 26 |
|
---|
[8132] | 27 | import java.io.BufferedReader;
|
---|
| 28 | import java.io.IOException;
|
---|
| 29 | import java.io.InputStream;
|
---|
| 30 | import java.io.InputStreamReader;
|
---|
[6127] | 31 | import java.util.Iterator;
|
---|
| 32 |
|
---|
[8132] | 33 | /**
|
---|
| 34 | * @author Drew Noakes https://drewnoakes.com
|
---|
| 35 | */
|
---|
[13061] | 36 | public final class StringUtil
|
---|
[6127] | 37 | {
|
---|
[8132] | 38 | @NotNull
|
---|
[6127] | 39 | public static String join(@NotNull Iterable<? extends CharSequence> strings, @NotNull String delimiter)
|
---|
| 40 | {
|
---|
| 41 | int capacity = 0;
|
---|
| 42 | int delimLength = delimiter.length();
|
---|
| 43 |
|
---|
| 44 | Iterator<? extends CharSequence> iter = strings.iterator();
|
---|
| 45 | if (iter.hasNext())
|
---|
| 46 | capacity += iter.next().length() + delimLength;
|
---|
| 47 |
|
---|
| 48 | StringBuilder buffer = new StringBuilder(capacity);
|
---|
| 49 | iter = strings.iterator();
|
---|
| 50 | if (iter.hasNext()) {
|
---|
| 51 | buffer.append(iter.next());
|
---|
| 52 | while (iter.hasNext()) {
|
---|
| 53 | buffer.append(delimiter);
|
---|
| 54 | buffer.append(iter.next());
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | return buffer.toString();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[8132] | 60 | @NotNull
|
---|
[6127] | 61 | public static <T extends CharSequence> String join(@NotNull T[] strings, @NotNull String delimiter)
|
---|
| 62 | {
|
---|
| 63 | int capacity = 0;
|
---|
| 64 | int delimLength = delimiter.length();
|
---|
| 65 | for (T value : strings)
|
---|
| 66 | capacity += value.length() + delimLength;
|
---|
| 67 |
|
---|
| 68 | StringBuilder buffer = new StringBuilder(capacity);
|
---|
| 69 | boolean first = true;
|
---|
| 70 | for (T value : strings) {
|
---|
| 71 | if (!first) {
|
---|
| 72 | buffer.append(delimiter);
|
---|
| 73 | } else {
|
---|
| 74 | first = false;
|
---|
| 75 | }
|
---|
| 76 | buffer.append(value);
|
---|
| 77 | }
|
---|
| 78 | return buffer.toString();
|
---|
| 79 | }
|
---|
[8132] | 80 |
|
---|
| 81 | @NotNull
|
---|
| 82 | public static String fromStream(@NotNull InputStream stream) throws IOException
|
---|
| 83 | {
|
---|
| 84 | BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
---|
| 85 | StringBuilder sb = new StringBuilder();
|
---|
| 86 | String line;
|
---|
| 87 | while ((line = reader.readLine()) != null) {
|
---|
| 88 | sb.append(line);
|
---|
| 89 | }
|
---|
| 90 | return sb.toString();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public static int compare(@Nullable String s1, @Nullable String s2)
|
---|
| 94 | {
|
---|
| 95 | boolean null1 = s1 == null;
|
---|
| 96 | boolean null2 = s2 == null;
|
---|
| 97 |
|
---|
| 98 | if (null1 && null2) {
|
---|
| 99 | return 0;
|
---|
| 100 | } else if (null1) {
|
---|
| 101 | return -1;
|
---|
| 102 | } else if (null2) {
|
---|
| 103 | return 1;
|
---|
| 104 | } else {
|
---|
| 105 | return s1.compareTo(s2);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @NotNull
|
---|
| 110 | public static String urlEncode(@NotNull String name)
|
---|
| 111 | {
|
---|
| 112 | // Sufficient for now, it seems
|
---|
| 113 | return name.replace(" ", "%20");
|
---|
| 114 | }
|
---|
[6127] | 115 | }
|
---|