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
|
/*******************************************************************************
* Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
* This file is part of Gluster Management Console.
*
* Gluster Management Console is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Gluster Management Console is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*******************************************************************************/
package org.gluster.storage.management.core.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class StringUtil {
public static boolean filterString(String sourceString, String filterString, boolean caseSensitive) {
return caseSensitive ? sourceString.contains(filterString) : sourceString.toLowerCase().contains(
filterString.toLowerCase());
}
public static String removeSpaces(String str) {
return str.replaceAll("\\s+", "");
}
public static String collectionToString(Collection<? extends Object> list, String delimiter) {
if (list.size() == 0) {
return "";
}
StringBuilder output = new StringBuilder();
for (Object element : list) {
output.append(element.toString()).append(delimiter);
}
String outputStr = output.toString();
int endIndex = outputStr.length() - delimiter.length();
return outputStr.substring(0, endIndex);
}
public static <T extends Enum<T>> List<String> enumToArray(T[] values) {
List<String> enumAsArray = new ArrayList<String>();
for (T value : values) {
enumAsArray.add(value.toString());
}
return enumAsArray;
}
/**
* Extracts a list from a string by splitting it on given delimiter
* @param input the input string
* @return A {@link List} of extracted tokens
*/
public static List<String> extractList(String input, String delim) {
String[] arr = input.split(delim);
List<String> output = new ArrayList<String>();
for(String str : arr) {
String brick = str.trim();
if(!brick.isEmpty()) {
output.add(brick);
}
}
return output;
}
/**
* Extracts a map from a string by splitting it on the given primary and secondary delimiter. e.g. The input string
* <i>k1=v1,k2=v2,k3=v3</i> will yield the following map:<br>
* k1 -> v1<br>
* k2 -> v2<br>
* k3 -> v3<br>
* where <b>,</b> is the primary delimiter and <b>=</b> is the secondary delimiter.
*
* @param input
* @param majorDelim
* @param minorDelim
* @return Map of key value pairs
*/
public static Map<String, String> extractMap(String input, String majorDelim, String minorDelim) {
String[] arr = input.split(majorDelim);
Map<String, String> output = new LinkedHashMap<String, String>();
for(String str : arr) {
String[] elements = str.split(minorDelim);
if(elements.length == 2) {
String key = elements[0].trim();
String value = elements[1].trim();
if(!key.isEmpty() && !value.isEmpty()) {
output.put(key, value);
}
}
}
return output;
}
/**
* Extract value of given token from given line. It is assumed that the token, if present, will be of the following
* form: <code>token: value</code>
*
* @param line
* Line to be analyzed
* @param token
* Token whose value is to be extracted
* @return Value of the token, if present in the line
*/
public static String extractToken(String line, String token) {
if (line.contains(token)) {
return line.split(token)[1].trim();
}
return null;
}
}
|