Example usage for java.lang StringBuilder length

List of usage examples for java.lang StringBuilder length

Introduction

In this page you can find the example usage for java.lang StringBuilder length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:Main.java

/**
 * Transforms a collection of Integers into a comma delimited String. If the
 * given collection of elements are null or is empty, an empty String is
 * returned.//from  www.  j  av a 2s  .c  o  m
 *
 * @param elements the collection of Integers
 * @return a comma delimited String.
 */
public static String getCommaDelimitedString(Collection<?> elements) {
    final StringBuilder builder = new StringBuilder();

    if (elements != null && !elements.isEmpty()) {
        for (Object element : elements) {
            builder.append(element.toString()).append(DELIMITER);
        }

        return builder.substring(0, builder.length() - DELIMITER.length());
    }

    return builder.toString();
}

From source file:com.ebay.cloud.cms.service.resources.impl.CMSResourceUtils.java

private static String joinSortOrder(List<SortOrder> sortOrder) {
    StringBuilder sb = new StringBuilder();
    for (SortOrder o : sortOrder) {
        sb.append(o.toString()).append(',');
    }//from  ww w .  j av  a  2  s . c om
    if (sb.length() > 0) {
        sb.setLength(sb.length() - 1);
    }
    return sb.toString();
}

From source file:de.fhg.fokus.odp.portal.managedatasets.utils.HashMapUtils.java

/**
 * Map to meta data./*from  w  ww. j a v a2s.  c o m*/
 * 
 * @param map
 *            the map
 * @return the meta data bean
 * @throws ParseException
 *             the parse exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static MetaDataBean mapToMetaData(Map map) throws ParseException {
    log.debug("HashMap to convert to MetaDataBean: " + map.toString());
    MetaDataBean metaData = new MetaDataBean();
    metaData.setTitle((String) map.get("title"));
    metaData.setName((String) map.get("name"));
    metaData.setAuthor((String) map.get("author"));
    metaData.setAuthor_email((String) map.get("author_email"));
    metaData.setMaintainer((String) map.get("maintainer"));
    metaData.setMaintainer_email((String) map.get("maintainer_email"));
    metaData.setUrl((String) map.get("url"));
    metaData.setNotes((String) map.get("notes"));
    metaData.setLicense_id((String) map.get("license_id"));
    metaData.setCkanId((String) map.get("id"));
    metaData.setMetadata_created(DateUtils.stringToDateMetaData((String) map.get("metadata_created")));
    metaData.setMetadata_modified(DateUtils.stringToDateMetaData((String) map.get("metadata_modified")));
    Map<String, String> extrasMap = new HashMap<String, String>();
    extrasMap = (Map) map.get("extras");
    metaData.setDate_released(DateUtils.stringToDateTemporalCoverage(extrasMap.get("date_released")));
    metaData.setDate_updated(DateUtils.stringToDateTemporalCoverage(extrasMap.get("date_updated")));
    metaData.setTemporal_coverage_from(extrasMap.get("temporal_coverage_from"));
    metaData.setTemporal_coverage_to(extrasMap.get("temporal_coverage_to"));
    metaData.setTemporal_granularity(extrasMap.get("temporal_granularity"));
    metaData.setGeographical_coverage(extrasMap.get("geographical_coverage"));
    metaData.setGeographical_granularity(extrasMap.get("geographical_granularity"));
    metaData.setOthers(extrasMap.get("others"));
    metaData.setGroups((List<String>) map.get("groups"));
    StringBuilder tagsBuilder = new StringBuilder();
    for (String tag : (List<String>) map.get("tags")) {
        tag.trim();
        tagsBuilder.append(tag).append(",");
    }
    if (tagsBuilder.length() > 0) {
        tagsBuilder = tagsBuilder.deleteCharAt(tagsBuilder.length() - 1);
    }
    metaData.setTags(tagsBuilder.toString());
    metaData.setVersion((String) map.get("version"));

    List<Map<String, String>> resorcesList;
    resorcesList = (List) map.get("resources");

    for (Map<String, String> resMap : resorcesList) {
        Resource resource = new Resource();

        resource.setFormat(resMap.get("format").toUpperCase());
        resource.setDescription(resMap.get("description"));
        resource.setUrl(resMap.get("url"));
        resource.setLanguage(resMap.get("language"));
        metaData.getResources().add(resource);
    }

    return metaData;
}

From source file:ch.digitalfondue.jfiveparse.TreeConstructionTest.java

static String renderNodes(List<Node> nodes) {
    StringBuilder sb = new StringBuilder();
    for (Node node : nodes) {
        renderNode(node, 0, sb);/*  w w  w .ja  va  2 s. c  o m*/
    }
    return sb.deleteCharAt(sb.length() - 1).toString();
}

From source file:cn.edu.zju.acm.onlinejudge.persistence.sql.Database.java

/**
 * Create a string containing given number values. The string is like '(value1, value2, value3)'
 * /*from  www  .  j a  v a  2 s . co m*/
 * @param values
 *            the values
 * @return a string like '(value1, value2, value3)'
 */
public static String createNumberValues(Collection<Long> values) {
    StringBuilder ret = new StringBuilder();
    ret.append('(');
    for (Long value : values) {
        if (ret.length() > 1) {
            ret.append(',');
        }
        ret.append(value);
    }
    ret.append(')');
    return ret.toString();
}

From source file:com.DPFaragir.DPFUtils.java

public static String getMACAddress(String interfaceName) {
    try {/* w  w  w  . ja  v  a2 s .c o  m*/
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++)
                buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
    /*try {
    // this is so Linux hack
    return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
    } catch (IOException ex) {
    return null;
    }*/
}

From source file:biblivre3.administration.reports.ReportUtils.java

public static String formatDeweyString(final String dewey, final int digits) {
    if (StringUtils.isBlank(dewey)) {
        return "";
    }//from w ww . j a va 2  s  . co m

    if (digits == -1) {
        return dewey;
    }

    int i = digits;

    StringBuilder format = new StringBuilder();
    for (char c : dewey.toCharArray()) {
        if (i == 0) {
            break;
        }

        if (Character.isDigit(c)) {
            i--;
        }

        format.append(c);
    }

    if (digits < 3) {
        while (format.length() < 3) {
            format.append("0");
        }
    }

    return format.toString();
}

From source file:cn.edu.zju.acm.onlinejudge.persistence.sql.Database.java

/**
 * Create a string containing given values. The string is like '(value1, value2, value3)'
 * //from  ww  w  .  j  a va  2 s. c  o  m
 * @param values
 *            the values
 * @return a string like '(value1, value2, value3)'
 */
public static String createValues(Collection<String> values) {
    StringBuilder ret = new StringBuilder();
    ret.append('(');
    for (String value : values) {
        if (ret.length() > 1) {
            ret.append(',');
        }
        ret.append('\'').append(value.replaceAll("'", "''")).append('\'');
    }
    ret.append(')');
    return ret.toString();
}

From source file:com.norconex.commons.lang.time.DurationUtil.java

private static String format(Locale locale, long duration, boolean islong, int maxUnits) {
    int max = Integer.MAX_VALUE;
    if (maxUnits > 0) {
        max = maxUnits;//  www  .  java 2 s .co m
    }
    long days = duration / DateUtils.MILLIS_PER_DAY;
    long accountedMillis = days * DateUtils.MILLIS_PER_DAY;
    long hours = (duration - accountedMillis) / DateUtils.MILLIS_PER_HOUR;
    accountedMillis += (hours * DateUtils.MILLIS_PER_HOUR);
    long mins = (duration - accountedMillis) / DateUtils.MILLIS_PER_MINUTE;
    accountedMillis += (mins * DateUtils.MILLIS_PER_MINUTE);
    long secs = (duration - accountedMillis) / DateUtils.MILLIS_PER_SECOND;
    StringBuilder b = new StringBuilder();
    int unitCount = 0;
    // days
    if (days > 0) {
        b.append(getString(locale, days, "day", islong));
        unitCount++;
    }
    // hours
    if ((hours > 0 || b.length() > 0) && unitCount < max) {
        b.append(getString(locale, hours, "hour", islong));
        unitCount++;
    }
    // minutes
    if ((mins > 0 || b.length() > 0) && unitCount < max) {
        b.append(getString(locale, mins, "minute", islong));
        unitCount++;
    }
    // seconds
    if (unitCount < max) {
        b.append(getString(locale, secs, "second", islong));
    }
    return b.toString().trim();
}

From source file:it.jnrpe.server.JNRPEServer.java

/**
 * Prints the JNRPE Server usage and, eventually, the error about the last
 * invocation.//from  w w  w  .j a v a 2s . c  o  m
 * 
 * @param e
 *            The last error. Can be null.
 */
@SuppressWarnings("unchecked")
private static void printUsage(final Exception e) {
    printVersion();
    if (e != null) {
        System.out.println(e.getMessage() + "\n");
    }

    HelpFormatter hf = new HelpFormatter();

    StringBuilder sbDivider = new StringBuilder("=");
    while (sbDivider.length() < hf.getPageWidth()) {
        sbDivider.append("=");
    }

    // DISPLAY SETTING
    hf.getDisplaySettings().clear();
    hf.getDisplaySettings().add(DisplaySetting.DISPLAY_GROUP_EXPANDED);
    hf.getDisplaySettings().add(DisplaySetting.DISPLAY_PARENT_CHILDREN);

    // USAGE SETTING

    hf.getFullUsageSettings().clear();
    hf.getFullUsageSettings().add(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
    hf.getFullUsageSettings().add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
    hf.getFullUsageSettings().add(DisplaySetting.DISPLAY_PARENT_CHILDREN);
    hf.getFullUsageSettings().add(DisplaySetting.DISPLAY_GROUP_EXPANDED);

    hf.setDivider(sbDivider.toString());

    hf.setGroup(configureCommandLine());
    hf.print();
    System.exit(0);
}