Example usage for java.util Stack push

List of usage examples for java.util Stack push

Introduction

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

Prototype

public E push(E item) 

Source Link

Document

Pushes an item onto the top of this stack.

Usage

From source file:Main.java

public static List<File> listFilesByName(File f) {
    Log.d("listFileByName f", "" + f);
    final List<File> fList = new LinkedList<>();
    final Stack<File> stk = new Stack<>();
    if (f.isDirectory()) {
        stk.push(f);
    } else {//from   w ww. java2  s.c o  m
        fList.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);
            } else {
                fList.add(f2);
            }
        }
    }
    return fList;
}

From source file:Main.java

public static void sortingByFastStack(int[] intArray, boolean ascending) {
    Stack<Integer> sa = new Stack<Integer>();
    sa.push(0);
    sa.push(intArray.length - 1);/*from  w  w w .  j  a v  a2s  . c o  m*/
    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:Main.java

public static <T> void flipStack(Stack<T> stack) {
    ArrayList<T> tmp = new ArrayList<>(stack);
    stack.clear();/*w w  w  .j av a  2s  .  c  o m*/
    for (int i = tmp.size() - 1; i >= 0; i--)
        stack.push(tmp.get(i));
}

From source file:Main.java

public static StringBuilder listFilesByName(File f, String lineSep) {
    Log.d("listFileByName f", "" + f);
    StringBuilder sb = new StringBuilder();
    if (f != null) {
        final Stack<File> stk = new Stack<>();
        if (f.isDirectory()) {
            stk.push(f);
        } else {/*from  w  w w.j a  v a2 s  .  c  o  m*/
            sb.append(f.getAbsolutePath()).append(": ").append(f.length()).append(" bytes.").append(lineSep);
        }
        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);
                } else {
                    sb.append(f2.getAbsolutePath()).append(": ").append(f2.length()).append(" bytes.")
                            .append(lineSep);
                }
            }
        }

    }
    return sb;
}

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);
            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;
        }//from  w  w  w. ja v a2s . co  m
    }
    return st.isEmpty();
}

From source file:de.se_rwth.langeditor.util.Misc.java

public static <T> Stream<T> preorder(T start, Function<T, Collection<? extends T>> generatingFunction) {
    List<T> elements = new ArrayList<>();
    Stack<T> stack = new Stack<>();
    stack.push(start);
    T currentElement;//from  w  w  w.  j  a va2s.com
    while (!stack.isEmpty()) {
        currentElement = stack.pop();
        elements.add(currentElement);
        stack.addAll(generatingFunction.apply(currentElement));
    }
    return elements.stream();
}

From source file:de.se_rwth.langeditor.util.Misc.java

public static <T> void traverse(T root, Function<T, Collection<? extends T>> childGenerator,
        Consumer<? super T> enter, Consumer<? super T> exit) {

    Set<T> previouslyVisited = new HashSet<>();
    Stack<T> yetToVisit = new Stack<>();
    yetToVisit.push(root);

    T nextElement;// w w  w  .j  a v a2 s .c om
    while (!yetToVisit.isEmpty()) {
        nextElement = yetToVisit.peek();
        if (!previouslyVisited.contains(nextElement)) {
            enter.accept(nextElement);
            previouslyVisited.add(nextElement);
            yetToVisit.addAll(childGenerator.apply(nextElement));
        } else {
            exit.accept(yetToVisit.pop());
        }
    }
}

From source file:org.apache.kylin.common.BackwardCompatibilityConfig.java

private static void generateFindAndReplaceScript(String kylinRepoPath, String outputPath) throws IOException {
    BackwardCompatibilityConfig bcc = new BackwardCompatibilityConfig();
    File repoDir = new File(kylinRepoPath).getCanonicalFile();
    File outputDir = new File(outputPath).getCanonicalFile();
    PrintWriter out = null;//w  ww  . ja  v  a  2  s . c  o  m

    // generate sed file
    File sedFile = new File(outputDir, "upgrade-old-config.sed");
    try {
        out = new PrintWriter(sedFile);
        for (Entry<String, String> e : bcc.old2new.entrySet()) {
            out.println("s/" + quote(e.getKey()) + "/" + e.getValue() + "/g");
        }
        for (Entry<String, String> e : bcc.old2newPrefix.entrySet()) {
            out.println("s/" + quote(e.getKey()) + "/" + e.getValue() + "/g");
        }
    } finally {
        IOUtils.closeQuietly(out);
    }

    // generate sh file
    File shFile = new File(outputDir, "upgrade-old-config.sh");
    try {
        out = new PrintWriter(shFile);
        out.println("#!/bin/bash");
        Stack<File> stack = new Stack<>();
        stack.push(repoDir);
        while (!stack.isEmpty()) {
            File dir = stack.pop();
            for (File f : dir.listFiles()) {
                if (f.getName().startsWith("."))
                    continue;
                if (f.isDirectory()) {
                    if (acceptSourceDir(f))
                        stack.push(f);
                } else if (acceptSourceFile(f))
                    out.println("sed -i -f upgrade-old-config.sed " + f.getAbsolutePath());
            }
        }
    } finally {
        IOUtils.closeQuietly(out);
    }

    System.out.println("Files generated:");
    System.out.println(shFile);
    System.out.println(sedFile);
}

From source file:com.inmobi.conduit.distcp.tools.util.TestDistCpUtils.java

public static boolean checkIfFoldersAreInSync(FileSystem fs, String targetBase, String sourceBase)
        throws IOException {
    Path base = new Path(targetBase);

    Stack<Path> stack = new Stack<Path>();
    stack.push(base);
    while (!stack.isEmpty()) {
        Path file = stack.pop();//w ww  .  j  av a 2s  .  co m
        if (!fs.exists(file))
            continue;
        FileStatus[] fStatus = fs.listStatus(file);
        if (fStatus == null || fStatus.length == 0)
            continue;

        for (FileStatus status : fStatus) {
            if (status.isDir()) {
                stack.push(status.getPath());
            }
            Assert.assertTrue(fs.exists(new Path(
                    sourceBase + "/" + DistCpUtils.getRelativePath(new Path(targetBase), status.getPath()))));
        }
    }
    return true;
}

From source file:Main.java

/**
 * Check of the balanced tags sup/sub//from  w  ww. j av  a2  s.c om
 * 
 * @param snippet
 * @return <code>true</code> if balanced, <code>false</code> otherwise
 */
public static boolean isBalanced(String snippet) {
    if (snippet == null)
        return true; // ????

    Stack<String> s = new Stack<String>();
    Matcher m = SUBS_OR_SUPS.matcher(snippet);
    while (m.find()) {
        String tag = m.group(1);
        if (tag.toLowerCase().startsWith("su")) {
            s.push(tag);
        } else {
            if (s.empty() || !tag.equals("/" + s.pop())) {
                return false;
            }
        }
    }

    return s.empty();
}