Example usage for java.util Stack pop

List of usage examples for java.util Stack pop

Introduction

In this page you can find the example usage for java.util Stack pop.

Prototype

public synchronized E pop() 

Source Link

Document

Removes the object at the top of this stack and returns that object as the value of this function.

Usage

From source file:Main.java

public static void popPreviousFragmentFromStack(FragmentManager fragmentManager,
        Stack<Fragment> fragmentStack) {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    fragmentStack.lastElement().onPause();
    ft.remove(fragmentStack.pop());
    fragmentStack.lastElement().onResume();
    ft.show(fragmentStack.lastElement());
    ft.commit();/*from w  ww  .  j a  v a  2s  .com*/
}

From source file:Main.java

/**
 * Finds element in DOM tree/*from   ww w.j  a  v  a  2s . co m*/
 * @param topElm Top element
 * @param nodeName Node name
 * @return returns found node
 */
public static List<Node> findNodesByType(Element topElm, int type) {
    List<Node> retvals = new ArrayList<Node>();
    if (topElm == null)
        throw new IllegalArgumentException("topElm cannot be null");
    synchronized (topElm.getOwnerDocument()) {
        Stack<Node> stack = new Stack<Node>();
        stack.push(topElm);
        while (!stack.isEmpty()) {
            Node curElm = stack.pop();
            if (curElm.getNodeType() == type) {
                retvals.add(curElm);
            }
            List<Node> nodesToProcess = new ArrayList<Node>();

            NodeList childNodes = curElm.getChildNodes();
            for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
                Node item = childNodes.item(i);
                //stack.push((Element) item);
                nodesToProcess.add(item);
            }
            Collections.reverse(nodesToProcess);
            for (Node node : nodesToProcess) {
                stack.push(node);
            }
        }
        return retvals;
    }
}

From source file:Main.java

/**
 0: number of files/*  ww  w  .  ja va 2s.co  m*/
 1: total length
 2: number of directories
 */
public static void getDirSize(final File f, final long[] l) {
    if (f.isFile()) {
        l[0]++;
        l[1] += f.length();
    } else {
        Stack<File> stk = new Stack<>();
        stk.add(f);
        File fi = null;
        File[] fs;
        while (stk.size() > 0) {
            fi = stk.pop();
            fs = fi.listFiles();
            for (File f2 : fs) {
                if (f2.isDirectory()) {
                    stk.push(f2);
                    l[2]++;
                } else {
                    l[0]++;
                    l[1] += f2.length();
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Find element.//ww  w .ja  va2  s  .co m
 * 
 * @param topElm
 *        the top elm
 * @param localName
 *        the local name
 * @param namespace
 *        the namespace
 * @return the element
 */
public static Element findElement(Element topElm, String localName, String namespace) {
    Stack<Element> stack = new Stack<Element>();
    stack.push(topElm);
    while (!stack.isEmpty()) {
        Element curElm = stack.pop();
        if ((curElm.getLocalName().equals(localName))
                && (namespacesAreSame(curElm.getNamespaceURI(), namespace))) {
            return curElm;
        }
        NodeList childNodes = curElm.getChildNodes();
        for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
            Node item = childNodes.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                stack.push((Element) item);
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Sorts objects in the given collection into different types by Class.
 * @param <E> the element type/*w w  w. j  a v  a  2  s .  co  m*/
 * @param objects a Collection of objects
 * @param inherit if true, objects are put into all their class's superclasses as well, except
 * Object - the original Collection can be used in that case
 * @return a Map from Class to List of objects in that class
 */
public static <E> Map<Class<?>, List<E>> groupByClass(Collection<E> objects, boolean inherit) {
    Map<Class<?>, List<E>> retval = new HashMap<Class<?>, List<E>>();
    for (E o : objects) {
        Class<?> c = o.getClass();
        if (inherit) {
            Set<Class<?>> done = new HashSet<Class<?>>();
            done.add(Object.class);
            Stack<Class<?>> todo = new Stack<Class<?>>();
            todo.push(c);
            while (!todo.empty()) {
                c = todo.pop();
                if ((c != null) && !done.contains(c)) {
                    done.add(c);
                    List<E> l = retval.get(c);
                    if (l == null) {
                        l = new ArrayList<E>();
                        retval.put(c, l);
                    }
                    l.add(o);
                    todo.push(c.getSuperclass());
                    Class<?>[] classes = c.getInterfaces();
                    for (int i = 0; i < classes.length; i++) {
                        todo.push(classes[i]);
                    }
                }
            }
        } else {
            List<E> l = retval.get(c);
            if (l == null) {
                l = new ArrayList<E>();
                retval.put(c, l);
            }
            l.add(o);
        }
    }
    return retval;
}

From source file:Main.java

public static void sortingByFastStack(int[] intArray, boolean ascending) {
    Stack<Integer> sa = new Stack<Integer>();
    sa.push(0);/*  w w  w .jav  a  2 s .com*/
    sa.push(intArray.length - 1);
    while (!sa.isEmpty()) {
        int end = ((Integer) sa.pop()).intValue();
        int start = ((Integer) sa.pop()).intValue();
        int i = start;
        int j = end;
        int tmp = intArray[i];
        if (ascending) {
            while (i < j) {
                while (intArray[j] > tmp && i < j) {
                    j--;
                }
                if (i < j) {
                    intArray[i] = intArray[j];
                    i++;
                }
                for (; intArray[i] < tmp && i < j; i++) {
                    ;
                }
                if (i < j) {
                    intArray[j] = intArray[i];
                    j--;
                }
            }
        } else {
            while (i < j) {
                while (intArray[j] < tmp && i < j) {
                    j--;
                }
                if (i < j) {
                    intArray[i] = intArray[j];
                    i++;
                }
                for (; intArray[i] > tmp && i < j; i++) {
                    ;
                }
                if (i < j) {
                    intArray[j] = intArray[i];
                    j--;
                }
            }
        }

        intArray[i] = tmp;
        if (start < i - 1) {
            sa.push(Integer.valueOf(start));
            sa.push(Integer.valueOf(i - 1));
        }
        if (end > i + 1) {
            sa.push(Integer.valueOf(i + 1));
            sa.push(Integer.valueOf(end));
        }
    }
}

From source file:com.intuit.tank.harness.functions.GenericFunctions.java

/**
 * //from ww w  .  j  a v  a2s. com
 * @param startSsn
 * @return
 */
public static String getSsn(String val) {
    int ret = 0;
    if (lastSSN < 0) {
        int startSsn = 100000000;
        if (NumberUtils.isDigits(val)) {
            startSsn = Integer.parseInt(val);
        }
        lastSSN = startSsn;
    }
    Stack<Integer> stack = StringFunctions.getStack(lastSSN, 899999999, null, false);
    do {
        ret = stack.pop();
    } while (!isValidSsn(ret));

    String ssnString = Integer.toString(ret);
    StringUtils.leftPad(ssnString, 9, '0');

    ssnString = ssnString.substring(0, 3) + "-" + ssnString.substring(3, 5) + "-" + ssnString.substring(5, 9);
    return ssnString;
}

From source file:Main.java

public static boolean isBalanced(String in) {
    Stack<Character> st = new Stack<Character>();

    for (char chr : in.toCharArray()) {
        switch (chr) {
        case '{':
        case '(':
        case '[':
            st.push(chr);/*from ww  w  . ja v  a2  s.co m*/
            break;
        case ']':
            if (st.isEmpty() || st.pop() != '[')
                return false;
            break;
        case ')':
            if (st.isEmpty() || st.pop() != '(')
                return false;
            break;
        case '}':
            if (st.isEmpty() || st.pop() != '{')
                return false;
            break;
        }
    }
    return st.isEmpty();
}

From source file:org.lightjason.agentspeak.action.buildin.rest.IBaseRest.java

/**
 * creates a literal structure from a stream of string elements,
 * the string stream will be build in a tree structure
 *
 * @param p_functor stream with functor strings
 * @param p_values value stream/*  w w w. jav a  2 s.com*/
 * @return term
 */
protected static ITerm baseliteral(final Stream<String> p_functor, final Stream<ITerm> p_values) {
    final Stack<String> l_tree = p_functor.collect(Collectors.toCollection(Stack::new));

    ILiteral l_literal = CLiteral.from(l_tree.pop(), p_values);
    while (!l_tree.isEmpty())
        l_literal = CLiteral.from(l_tree.pop(), l_literal);

    return l_literal;
}

From source file:org.lightjason.agentspeak.action.builtin.rest.IBaseRest.java

/**
 * creates a literal structure from a stream of string elements,
 * the string stream will be build in a tree structure
 *
 * @param p_functor stream with functor strings
 * @param p_values value stream//from  w  w w  .  j a  va  2  s.  co m
 * @return term
 */
@Nonnull
protected static ITerm baseliteral(@Nonnull final Stream<String> p_functor,
        @Nonnull final Stream<ITerm> p_values) {
    final Stack<String> l_tree = p_functor.collect(Collectors.toCollection(Stack::new));

    ILiteral l_literal = CLiteral.from(l_tree.pop(), p_values);
    while (!l_tree.isEmpty())
        l_literal = CLiteral.from(l_tree.pop(), l_literal);

    return l_literal;
}