forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
192 lines (174 loc) · 6.15 KB
/
Util.java
File metadata and controls
192 lines (174 loc) · 6.15 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
package io.sentry.util;
import java.io.Closeable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* Sentry static Utility class.
*/
public final class Util {
// Hide the constructor.
private Util() {
}
/**
* Returns {@code true} if the given string is null or is the empty string.
*
* @param string a string reference to check
* @return {@code true} if the string is null or is the empty string
*/
public static boolean isNullOrEmpty(String string) {
return string == null || string.length() == 0; // string.isEmpty() in Java 6
}
private static Map<String, String> parseCsv(String inputString, String typeName) {
if (isNullOrEmpty(inputString)) {
return Collections.emptyMap();
}
String[] entries = inputString.split(",");
Map<String, String> map = new LinkedHashMap<String, String>();
for (String entry : entries) {
String[] split = entry.split(":");
if (split.length != 2) {
throw new IllegalArgumentException("Invalid " + typeName + " entry: " + entry);
}
map.put(split[0], split[1]);
}
return map;
}
/**
* Parses the provided tags string into a Map of String -> String.
*
* @param tagsString comma-delimited key-value pairs, e.g. "tag1:value1,tag2:value2".
* @return Map of tags e.g. (tag1 -> value1, tag2 -> value2)
*/
public static Map<String, String> parseTags(String tagsString) {
return parseCsv(tagsString, "tags");
}
/**
* Parses the provided extras string into a Map of String -> String.
*
* @param extrasString comma-delimited key-value pairs, e.g. "extra1:value1,extra2:value2".
* @return Map of extras e.g. (extra1 -> value1, extra2 -> value2)
*/
public static Map<String, String> parseExtra(String extrasString) {
return parseCsv(extrasString, "extras");
}
/**
* Parses the provided extraTags string into a Set of Strings.
*
* @param extraTagsString comma-delimited tags
* @return Set of Strings representing extra tags
* @deprecated prefer {@link Util#parseMdcTags(String)}
*/
@Deprecated
public static Set<String> parseExtraTags(String extraTagsString) {
return parseMdcTags(extraTagsString);
}
/**
* Parses the provided Strings into a Set of Strings.
*
* @param mdcTagsString comma-delimited tags
* @return Set of Strings representing mdc tags
*/
public static Set<String> parseMdcTags(String mdcTagsString) {
if (isNullOrEmpty(mdcTagsString)) {
return Collections.emptySet();
}
return new HashSet<String>(Arrays.asList(mdcTagsString.split(",")));
}
/**
* Parses the provided string value into an integer value.
* <p>If the string is null or empty this returns the default value.</p>
*
* @param value value to parse
* @param defaultValue default value
* @return integer representation of provided value or default value.
*/
public static Integer parseInteger(String value, Integer defaultValue) {
if (isNullOrEmpty(value)) {
return defaultValue;
}
return Integer.parseInt(value);
}
/**
* Parses the provided string value into a long value.
* <p>If the string is null or empty this returns the default value.</p>
*
* @param value value to parse
* @param defaultValue default value
* @return long representation of provided value or default value.
*/
public static Long parseLong(String value, Long defaultValue) {
if (isNullOrEmpty(value)) {
return defaultValue;
}
return Long.parseLong(value);
}
/**
* Parses the provided string value into a double value.
* <p>If the string is null or empty this returns the default value.</p>
*
* @param value value to parse
* @param defaultValue default value
* @return double representation of provided value or default value.
*/
public static Double parseDouble(String value, Double defaultValue) {
if (isNullOrEmpty(value)) {
return defaultValue;
}
return Double.parseDouble(value);
}
/**
* Trims a String, ensuring that the maximum length isn't reached.
*
* @param string string to trim
* @param maxMessageLength maximum length of the string
* @return trimmed string
*/
public static String trimString(String string, int maxMessageLength) {
if (string == null) {
return null;
} else if (string.length() > maxMessageLength) {
// CHECKSTYLE.OFF: MagicNumber
return string.substring(0, maxMessageLength - 3) + "...";
// CHECKSTYLE.ON: MagicNumber
} else {
return string;
}
}
/**
* Try to remove the shutDownHook, handling the case where the VM is in shutdown process.
* @param shutDownHook the shutDownHook to remove
* @return true if the hook was removed, false otherwise
*/
public static boolean safelyRemoveShutdownHook(Thread shutDownHook) {
try {
return Runtime.getRuntime().removeShutdownHook(shutDownHook);
} catch (IllegalStateException e) {
// CHECKSTYLE.OFF: EmptyBlock
if (e.getMessage().equals("Shutdown in progress")) {
// ignore
} else {
throw e;
}
// CHECKSTYLE.ON: EmptyBlock
}
return false;
}
/**
* Closes an instance of Closeable and suppress all possible raised Exceptions.
* @param closeable an instance that needs to be closed. null value is supported.
*/
public static void closeQuietly(Closeable closeable) {
// CHECKSTYLE.OFF: EmptyCatchBlock
if (closeable != null) {
try {
closeable.close();
} catch (Exception ignored) {
}
}
// CHECKSTYLE.ON: EmptyCatchBlock
}
}