List of usage examples for java.util Stack push
public E push(E item)
From source file:opennlp.tools.parse_thicket.kernel_interface.BracesProcessor.java
public static boolean isParenthesisMatch(String str) { Stack<Character> stack = new Stack<Character>(); char c;/*from w w w . ja v a2 s. c o m*/ for (int i = 0; i < str.length(); i++) { c = str.charAt(i); if (c == '{') return false; if (c == '(') stack.push(c); if (c == '{') { stack.push(c); if (c == '}') if (stack.empty()) return false; else if (stack.peek() == '{') stack.pop(); } else if (c == ')') if (stack.empty()) return false; else if (stack.peek() == '(') stack.pop(); else return false; } return stack.empty(); }
From source file:com.sunyue.util.calculator.core.ExpressionConverter.java
/** * Convert an infix expression to a postfix expression. * // www. j a va2 s .com * @param exp * expression parsed by ExpressionParser * @return postfix expression */ public static Object[] convert(Object[] exp) { if (ArrayUtils.isEmpty(exp)) { throw new CalculationException("Expression can not be empty"); } // remember if brackets are coupled int coupled = 0; // out put postfix expression List<Object> out = new ArrayList<Object>(); Stack<Object> opStack = new Stack<Object>(); for (int i = 0; i < exp.length; i++) { if (exp[i] instanceof Operator) { // operator Operator op = (Operator) exp[i]; while (true) { if (opStack.isEmpty()) { opStack.push(op); break; } else { Object obj = opStack.peek(); if (!(obj instanceof Bracket)) { Operator preOp = (Operator) opStack.peek(); if (op.getPriority() <= preOp.getPriority()) { // pop and output operator with not lower // priority out.add(opStack.pop()); } else { // push otherwise opStack.push(op); break; } } else { // push when bracket on top opStack.push(op); break; } } } } else if (Bracket.LEFT_BRACKET.equals(exp[i])) { opStack.push(exp[i]); coupled++; } else if (Bracket.RIGHT_BRACKET.equals(exp[i])) { if (coupled <= 0) { throw new CalculationException("Brackets are not coupled, missing left bracket ("); } while (true) { Object op = opStack.pop(); if (Bracket.LEFT_BRACKET.equals(op)) { // eliminate coupled brackets break; } else { // pop and output until coupled left bracket out.add(op); } } coupled--; } else { // general numbers out.add(exp[i]); } } if (coupled != 0) { throw new CalculationException("Brackets are not coupled, missing right bracket )"); } // output rest elements while (!opStack.isEmpty()) { out.add(opStack.pop()); } return out.toArray(); }
From source file:com.trenako.web.infrastructure.SearchRequestUrlParser.java
/** * Parses the {@code path} string, matching the {@code SearchCriteria} property names. * <p>//w ww . ja v a2 s. c o m * This method is able to manage paths with wrong sequences, in this case * the values outside the correct sequence are simply ignored. * </p> * * @param path the {@code path} string * @return a {@code Map} with the extracted values */ public static Map<String, String> parseUrl(String path) { Assert.notNull(path, "Path must be not null"); Map<String, String> values = new HashMap<>(); Stack<String> stack = new Stack<>(); String[] tokens = path.split("/"); for (String tk : tokens) { if (SEARCH_CRITERIA_KEYS.contains(tk)) { if (!stack.isEmpty()) { // a different key name was found, but no value was provided // (ie /key1/key2/value2) stack.pop(); } stack.push(tk); } else { if (!stack.isEmpty()) { // match this value with the key name in the stack values.put(stack.pop(), tk); } } } return values; }
From source file:de.codesourcery.jasm16.ast.ASTUtils.java
public static Iterator<ASTNode> createInOrderIterator(ASTNode node) { if (node == null) { throw new IllegalArgumentException("node must not be NULL"); }// w ww . java 2 s. c om final Stack<ASTNode> stack = new Stack<ASTNode>(); stack.push(node); return new Iterator<ASTNode>() { @Override public boolean hasNext() { return !stack.isEmpty(); } /* A (1) * |\ *(5) E B (2) * |\ *(4) D C (3) */ @Override public ASTNode next() { if (stack.isEmpty()) { throw new NoSuchElementException(); } ASTNode result = stack.pop(); final List<ASTNode> children = result.getChildren(); Collections.reverse(children); for (ASTNode child : children) { stack.push(child); } return result; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:Main.java
/** * Removes dot segments according to RFC 3986, section 5.2.4 * * @param uri the original URI/*from ww w .ja v a 2 s .c o m*/ * @return the URI without dot segments */ private static URI removeDotSegments(URI uri) { String path = uri.getPath(); if ((path == null) || (path.indexOf("/.") == -1)) { // No dot segments to remove return uri; } String[] inputSegments = path.split("/"); Stack<String> outputSegments = new Stack<String>(); for (int i = 0; i < inputSegments.length; i++) { if ((inputSegments[i].length() == 0) || (".".equals(inputSegments[i]))) { // Do nothing } else if ("..".equals(inputSegments[i])) { if (!outputSegments.isEmpty()) { outputSegments.pop(); } } else { outputSegments.push(inputSegments[i]); } } StringBuilder outputBuffer = new StringBuilder(); for (String outputSegment : outputSegments) { outputBuffer.append('/').append(outputSegment); } try { return new URI(uri.getScheme(), uri.getAuthority(), outputBuffer.toString(), uri.getQuery(), uri.getFragment()); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
From source file:org.hellojavaer.testcase.generator.TestCaseGenerator.java
@SuppressWarnings("rawtypes") public static <T> List<T> generate(Class<T> clazz, String[] uniqueFields, int count, String[] excludeFields, int recursiveCycleLimit) { List<String> excludeFieldList = null; if (excludeFields != null && excludeFields.length > 0) { excludeFieldList = Arrays.asList(excludeFields); }/* ww w . j a v a 2s . com*/ if (recursiveCycleLimit <= 0) { recursiveCycleLimit = 0; } checkBeanValidity(clazz, excludeFieldList, recursiveCycleLimit); List<T> list = new ArrayList<T>(count); ControlParam cp = new ControlParam(); cp.setExcludeFieldList(excludeFieldList); cp.setNumIndex(1); cp.setRandom(new Random(RANDOM_SEED)); cp.setStrIndex(100000); cp.setStrStep(1111L); cp.setCharIndex(0); cp.setRecursiveCycleLimit(recursiveCycleLimit); Calendar time = Calendar.getInstance(); time.setTime(START_TIME); cp.setTime(time); if (uniqueFields != null && uniqueFields.length > 0) { cp.setUniqueFieldList(Arrays.asList(uniqueFields)); } for (int i = 0; i < count; i++) { Stack<Class> stack = new Stack<Class>(); stack.push(clazz); list.add(produceBean(clazz, cp, stack)); } return list; }
From source file:com.baifendian.swordfish.execserver.parameter.placeholder.CalculateUtil.java
/** * ???//from ww w . ja v a2 s. c o m * * @param inOrderList * @return ?? */ private static List<String> getPostOrder(List<String> inOrderList) { List<String> result = new ArrayList<>(); Stack<String> stack = new Stack<>(); for (int i = 0; i < inOrderList.size(); i++) { if (Character.isDigit(inOrderList.get(i).charAt(0))) { result.add(inOrderList.get(i)); } else { switch (inOrderList.get(i).charAt(0)) { case '(': stack.push(inOrderList.get(i)); break; case ')': while (!stack.peek().equals("(")) { result.add(stack.pop()); } stack.pop(); break; default: while (!stack.isEmpty() && compare(stack.peek(), inOrderList.get(i))) { result.add(stack.pop()); } stack.push(inOrderList.get(i)); break; } } } while (!stack.isEmpty()) { result.add(stack.pop()); } return result; }
From source file:de.codesourcery.jasm16.ast.ASTUtils.java
public static Iterator<ASTNode> createDepthFirst(ASTNode node) { final Stack<ASTNode> stack = new Stack<ASTNode>(); if (node != null) { stack.push(node); }//from www .jav a 2 s . c o m return new Iterator<ASTNode>() { @Override public boolean hasNext() { return !stack.isEmpty(); } @Override public ASTNode next() { ASTNode n = stack.peek(); for (ASTNode child : n.getChildren()) { stack.push(child); } return stack.pop(); } @Override public void remove() { throw new UnsupportedOperationException("Not implemented"); } }; }
From source file:com.microsoft.tfs.core.clients.framework.configuration.internal.ReportingFolderEntityUtils.java
public static String getFullItemPath(final ReportingFolderEntity folder) { Check.notNull(folder, "folder"); //$NON-NLS-1$ final Stack<String> path = new Stack<String>(); TFSEntity facet = folder;/*from w w w . j a v a 2 s.c om*/ /* Walk the parentage to build the path to this object */ while (facet != null) { if (facet instanceof ReportingFolderEntity) { final String itemPath = ((ReportingFolderEntity) facet).getItemPath(); if (itemPath != null && itemPath.length() > 0) { path.push(itemPath); } facet = ((ReportingFolderEntity) facet).getReferencedResource(); } else if (facet instanceof ReportingServerEntity) { /* Stop */ break; } else { log.warn(MessageFormat.format("Invalid type {0} along reporting folder dependency chain", //$NON-NLS-1$ facet.getClass().getCanonicalName())); break; } } // Construct the full item path. String fullItemPath = ""; //$NON-NLS-1$ while (path.size() > 0) { String pathSegment = path.pop(); pathSegment = pathSegment.trim(); while (pathSegment.startsWith("/") || pathSegment.startsWith("\\")) //$NON-NLS-1$ //$NON-NLS-2$ { pathSegment = pathSegment.substring(1); } fullItemPath = URIUtils.combinePaths(fullItemPath, pathSegment); } // Full item paths always start with a '/' if (fullItemPath.startsWith("/")) //$NON-NLS-1$ { return fullItemPath; } return "/" + fullItemPath; //$NON-NLS-1$ }
From source file:de.tudarmstadt.ukp.csniper.webapp.search.tgrep.PennTreeUtils.java
public static PennTreeNode parsePennTree(String aTree) { StringTokenizer st = new StringTokenizer(aTree, "() ", true); PennTreeNode root = null;/*w w w.ja v a2s . c o m*/ Stack<PennTreeNode> stack = new Stack<PennTreeNode>(); boolean seenLabel = false; int i = 0; while (st.hasMoreTokens()) { String t = st.nextToken().trim(); if (t.length() == 0) { // Skip } else if ("(".equals(t)) { PennTreeNode n = new PennTreeNode(); stack.push(n); if (root == null) { root = n; } seenLabel = false; } else if (")".equals(t)) { PennTreeNode n = stack.pop(); if (!stack.isEmpty()) { PennTreeNode p = stack.peek(); p.addChild(n); } } else if (seenLabel) { // If the node has two labels, its a leaf, add a new terminal node then. PennTreeNode p = stack.peek(); PennTreeNode n = new PennTreeNode(); n.setTokenIndex(i); i++; n.setLabel(t); p.addChild(n); } else { PennTreeNode n = stack.peek(); n.setLabel(t); seenLabel = true; } } return root; }