Example usage for java.util LinkedList removeLast

List of usage examples for java.util LinkedList removeLast

Introduction

In this page you can find the example usage for java.util LinkedList removeLast.

Prototype

public E removeLast() 

Source Link

Document

Removes and returns the last element from this list.

Usage

From source file:org.apache.cocoon.util.NetUtils.java

/**
 * Normalize a uri containing ../ and ./ paths.
 *
 * @param uri The uri path to normalize/*from   w  ww . j  a v a 2  s . c o  m*/
 * @return The normalized uri
 */
public static String normalize(String uri) {
    if ("".equals(uri)) {
        return uri;
    }
    int leadingSlashes = 0;
    for (leadingSlashes = 0; leadingSlashes < uri.length()
            && uri.charAt(leadingSlashes) == '/'; ++leadingSlashes) {
    }
    boolean isDir = (uri.charAt(uri.length() - 1) == '/');
    StringTokenizer st = new StringTokenizer(uri, "/");
    LinkedList clean = new LinkedList();
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if ("..".equals(token)) {
            if (!clean.isEmpty() && !"..".equals(clean.getLast())) {
                clean.removeLast();
                if (!st.hasMoreTokens()) {
                    isDir = true;
                }
            } else {
                clean.add("..");
            }
        } else if (!".".equals(token) && !"".equals(token)) {
            clean.add(token);
        }
    }
    StringBuffer sb = new StringBuffer();
    while (leadingSlashes-- > 0) {
        sb.append('/');
    }
    for (Iterator it = clean.iterator(); it.hasNext();) {
        sb.append(it.next());
        if (it.hasNext()) {
            sb.append('/');
        }
    }
    if (isDir && sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') {
        sb.append('/');
    }
    return sb.toString();
}

From source file:org.rhq.enterprise.gui.legacy.util.SessionUtils.java

/**
 * Takes the current returnPath and pops it off of the workflow's stack.
 *
 * @param session      The HttpSesion to get and save the workflow to/from
 * @param workflowName The name of the workflow scope to save the input under.
 * @return return path or null if it can't be retrieved
 *//*from  www .j  a va  2  s  .com*/
public static String popWorkflow(HttpSession session, String workflowName) {
    HashMap workflows = (HashMap) session.getAttribute(AttrConstants.WORKFLOW_SES_ATTR);
    if (workflows == null) {
        return null;
    }

    LinkedList urlStack = (LinkedList) workflows.get(workflowName);
    if ((urlStack == null) || urlStack.isEmpty()) {
        return null;
    }

    String returnUrl = (String) urlStack.removeLast();
    workflows.put(workflowName, urlStack);
    session.setAttribute(AttrConstants.WORKFLOW_SES_ATTR, workflows);

    return returnUrl;
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

/**
 * Creates a {@link Template} that detects recursively calls.
 *
 * @param source The template source./*from ww w  .j a  va  2s. c  om*/
 * @param template The original template.
 * @return A new {@link Template} that detects recursively calls.
 */
private static Template infiniteLoop(final TemplateSource source, final BaseTemplate template) {
    return new ForwardingTemplate(template) {
        @Override
        protected void beforeApply(final Context context) {
            LinkedList<TemplateSource> invocationStack = context.data(Context.INVOCATION_STACK);
            invocationStack.addLast(source);
        }

        @Override
        protected void afterApply(final Context context) {
            LinkedList<TemplateSource> invocationStack = context.data(Context.INVOCATION_STACK);
            if (!invocationStack.isEmpty()) {
                invocationStack.removeLast();
            }
        }
    };
}

From source file:org.rhq.enterprise.gui.legacy.util.SessionUtils.java

/**
 * Set the "returnPath," the possible point of origin for a worklow
 *
 * @param session The http session.// ww  w.  ja  v a2 s .c  om
 * @param path    The return path url represented as a String.
 * @param ignore
 */
public static void setReturnPath(HttpSession session, String path, Boolean ignore) {
    LinkedList stack = (LinkedList) session.getAttribute(AttrConstants.RETURN_LOC_SES_ATTR);

    ReturnPath returnPath = new ReturnPath();

    if (stack == null) {
        stack = new LinkedList();
    }

    returnPath.setPath(path);
    returnPath.setIgnore(ignore);

    stack.addFirst(returnPath);

    // don't let it grow too large.
    if (stack.size() > RETURN_STACK_MAX_SIZE) {
        stack.removeLast();
    }

    session.setAttribute(AttrConstants.RETURN_LOC_SES_ATTR, stack);
}

From source file:com.zimbra.cs.imap.ImapMessage.java

static void serializeStructure(PrintStream ps, MimeMessage root, boolean extensions)
        throws IOException, MessagingException {
    LinkedList<LinkedList<MPartInfo>> queue = new LinkedList<LinkedList<MPartInfo>>();
    LinkedList<MPartInfo> level = new LinkedList<MPartInfo>();
    level.add(Mime.getParts(root).get(0));
    queue.add(level);//from  w w  w. ja  v  a 2  s .c o  m

    boolean pop = false;
    while (!queue.isEmpty()) {
        level = queue.getLast();
        if (level.isEmpty()) {
            queue.removeLast();
            pop = true;
            continue;
        }

        MPartInfo mpi = level.getFirst();
        MimePart mp = mpi.getMimePart();
        boolean hasChildren = mpi.getChildren() != null && !mpi.getChildren().isEmpty();

        // we used to force unset charsets on text/plain parts to US-ASCII, but that always seemed unwise...
        ContentType ctype = new ContentType(mp.getHeader("Content-Type", null))
                .setContentType(mpi.getContentType());
        String primary = nATOM(ctype.getPrimaryType()), subtype = nATOM(ctype.getSubType());

        if (!pop)
            ps.write('(');
        if (primary.equals("\"MULTIPART\"")) {
            if (!pop) {
                // 7.4.2: "Multiple parts are indicated by parenthesis nesting.  Instead of a body type
                //         as the first element of the parenthesized list, there is a sequence of one
                //         or more nested body structures.  The second element of the parenthesized
                //         list is the multipart subtype (mixed, digest, parallel, alternative, etc.)."
                if (!hasChildren) {
                    ps.print("NIL");
                } else {
                    queue.addLast(new LinkedList<MPartInfo>(mpi.getChildren()));
                    continue;
                }
            }
            ps.write(' ');
            ps.print(subtype);
            if (extensions) {
                // 7.4.2: "Extension data follows the multipart subtype.  Extension data is never
                //         returned with the BODY fetch, but can be returned with a BODYSTRUCTURE
                //         fetch.  Extension data, if present, MUST be in the defined order.  The
                //         extension data of a multipart body part are in the following order:
                //         body parameter parenthesized list, body disposition, body language,
                //         body location"
                ps.write(' ');
                nparams(ps, ctype);
                ps.write(' ');
                ndisposition(ps, mp.getHeader("Content-Disposition", null));
                ps.write(' ');
                nlist(ps, mp.getContentLanguage());
                ps.write(' ');
                nstring(ps, mp.getHeader("Content-Location", null));
            }
        } else {
            if (!pop) {
                // 7.4.2: "The basic fields of a non-multipart body part are in the following order:
                //         body type, body subtype, body parameter parenthesized list, body id, body
                //         description, body encoding, body size."
                String cte = mp.getEncoding();
                cte = (cte == null || cte.trim().equals("") ? "7bit" : cte);
                aSTRING(ps, ctype.getPrimaryType());
                ps.write(' ');
                aSTRING(ps, ctype.getSubType());
                ps.write(' ');
                nparams(ps, ctype);
                ps.write(' ');
                nstring(ps, mp.getContentID());
                ps.write(' ');
                nstring2047(ps, mp.getDescription());
                ps.write(' ');
                aSTRING(ps, cte);
                ps.write(' ');
                ps.print(Math.max(mp.getSize(), 0));
            }
            boolean rfc822 = primary.equals("\"MESSAGE\"") && subtype.equals("\"RFC822\"");
            if (rfc822) {
                // 7.4.2: "A body type of type MESSAGE and subtype RFC822 contains, immediately
                //         after the basic fields, the envelope structure, body structure, and
                //         size in text lines of the encapsulated message."
                if (!pop) {
                    if (!hasChildren) {
                        ps.print(" NIL NIL");
                    } else {
                        MimeMessage mm = (MimeMessage) mpi.getChildren().get(0).getMimePart();
                        ps.write(' ');
                        serializeEnvelope(ps, mm);
                        ps.write(' ');
                        queue.addLast(new LinkedList<MPartInfo>(mpi.getChildren()));
                        continue;
                    }
                }
                ps.write(' ');
                ps.print(getLineCount(mp));
            } else if (primary.equals("\"TEXT\"")) {
                // 7.4.2: "A body type of type TEXT contains, immediately after the basic fields, the
                //         size of the body in text lines.  Note that this size is the size in its
                //         content transfer encoding and not the resulting size after any decoding."
                ps.write(' ');
                ps.print(getLineCount(mp));
            }
            if (extensions) {
                // 7.4.2: "Extension data follows the basic fields and the type-specific fields
                //         listed above.  Extension data is never returned with the BODY fetch,
                //         but can be returned with a BODYSTRUCTURE fetch.  Extension data, if
                //         present, MUST be in the defined order.  The extension data of a
                //         non-multipart body part are in the following order: body MD5, body
                //         disposition, body language, body location"
                ps.write(' ');
                nstring(ps, mp.getContentMD5());
                ps.write(' ');
                ndisposition(ps, mp.getHeader("Content-Disposition", null));
                ps.write(' ');
                nlist(ps, mp.getContentLanguage());
                ps.write(' ');
                nstring(ps, mp.getHeader("Content-Location", null));
            }
        }
        ps.write(')');

        level.removeFirst();
        pop = false;
    }
}

From source file:org.commonjava.util.partyline.FileTreeTest.java

private File createStructure(String path, boolean writeTestFile) throws IOException {
    LinkedList<String> parts = new LinkedList<>(Arrays.asList(path.split("/")));
    String fname = parts.removeLast();

    File current = temp.newFolder();
    while (!parts.isEmpty()) {
        current = new File(current, parts.removeFirst());
    }// w w w.ja v  a2s.c  o  m

    current = new File(current, fname);
    if (writeTestFile) {
        current.getParentFile().mkdirs();
        FileUtils.write(current, "This is a test");
    } else {
        current.mkdirs();
    }

    return current;
}

From source file:savant.controller.RecentTracksController.java

private void resizeQueue(LinkedList queue, int size) {
    while (queue.size() > size) {
        queue.removeLast();
    }//w ww  .  j  a v a  2 s.co m
}

From source file:BasicPriorityLinkedList.java

public Object removeLast() {
    Object obj = null;//from w  w w  .ja v a2  s. co m

    // Initially we are just using a simple prioritization algorithm:
    // Lowest priority refs always get returned first.

    // TODO - A better prioritization algorithm

    for (int i = 0; i < priorities; i++) {
        LinkedList ll = linkedLists[i];
        if (!ll.isEmpty()) {
            obj = ll.removeLast();
        }
        if (obj != null) {
            break;
        }
    }

    if (obj != null) {
        size--;
    }

    return obj;
}

From source file:io.github.jeddict.jpa.spec.extend.cache.Cache.java

public void addClass(String _class, LinkedList<String> collection) {
    if (StringUtils.isEmpty(_class)) {
        return;//w w w .  j  a v  a  2s  . co m
    }
    if (collection.contains(_class)) {
        collection.remove(_class);
    }
    while (COLLECTION_SIZE < collection.size()) {
        collection.removeLast();
    }
    collection.addFirst(_class);
}

From source file:org.trend.hgraph.mapreduce.lib.input.TableInputFormat.java

private void checkAndPatchRegionEndKey(LinkedList<InputSplit> newSplits, byte[] regionEndKey) {
    if (Arrays.equals(HConstants.EMPTY_BYTE_ARRAY, regionEndKey)) {
        TableSplit tmpSplit = (TableSplit) newSplits.removeLast();
        newSplits.add(new TableSplit(tmpSplit.getTableName(), tmpSplit.getStartRow(), regionEndKey,
                tmpSplit.getLocations()[0]));
    }/* www.  java  2s.com*/
}