Example usage for java.lang StringBuilder delete

List of usage examples for java.lang StringBuilder delete

Introduction

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

Prototype

@Override
public StringBuilder delete(int start, int end) 

Source Link

Usage

From source file:Main.java

/**
 * Removes dot segments from the path of a URI.
 *
 * @param uri A {@link StringBuilder} containing the URI.
 * @param offset The index of the start of the path in {@code uri}.
 * @param limit The limit (exclusive) of the path in {@code uri}.
 *//*from  w  w  w.  jav a 2s.co  m*/
private static String removeDotSegments(StringBuilder uri, int offset, int limit) {
    if (offset >= limit) {
        // Nothing to do.
        return uri.toString();
    }
    if (uri.charAt(offset) == '/') {
        // If the path starts with a /, always retain it.
        offset++;
    }
    // The first character of the current path segment.
    int segmentStart = offset;
    int i = offset;
    while (i <= limit) {
        int nextSegmentStart = -1;
        if (i == limit) {
            nextSegmentStart = i;
        } else if (uri.charAt(i) == '/') {
            nextSegmentStart = i + 1;
        } else {
            i++;
            continue;
        }
        // We've encountered the end of a segment or the end of the path. If the final segment was
        // "." or "..", remove the appropriate segments of the path.
        if (i == segmentStart + 1 && uri.charAt(segmentStart) == '.') {
            // Given "abc/def/./ghi", remove "./" to get "abc/def/ghi".
            uri.delete(segmentStart, nextSegmentStart);
            limit -= nextSegmentStart - segmentStart;
            i = segmentStart;
        } else if (i == segmentStart + 2 && uri.charAt(segmentStart) == '.'
                && uri.charAt(segmentStart + 1) == '.') {
            // Given "abc/def/../ghi", remove "def/../" to get "abc/ghi".
            int prevSegmentStart = uri.lastIndexOf("/", segmentStart - 2) + 1;
            int removeFrom = prevSegmentStart > offset ? prevSegmentStart : offset;
            uri.delete(removeFrom, nextSegmentStart);
            limit -= nextSegmentStart - removeFrom;
            segmentStart = prevSegmentStart;
            i = prevSegmentStart;
        } else {
            i++;
            segmentStart = i;
        }
    }
    return uri.toString();
}

From source file:com.redhat.lightblue.assoc.QueryPlanTest.java

License:asdf

@Ignore
@Test/* w  ww  .j a va  2 s .  c  o  m*/
public void simple_StringBuilder_vs_String() {
    // intellij claimed in this case a String might be faster.. I felt like testing it.
    // note originally this created a new string builder each iteration.  it's as slow even this way..

    // net result? no appreciable difference for a single instance but scaled to many iterations string builder is
    // an order of magnitude slower.
    int count = 1000000;

    long timeStringBuilder = 0;
    {
        long before = System.currentTimeMillis();
        StringBuilder bld = new StringBuilder();
        for (int i = 0; i < count; i++) {
            bld.delete(0, 100);
            bld.append("asdf").append('_').append(1);
            String name = bld.toString();
        }
        long after = System.currentTimeMillis();

        timeStringBuilder = after - before;
    }

    long timeString = 0;
    {
        long before = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            String name = "asdf" + '_' + 1;
        }
        long after = System.currentTimeMillis();

        timeString = after - before;
    }

    Assert.assertTrue(timeString < timeStringBuilder);
}

From source file:org.tonguetied.web.DirectoryViewController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
    final StringBuilder servletPath = new StringBuilder(request.getServletPath());
    if (logger.isDebugEnabled())
        logger.debug("servletPath = " + servletPath);
    if (servletPath.toString().endsWith(suffix))
        servletPath.delete(servletPath.length() - suffix.length(), servletPath.length());

    final String homePath = request.getSession().getServletContext().getRealPath(servletPath.toString());
    if (logger.isDebugEnabled())
        logger.debug("homePath = " + homePath);

    final FileBean baseDirectory = new FileBean(homePath);
    if (logger.isInfoEnabled())
        logger.info("displaying contents of basedir = " + baseDirectory);
    final int lastIndex = servletPath.lastIndexOf("/");
    final String[] parents;
    if (lastIndex > 0)
        parents = servletPath.substring(1, lastIndex).split("/");
    else/*  w w w .j  a  v  a 2s  .  com*/
        parents = new String[] {};

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("baseDir", baseDirectory);
    model.put("suffix", suffix);
    model.put("parents", parents);

    return new ModelAndView("export/fileListing", model);
}

From source file:com.github.ferstl.depgraph.dot.AttributeBuilder.java

@Override
public String toString() {
    if (this.attributes.isEmpty()) {
        return "";
    }//from ww  w  .j  a va 2s  .  com

    StringBuilder sb = new StringBuilder("[");
    for (Entry<String, String> attribute : this.attributes.entrySet()) {
        sb.append(attribute.getKey() + "=" + attribute.getValue()).append(",");
    }

    return sb.delete(sb.length() - 1, sb.length()).append("]").toString();
}

From source file:de.d3web.empiricaltesting.casevisualization.jung.EdgeTransformer.java

/**
 * Transforms a Finding to nice formatted String which is necessary for
 * rendering./*from  w  w w .  j a  va2  s .com*/
 */
@Override
public String transform(EdgeFinding ef) {

    StringBuilder result = new StringBuilder();
    result.append("<html><center>");
    for (Finding f : graph.getDest(ef).getFindings()) {
        result.append(f.getValue().getValue().toString());
        result.append("<br/>");
    }
    result.delete(result.length() - 4, result.length());
    result.append("</center></html>");

    return result.toString();
}

From source file:com.github.ferstl.depgraph.graph.dot.DotAttributeBuilder.java

@Override
public String toString() {
    if (this.attributes.isEmpty()) {
        return "";
    }// w  w w .  ja  v a 2s.c om

    StringBuilder sb = new StringBuilder("[");
    for (Entry<String, String> attribute : this.attributes.entrySet()) {
        sb.append(attribute.getKey()).append("=").append(attribute.getValue()).append(",");
    }

    return sb.delete(sb.length() - 1, sb.length()).append("]").toString();
}

From source file:org.mule.module.dynamicFlows.DynamicFlowsModule.java

private String join(List<String> coll) {
    StringBuilder sb = new StringBuilder();

    for (String x : coll)
        sb.append(x + ",");

    sb.delete(sb.length() - 1, sb.length());

    return sb.toString();
}

From source file:org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException.java

@Override
public String getMessage() {
    StringBuilder sb = new StringBuilder();

    for (Class<?> entity : violations.keySet()) {
        sb.append(entity.getSimpleName()).append(' ').append(violations.get(entity).toString()).append(", ");
    }/* ww  w.j  av a 2 s .  c  o m*/
    sb.delete(sb.lastIndexOf(", "), sb.length());

    return sb.toString();
}

From source file:eu.operando.operandoapp.util.NotificationUtil.java

public void displayExfiltratedNotification(Context context, String applicationInfo,
        Set<RequestFilterUtil.FilterType> exfiltrated, int mainNotificationId) {
    try {/*www. j a  v  a2s . c  o m*/
        StringBuilder sb = new StringBuilder();
        for (RequestFilterUtil.FilterType f : exfiltrated) {
            sb.append(f + ", ");
        }
        sb.delete(sb.length() - 2, sb.length() - 1);

        RemoteViews smallContentView = new RemoteViews(context.getPackageName(),
                R.layout.proxy_notification_small);
        smallContentView.setImageViewResource(R.id.image, R.drawable.logo_bevel);
        smallContentView.setTextViewText(R.id.titleTxtView, "Personal Information");
        smallContentView.setTextViewText(R.id.subtitleTxtView, "Expand for more info");

        RemoteViews bigContentView = new RemoteViews(context.getPackageName(),
                R.layout.proxy_notification_large);
        bigContentView.setImageViewResource(R.id.image, R.drawable.logo_bevel);
        bigContentView.setTextViewText(R.id.titleTxtView, "Personal Information");
        bigContentView.setTextViewText(R.id.subtitleTxtView,
                applicationInfo.replaceAll("\\s\\(.*?\\)", "") + " requires access to " + sb);
        bigContentView.setTextViewText(R.id.allowBtn, "Allow");
        bigContentView.setTextViewText(R.id.blockBtn, "Block");

        //get exfiltrated info to string array
        String[] exfiltrated_array = new String[exfiltrated.size()];
        int i = 0;
        for (RequestFilterUtil.FilterType filter_type : exfiltrated) {
            exfiltrated_array[i] = filter_type.name();
            i++;
        }

        //set listeners for notification buttons

        Intent allowIntent = new Intent(context, NotificationActivityReceiver.class);
        allowIntent.setAction("allow");
        Bundle allowBundle = new Bundle();
        allowBundle.putString("appInfo", applicationInfo);
        allowBundle.putInt("notificationId", mainNotificationId);
        allowBundle.putStringArray("exfiltrated", exfiltrated_array);
        allowIntent.putExtras(allowBundle);
        PendingIntent pendingAllowIntent = PendingIntent.getBroadcast(context, mainNotificationId + 1,
                allowIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        bigContentView.setOnClickPendingIntent(R.id.allowBtn, pendingAllowIntent);

        Intent blockIntent = new Intent(context, NotificationActivityReceiver.class);
        blockIntent.setAction("block");
        Bundle blockBundle = new Bundle();
        blockBundle.putString("appInfo", applicationInfo);
        blockBundle.putInt("notificationId", mainNotificationId);
        blockBundle.putStringArray("exfiltrated", exfiltrated_array);
        blockIntent.putExtras(blockBundle);
        PendingIntent pendingBlockIntent = PendingIntent.getBroadcast(context, mainNotificationId + 2,
                blockIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        bigContentView.setOnClickPendingIntent(R.id.blockBtn, pendingBlockIntent);

        Notification.Builder mBuilder = new Notification.Builder(context).setSmallIcon(R.drawable.logo_bevel)
                .setContent(smallContentView);
        Notification proxyNotification = mBuilder.build();
        proxyNotification.defaults |= Notification.DEFAULT_ALL;
        proxyNotification.bigContentView = bigContentView;

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(mainNotificationId, proxyNotification);
        DatabaseHelper db = new DatabaseHelper(context);
        db.addPendingNotification(new PendingNotification(applicationInfo,
                TextUtils.join(", ", exfiltrated_array), mainNotificationId));
    } catch (Exception e) {
        Log.d("ERROR", e.getMessage());
    }

}

From source file:gov.nih.nci.cabig.caaers.domain.AdverseEventMeddraLowLevelTerm.java

@Transient
public String getUniversalTerm() {
    if (getTerm() == null) {
        return null;
    } else if (getAdverseEvent() != null && (getAdverseEvent().getDetailsForOther() != null)) {
        StringBuilder sb = new StringBuilder(getTerm().getFullName());
        // strip everything after "Other", if it is in the name
        int otherAt = sb.indexOf("Other");
        if (otherAt >= 0) {
            sb.delete(otherAt + 5, sb.length());
        }/*w w w .java  2 s.c o  m*/
        if (getAdverseEvent().getDetailsForOther() != null)
            sb.append(": ").append(getAdverseEvent().getDetailsForOther());
        return sb.toString();
    } else {
        return getTerm().getFullName();
    }
}