forked from assertj/assertj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.java
More file actions
242 lines (220 loc) · 8.41 KB
/
Strings.java
File metadata and controls
242 lines (220 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2020 the original author or authors.
*/
package org.assertj.core.util;
import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static org.assertj.core.util.Preconditions.checkArgument;
/**
* Utility methods related to {@code String}s.
*
* @author Alex Ruiz
*/
public final class Strings {
/**
* Indicates whether the given {@code String} is {@code null} or empty.
*
* @param s the {@code String} to check.
* @return {@code true} if the given {@code String} is {@code null} or empty, otherwise {@code false}.
*/
public static boolean isNullOrEmpty(String s) {
return s == null || s.length() == 0;
}
/**
* Returns the given {@code String} surrounded by single quotes, or {@code null} if the given {@code String} is
* {@code null}.
*
* @param s the given {@code String}.
* @return the given {@code String} surrounded by single quotes, or {@code null} if the given {@code String} is
* {@code null}.
*/
public static String quote(String s) {
return s != null ? concat("'", s, "'") : null;
}
/**
* Returns the given object surrounded by single quotes, only if the object is a {@code String}.
*
* @param o the given object.
* @return the given object surrounded by single quotes, only if the object is a {@code String}.
* @see #quote(String)
*/
public static Object quote(Object o) {
return o instanceof String ? quote(o.toString()) : o;
}
/**
* Concatenates the given objects into a single {@code String}. This method is more efficient than concatenating using
* "+", since only one <code>{@link StringBuilder}</code> is created.
*
* @param objects the objects to concatenate.
* @return a {@code String} containing the given objects.
*/
public static String concat(Object... objects) {
if (Arrays.isNullOrEmpty(objects)) {
return null;
}
return stream(objects).map(String::valueOf).collect(joining());
}
/**
* Format with {@link String#format(String, Object...)} the given message iif some args have been given otherwise juts
* return the message.
*
* @param message the string to format
* @param args args used to format the message, can be null or empty
* @return the formatted string if any args were given
*/
public static String formatIfArgs(String message, Object... args) {
return Arrays.isNullOrEmpty(args)
// here we need to format %n but not other % since we do not have arguments.
// => we replace all % to %% except if they are followed by a 'n'.
? format(escapePercentExceptWhenFollowedBy_n(message))
: format(message, args);
}
/**
* Escape any {@code %} to {@code %%} to avoid interpreting it in {@link String#format(String, Object...)}.
*
* @param value the String to escape
* @return the escaped String
*/
public static String escapePercent(String value) {
return value == null ? null : value.replace("%", "%%");
}
/**
* Joins the given {@code String}s using a given delimiter. The following example illustrates proper usage of this
* method:
* <pre><code class='java'> Strings.join("a", "b", "c").with("|");</code></pre>
*
* which will result in the {@code String} <code>"a|b|c"</code>.
*
* @param strings the {@code String}s to join.
* @return an intermediate object that takes a given delimiter and knows how to join the given {@code String}s.
* @see StringsToJoin#with(String)
*/
public static StringsToJoin join(String... strings) {
return new StringsToJoin(strings);
}
/**
* Joins the given {@code Object}s using a given delimiter. The following example illustrates proper usage of this
* method:
* <pre><code class='java'> Strings.join(new ArrayList("a", "b", "c")).with("|");</code></pre>
*
* which will result in the {@code String} <code>"a|b|c"</code>.
*
* @param toStringable the {@code Object}s to join.
* @return an intermediate object that takes a given delimiter and knows how to join the given {@code Object}s.
* @see StringsToJoin#with(String)
*/
public static StringsToJoin join(Iterable<?> toStringable) {
String[] strings = Streams.stream(toStringable).map(String::valueOf).toArray(String[]::new);
return new StringsToJoin(strings);
}
/**
* Knows how to join {@code String}s using a given delimiter.
*
* @see Strings#join(String[])
*/
public static class StringsToJoin {
/** The {@code String}s to join. */
private final String[] strings;
/**
* Creates a new <code>{@link StringsToJoin}</code>.
*
* @param strings the {@code String}s to join.
*/
StringsToJoin(String... strings) {
this.strings = strings;
}
/**
* Specifies the delimiter to use to join {@code String}s.
*
* @param delimiter the delimiter to use.
* @return the {@code String}s joined using the given delimiter.
*/
public String with(String delimiter) {
return with(delimiter, null);
}
/**
* Specifies the delimiter to use to join {@code String}s and the escape String to wrap strings with.
*
* @param delimiter the delimiter to use.
* @param escapeString the String wrapper to use.
* @return the {@code String}s joined using the given delimiter.
*/
public String with(String delimiter, String escapeString) {
checkArgument(delimiter != null, "Delimiter should not be null");
if (Arrays.isNullOrEmpty(strings)) {
return "";
}
String escape = escapeString == null ? "" : escapeString;
StringBuilder b = new StringBuilder();
int stringCount = strings.length;
for (int i = 0; i < stringCount; i++) {
String s = strings[i];
if (s != null) {
b.append(escape);
b.append(s);
b.append(escape);
}
if (i < stringCount - 1) {
b.append(delimiter);
}
}
return b.toString();
}
}
/**
* Appends a given {@code String} to the given target, only if the target does not end with the given {@code String}
* to append. The following example illustrates proper usage of this method:
* <pre><code class='java'> Strings.append("c").to("ab");
* Strings.append("c").to("abc");</code></pre>
*
* resulting in the {@code String} <code>"abc"</code> for both cases.
*
* @param toAppend the {@code String} to append.
* @return an intermediate object that takes the target {@code String} and knows to append the given {@code String}.
* @see StringToAppend#to(String)
*/
public static StringToAppend append(String toAppend) {
return new StringToAppend(toAppend);
}
/**
* Knows how to append a given {@code String} to the given target, only if the target does not end with the given
* {@code String} to append.
*/
public static class StringToAppend {
private final String toAppend;
StringToAppend(String toAppend) {
this.toAppend = toAppend;
}
/**
* Appends the {@code String} specified in the constructor to the {@code String} passed as argument.
*
* @param s the target {@code String}.
* @return a {@code String} containing the target {@code String} with the given {@code String} to append added to
* the end.
*/
public String to(String s) {
if (!s.endsWith(toAppend)) {
return concat(s, toAppend);
}
return s;
}
}
// change %%n back to %n which could have been done by calling escapePercent
private static String escapePercentExceptWhenFollowedBy_n(String message) {
return revertEscapingPercent_n(escapePercent(message));
}
private static String revertEscapingPercent_n(String value) {
return value == null ? null : value.replace("%%n", "%n");
}
private Strings() {}
}