List of usage examples for java.util Stack pop
public synchronized E pop()
From source file:com.ms.commons.test.math.expression.util.MathExpressionParseUtil.java
private static MathExpression buildExpression(String expr, Stack<String> expressionStack, MathExpressionBuilder meb) throws MathParseException { if ((expressionStack.size() == 0) || (expressionStack.size() == 2)) { throw new MathParseException("Error in parse '" + expr + "'."); }//w ww .j a v a 2 s.co m if (expressionStack.size() == 1) { String subExpr = expressionStack.pop(); if (hasSeparators(subExpr)) { return buildExpression(expr, parseTokenStack(subExpr), meb); } else { return meb.buildSimpleMathExpression(subExpr); } } else { expressionStack = adjustExpressionStack(expressionStack); String subExpr = expressionStack.pop(); char op = expressionStack.pop().charAt(0); return meb.buildSimpleMathExpressionByOp(op, buildExpression(expr, expressionStack, meb), buildExpression(expr, parseTokenStack(subExpr), meb)); } }
From source file:alluxio.job.move.MoveDefinition.java
/** * @param source an Alluxio URI//w w w . j ava 2 s .c o m * @param fileSystem the Alluxio file system * @return whether the URI is a file or a directory which contains files (including recursively) * @throws Exception if an unexpected exception occurs */ private static boolean hasFiles(AlluxioURI source, FileSystem fileSystem) throws Exception { Stack<AlluxioURI> dirsToCheck = new Stack<>(); dirsToCheck.add(source); while (!dirsToCheck.isEmpty()) { try { for (URIStatus status : fileSystem.listStatus(dirsToCheck.pop())) { if (!status.isFolder()) { return true; } dirsToCheck.push(new AlluxioURI(status.getPath())); } } catch (FileDoesNotExistException e) { // This probably means another worker has deleted the directory already, so we can probably // return false here. To be safe though, we will fall through and complete the search. } } return false; }
From source file:Main.java
public static String getColumnId(int column) { Stack<Character> digits = new Stack<Character>(); int dividant = column; while (dividant > 26) { int remain = dividant % 26; dividant = dividant / 26;/* w w w . java 2 s . com*/ if (remain == 0) { remain = 26; dividant--; } digits.push((char) ('A' + remain - 1)); } digits.push((char) ('A' + dividant - 1)); StringBuffer buffer = new StringBuffer(); while (!digits.empty()) { buffer.append(digits.pop()); } String columnId = buffer.toString(); return columnId; }
From source file:com.k42b3.aletheia.protocol.http.Util.java
public static String removeDotSegments(String relativePath) { // remove query or fragment part if any int pos = relativePath.indexOf('?'); if (pos != -1) { relativePath = relativePath.substring(0, pos); }/* w w w . j a v a 2s.c o m*/ pos = relativePath.indexOf('#'); if (pos != -1) { relativePath = relativePath.substring(0, pos); } // if the path contains no slash we have nothing to resolve if (relativePath.indexOf('/') == -1) { return relativePath; } String[] parts = relativePath.split("/"); Stack<String> path = new Stack<String>(); String part; for (int i = 0; i < parts.length; i++) { part = parts[i].trim(); if (part.isEmpty() || part.equals(".")) { } else if (part.equals("..")) { path.pop(); } else { path.add(part); } } // build absolute url String absoluteUrl = ""; if (path.size() > 0) { for (int i = 0; i < path.size(); i++) { if (i > 0 && path.get(i).indexOf('.') != -1 && path.get(i - 1).equals(path.get(i))) { // if the element before has the same name and it contains // an dot we have probably an file name continue; } absoluteUrl += "/" + path.get(i); } } else { absoluteUrl = "/"; } // add last slash if (relativePath.endsWith("/") && !absoluteUrl.endsWith("/")) { absoluteUrl = absoluteUrl + "/"; } return absoluteUrl; }
From source file:com.intuit.tank.harness.functions.StringFunctions.java
/** * Generate a random user id as an integer. * /*from w ww . j a v a 2 s .co m*/ * e.g. #function.string.useridFromRange.25.100 * * @param values * Array values - [3] = minId, [4] = maxId, [5] = inclusions/inclusions list comma seperated regex to * test the value * @param include * The format for the date * @return The random user id */ static private String userIdFromRange(String[] values, boolean include) { int minId = Integer.parseInt(values[3]); int maxId = Integer.parseInt(values[4]); String exclusions = values.length > 5 ? values[5] : null; Stack<Integer> stack = getStack(minId, maxId, exclusions, include); if (stack.size() > 0) { return Integer.toString(stack.pop()); } throw new IllegalArgumentException( "Exhausted random User Ids. Range not large enough for the number of calls."); }
From source file:com.intuit.tank.harness.functions.StringFunctions.java
/** * Generate a random user id in the range provided * //from w ww. j av a2s. co m * e.g. #function.string.useridFromRange.25.100 * * @param prefixLength * The length of the random prefix * @param dateFormat * The format for the date * @return The random user id */ static private String userIdFromRangeWithMod(String[] values, boolean include) { int minId = Integer.parseInt(values[3]); int maxId = Integer.parseInt(values[4]); int mod = Integer.parseInt(values[5]); Stack<Integer> stack = getStackWithMods(minId, maxId, mod, include); if (stack.size() > 0) { return Integer.toString(stack.pop()); } throw new IllegalArgumentException( "Exhausted random User Ids. Range not large enough for the number of calls."); }
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);//from w ww.j ava2 s . com T nextElement; 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:gdt.data.grain.Support.java
/** * Intersect two string arrays //ww w . j av a 2 s . co m * @param list1 first array * @param list2 second array * @return the result string array. */ public static String[] intersect(String[] list1, String[] list2) { if (list2 == null || list1 == null) { return null; } Stack<String> s1 = new Stack<String>(); Stack<String> s2 = new Stack<String>(); for (String aList2 : list2) s2.push(aList2); String line$; boolean found; String member$ = null; while (!s2.isEmpty()) { try { found = false; line$ = s2.pop().toString(); if (line$ == null) continue; for (String aList1 : list1) { member$ = aList1; if (line$.equals(member$)) { found = true; break; } } if (found) Support.addItem(member$, s1); } catch (Exception e) { Logger.getLogger(Support.class.getName()).info("intersect:" + e.toString()); } } return s1.toArray(new String[0]); }
From source file:com.espertech.esper.view.ViewSupport.java
private static boolean findDescendentRecusive(View parentView, Viewable descendentView, Stack<View> stack) { stack.push(parentView);/*from w w w . j a v a2s . c o m*/ boolean found = false; for (View view : parentView.getViews()) { if (view == descendentView) { return true; } found = findDescendentRecusive(view, descendentView, stack); if (found) { break; } } if (!found) { stack.pop(); return false; } return true; }
From source file:ReversePolishNotation.java
/** * This method will calculate the answer of the given Reverse Polar Notation * equation. All exceptions are thrown to the parent for handling. * //w w w . j a v a 2 s.co m * @param input * is the equation entered by the user * @throws EmptyRPNException * when there is no RPN equation to be evaluated. * @throws RPNDivideByZeroException * when the RPN equation attempts to divide by zero. * @throws RPNUnderflowException * when the RPN equation has a mathematical operator before * there are enough numerical values for it to evaluate. * @throws InvalidRPNException * when the RPN equation is a String which is unable to be * manipulated. * @throws RPNOverflowException * when the RPN equation has too many numerical values and not * enough mathematical operators with which to evaluate them. * @return the top item of the stack; the calculated answer of the Reverse * Polish Notation equation */ public static double calcRPN(String input) { // eliminate any leading or trailing whitespace from input input = input.trim(); // scanner to manipulate input and stack to store double values String next; Stack<Double> stack = new Stack<Double>(); Scanner scan = new Scanner(input); // loop while there are tokens left in scan while (scan.hasNext()) { // retrieve the next token from the input next = scan.next(); // see if token is mathematical operator if (nextIsOperator(next)) { // ensure there are enough numbers on stack if (stack.size() > 1) { if (next.equals("+")) { stack.push((Double) stack.pop() + (Double) stack.pop()); } else if (next.equals("-")) { stack.push(-(Double) stack.pop() + (Double) stack.pop()); } else if (next.equals("*")) { stack.push((Double) stack.pop() * (Double) stack.pop()); } else if (next.equals("/")) { double first = stack.pop(); double second = stack.pop(); if (first == 0) { System.out.println("The RPN equation attempted to divide by zero."); } else { stack.push(second / first); } } } else { System.out.println( "A mathematical operator occured before there were enough numerical values for it to evaluate."); } } else { try { stack.push(Double.parseDouble(next)); } catch (NumberFormatException c) { System.out.println("The string is not a valid RPN equation."); } } } if (stack.size() > 1) { System.out.println( "There too many numbers and not enough mathematical operators with which to evaluate them."); } return (Double) stack.pop(); }