List of usage examples for java.util Stack Stack
public Stack()
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;//from w w w . j av a 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:Main.java
/** * Finds element in DOM tree//from w ww . j av a 2 s . com * @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
public static <T> Stack<T> newStack() { return new Stack<T>(); }
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);//w ww. j a v a2s.c om } else { 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: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 ww . ja va2s . 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:BracketChecker.java
public void check() { Stack<Character> theStack = new Stack<Character>(); for (int j = 0; j < input.length(); j++) { char ch = input.charAt(j); switch (ch) { case '{': case '[': case '(': theStack.push(ch);//ww w. java2 s .co m break; case '}': case ']': case ')': if (!theStack.isEmpty()) { char chx = theStack.pop(); if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[') || (ch == ')' && chx != '(')) System.out.println("Error: " + ch + " at " + j); } else System.out.println("Error: " + ch + " at " + j); break; default: break; } } if (!theStack.isEmpty()) { System.out.println("Error: missing right delimiter"); } }
From source file:Main.java
/** * Given any of the known collection types, this method will return an instance of the collection. * @param collectionType the type of the collection * @return the collection instance/*w ww . j av a 2 s .c om*/ */ public static Collection<?> getCollection(Class<?> collectionType) { if (HashSet.class.equals(collectionType)) { return new HashSet<Object>(); } else if (TreeSet.class.equals(collectionType)) { return new TreeSet<Object>(); } else if (CopyOnWriteArraySet.class.equals(collectionType)) { return new CopyOnWriteArraySet<Object>(); } else if (LinkedHashSet.class.equals(collectionType)) { return new LinkedHashSet<Object>(); } else if (ArrayList.class.equals(collectionType)) { return new ArrayList<Object>(); } else if (LinkedList.class.equals(collectionType)) { return new LinkedList<Object>(); } else if (Vector.class.equals(collectionType)) { return new Vector<Object>(); } else if (Stack.class.equals(collectionType)) { return new Stack<Object>(); } else if (PriorityQueue.class.equals(collectionType)) { return new PriorityQueue<Object>(); } else if (PriorityBlockingQueue.class.equals(collectionType)) { return new PriorityBlockingQueue<Object>(); } else if (ArrayDeque.class.equals(collectionType)) { return new ArrayDeque<Object>(); } else if (ConcurrentLinkedQueue.class.equals(collectionType)) { return new ConcurrentLinkedQueue<Object>(); } else if (LinkedBlockingQueue.class.equals(collectionType)) { return new LinkedBlockingQueue<Object>(); } else if (LinkedBlockingDeque.class.equals(collectionType)) { return new LinkedBlockingDeque<Object>(); } else if (List.class.equals(collectionType)) { return new LinkedList<Object>(); } else if (Set.class.equals(collectionType)) { return new HashSet<Object>(); } else if (Queue.class.equals(collectionType)) { return new PriorityQueue<Object>(); } else if (Deque.class.equals(collectionType)) { return new ArrayDeque<Object>(); } else if (Collection.class.equals(collectionType)) { return new LinkedList<Object>(); } throw new IllegalArgumentException("Unsupported collection type: " + collectionType); }
From source file:com.sunyue.util.calculator.core.ExpressionConverter.java
/** * Convert an infix expression to a postfix expression. * //ww w . j a v a 2 s . c o m * @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:Main.java
/** * Computes of a given List with Lists the combinations. * Example: Given { { a, b } { 1, 2, 3} { p, q} } * Result is: { {a,1,p} {a,1,q} {a,2,p} {a,2,q} {a,3,p} * ... {b,2,q} {b,3,p} {b,3,q} } *///ww w . ja va 2 s .co m public static <T> List<List<T>> combinations(List<List<T>> listWithLists) { List<List<T>> combinations = new ArrayList<List<T>>(); Stack<T> elementStack = new Stack<T>(); combine(elementStack, listWithLists, combinations); return combinations; }
From source file:Main.java
/** * Check of the balanced tags sup/sub//from w w w. jav a 2s . c o m * * @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(); }