From 1142b0e41de39010de7845cf70d71dbb001fc1dc Mon Sep 17 00:00:00 2001 From: Shireesh Anjal Date: Fri, 25 Nov 2011 20:13:35 +0530 Subject: Renamed projects / packages com.gluster.* to org.gluster.* --- .../storage/management/core/utils/StringUtil.java | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/StringUtil.java (limited to 'src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/StringUtil.java') diff --git a/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/StringUtil.java b/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/StringUtil.java new file mode 100644 index 00000000..b3b0a4b5 --- /dev/null +++ b/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/StringUtil.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * Copyright (c) 2011 Gluster, Inc. + * 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 Affero 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 Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * . + *******************************************************************************/ +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 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 > List enumToArray(T[] values) { + List enumAsArray = new ArrayList(); + 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 extractList(String input, String delim) { + String[] arr = input.split(delim); + List output = new ArrayList(); + 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 + * k1=v1,k2=v2,k3=v3 will yield the following map:
+ * k1 -> v1
+ * k2 -> v2
+ * k3 -> v3
+ * where , is the primary delimiter and = is the secondary delimiter. + * + * @param input + * @param majorDelim + * @param minorDelim + * @return Map of key value pairs + */ + public static Map extractMap(String input, String majorDelim, String minorDelim) { + String[] arr = input.split(majorDelim); + Map output = new LinkedHashMap(); + 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: token: value + * + * @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; + } +} -- cgit