public static boolean isNotBlank(String s) { return !isBlank(s); }
/** * Print out an array as a String */ public static String arrayToString(Object[] array) { return arrayToString(array, ','); }
public static String collectionToString(Collection<?> collection) { return collectionToString(collection, ","); }
/** * Split a string on delimiter boundaries, and place each element into an Array. * * @param toExplode String to split up * @param delim Delimiting token, ala StringTokenizer * * @return an Array comprised of elements split by the tokenizing */ public static String[] explodeToArray(String toExplode, String delim) { List<String> strings = explode(toExplode, delim); String[] ret; ret = strings.toArray(new String[strings.size()]); return ret; }
private String getProductVersionInFile(File file, String versionPrefix, JBossProductType productType) { if (!file.exists() || file.isDirectory()) { return null; } try { String versionLine = FileUtils.findString(file.getAbsolutePath(), versionPrefix); if (isNotBlank(versionLine)) { return productType.SHORT_NAME + " " + versionLine.substring(versionLine.lastIndexOf(versionPrefix) + versionPrefix.length()); } } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Could not read file " + file.getAbsolutePath(), e); } } return null; }
/** * Print out a List in a user-friendly string format. * * @param list A List to print out. * * @return The List in a user-friendly string format. */ public static String listToString(List<?> list) { return listToString(list, ","); }
public static boolean isNotEmpty(String s) { return !isEmpty(s); }
/** * @return The stack trace for the given Throwable as a String. */ public static String getStackTrace(Throwable t) { if (t == null) { return "THROWABLE-WAS-NULL (at " + getStackTrace(new Exception()) + ")"; } try { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); Throwable cause = t.getCause(); if (cause != null) { return sw.toString() + getStackTrace(cause); } return sw.toString(); } catch (Exception e) { return "\n\nStringUtil.getStackTrace " + "GENERATED EXCEPTION: '" + e.toString() + "' \n\n"; } }
public static String formatDuration(long duration, int scale, boolean minDigits) { long hours; long mins; int digits; double millis; hours = duration / 3600000; duration -= hours * 3600000; mins = duration / 60000; duration -= mins * 60000; millis = (double) duration / 1000; StringBuilder buf = new StringBuilder(); if ((hours > 0) || (minDigits == false)) { buf.append(((hours < 10) && (minDigits == false)) ? ("0" + hours) : String.valueOf(hours)).append(':'); minDigits = false; } if ((mins > 0) || (minDigits == false)) { buf.append(((mins < 10) && (minDigits == false)) ? ("0" + mins) : String.valueOf(mins)).append(':'); minDigits = false; } // Format seconds and milliseconds NumberFormat fmt = NumberFormat.getInstance(); digits = (((minDigits == false) || ((scale == 0) && (millis >= 9.5))) ? 2 : 1); fmt.setMinimumIntegerDigits(digits); fmt.setMaximumIntegerDigits(2); // Max of 2 fmt.setMinimumFractionDigits(0); // Don't need any fmt.setMaximumFractionDigits(scale); buf.append(fmt.format(millis)); return buf.toString(); }
/** * Tokenize the named preference into a List of Strings. If no such preference exists, or the preference is null, * an empty List will be returned. * * @param delimiter the delimiter to break it up by * @param key the name of the preference * @return <code>List</code> of <code>String</code> tokens */ public List<String> getPreferenceAsList(String key, String delimiter) { String pref = null; try { pref = getPreference(key); } catch (IllegalArgumentException e) { log.debug("A user preference named '" + key + "' does not exist."); } return (pref != null) ? StringUtil.explode(pref, delimiter) : new ArrayList<String>(); }
@Override boolean isConnectorPropertyDefined() { PropertyMap connector = configuration.getMap(CONNECTOR_PROPERTY); if (connector != null) { return isNotBlank(connector.getSimpleValue("name:0", EMPTY_STRING)); } return false; }
protected void setPreference(String key, List<?> values, String delim) throws IllegalArgumentException { String stringified = StringUtil.listToString(values, delim); setPreference(key, stringified); }
if (StringUtil.isEmpty(resourceTypeId)) {
result.setErrorStackTrace(StringUtil.getStackTrace(e));
private FormattedNumber format(BigDecimal baseTime, int granularity, int milliDigits, Locale locale) { TimeBreakDown tbd; String res; // We work in a few different time ranges depending on the // magnitude of the duration. Quite hardcoded if (granularity == GRANULAR_YEARS) { tbd = this.breakDownTime(baseTime); res = tbd.nYears + "y " + tbd.nDays + "d"; } else if (granularity == GRANULAR_DAYS) { long nDays; tbd = this.breakDownTime(baseTime); nDays = (tbd.nYears * 365) + tbd.nDays; res = nDays + ((nDays == 1) ? " day " : " days ") + StringUtil.formatDuration((tbd.nHours * 60 * 60 * 1000) + (tbd.nMins * 60 * 1000) + (tbd.nSecs * 1000) + tbd.nMilli); } else if ((granularity == GRANULAR_HOURS) || (granularity == GRANULAR_MINS) || (granularity == GRANULAR_SECS)) { long nMillis; nMillis = baseTime.divide(UnitsUtil.FACT_MILLIS, BigDecimal.ROUND_HALF_EVEN).longValue(); res = StringUtil.formatDuration(nMillis, milliDigits, granularity == GRANULAR_SECS); if (granularity == GRANULAR_SECS) { res = res + 's'; } } else { throw new IllegalStateException("Unexpected granularity"); } return new FormattedNumber(res.trim(), ""); }
private String getManagedServerNameFromPathAttribute(String nodePath) { for (StringTokenizer tokenizer = new StringTokenizer(nodePath, PATH_DELIM); tokenizer.hasMoreTokens();) { String token = tokenizer.nextToken(); if (token.startsWith(SERVER_PREFIX)) { String managedServerName = token.substring(SERVER_PREFIX.length()); if (StringUtil.isBlank(managedServerName)) { throw new RuntimeException("Blank managed server name in path [" + nodePath + "]"); } return managedServerName; } } throw new RuntimeException("Could not determine managed server name from path [" + nodePath + "]"); } }
private String fullErrorMessage(String basicMessage, List<String> patchIds, int attemptedPatchIdIndex, String action) { String message = basicMessage; if (attemptedPatchIdIndex > 0) { message += " The following patches were successfully " + action + ": " + StringUtil.collectionToString( patchIds.subList(0, attemptedPatchIdIndex)) + "."; } if (attemptedPatchIdIndex < patchIds.size() - 1) { message += " The following patches were NOT " + action + ": " + StringUtil .collectionToString(patchIds.subList(attemptedPatchIdIndex + 1, patchIds.size())) + "."; } return message; }
public MetricViewData getMetricViewData(String context, String viewName) { //TODO: jmarques - externalize default view name // instead of PREF_MEASUREMENT_INDICATOR_VIEW_DEFAULT_NAME // lookup PREF_MEASUREMENT_INDICATOR_VIEW_DEFAULT from the bundle if (viewName == null || "".equals(viewName)) { viewName = "default"; } MetricViewData chartPreferences = new MetricViewData(); // important to let IllegalArgumentException bubble out of here, so the caller can persist the default set String data = getPreference(PREF_MEASUREMENT_INDICATOR_VIEW_PREFIX + context + "." + viewName); chartPreferences.charts = StringUtil.explode(data, PREF_ITEM_DELIM); return chartPreferences; }
/** * Test if #toCheck matches one of the allowedValues and throw a BadArgumentException * if not. In this case the attribute name is passed in the exception * * * @param attributeName Name of the Attribute in error * @param toCheck Value to check for * @param allowedValues Allowed values * @throws BadArgumentException if the values to check does not match any of the allowed values */ private void checkForAllowedValues(String attributeName, String toCheck, String... allowedValues) { if (toCheck==null) { throw new BadArgumentException("Field " + attributeName + " must be set. Allowed values are: " + StringUtil.arrayToString(allowedValues)); } if (allowedValues==null) { throw new IllegalArgumentException("No allowed values are provided - please contact support"); } boolean match = false; for (String value : allowedValues) { if (toCheck.equals(value)) match=true; } if (!match) { throw new BadArgumentException("Field " + attributeName + " has an invalid value [" + toCheck + "]. Allowed values are: " + StringUtil.arrayToString(allowedValues)); } }
ConfigurationUpdateHelper(Configuration configuration) { this.configuration = configuration; hasDiscoveryGroupName = isNotBlank(this.configuration.getSimpleValue(DISCOVERY_GROUP_NAME)); hasConnector = isConnectorPropertyDefined(); hasNoneOfTheAlternatives = hasDiscoveryGroupName == hasConnector && hasConnector == false; errorMessage = getBeginningOfErrorMessage() + (hasNoneOfTheAlternatives ? " You provided none." : " You provided both."); }