List of usage examples for java.util Stack pop
public synchronized E pop()
From source file:org.apache.hadoop.hive.ql.plan.BaseWork.java
/** * Returns a set containing all leaf operators from the operator tree in this work. * @return a set containing all leaf operators in this operator tree. *//*ww w . j av a 2s. c o m*/ public Set<Operator<?>> getAllLeafOperators() { Set<Operator<?>> returnSet = new LinkedHashSet<Operator<?>>(); Set<Operator<?>> opSet = getAllRootOperators(); Stack<Operator<?>> opStack = new Stack<Operator<?>>(); // add all children opStack.addAll(opSet); while (!opStack.empty()) { Operator<?> op = opStack.pop(); if (op.getNumChild() == 0) { returnSet.add(op); } if (op.getChildOperators() != null) { opStack.addAll(op.getChildOperators()); } } return returnSet; }
From source file:com.dianping.cat.consumer.problem.ProblemReportMerger.java
@Override protected void visitMachineChildren(Machine to, Machine from) { Stack<Object> objs = getObjects(); for (Entry source : from.getEntries()) { Entry target = findOrCreateEntry(to, source); objs.push(target);/*from ww w . j av a2s .c o m*/ source.accept(this); objs.pop(); } for (Entity source : from.getEntities().values()) { Entity target = findOrCreateEntity(to, source); objs.push(target); source.accept(this); objs.pop(); } }
From source file:com.qwazr.utils.WildcardMatcher.java
/** * Match the passed name with the current pattern * * @param name the string to test * @param caseSensitivity/*from w ww.jav a2 s.c o m*/ * @return true if the name match the pattern */ public boolean match(String name, IOCase caseSensitivity) { if (name == null && wcs == null) { return true; } if (name == null || wcs == null) { return false; } if (caseSensitivity == null) { caseSensitivity = IOCase.SENSITIVE; } int length = name.length(); boolean anyChars = false; int textIdx = 0; int wcsIdx = 0; Stack<int[]> backtrack = new Stack<int[]>(); // loop around a backtrack stack, to handle complex * matching do { if (backtrack.size() > 0) { int[] array = backtrack.pop(); wcsIdx = array[0]; textIdx = array[1]; anyChars = true; } // loop whilst tokens and text left to process while (wcsIdx < wcs.length) { if (wcs[wcsIdx].equals("?")) { // ? so move to next text char textIdx++; if (textIdx > length) { break; } anyChars = false; } else if (wcs[wcsIdx].equals("*")) { // set any chars status anyChars = true; if (wcsIdx == wcs.length - 1) { textIdx = length; } } else { // matching text token if (anyChars) { // any chars then try to locate text token textIdx = caseSensitivity.checkIndexOf(name, textIdx, wcs[wcsIdx]); if (textIdx == -1) { // token not found break; } int repeat = caseSensitivity.checkIndexOf(name, textIdx + 1, wcs[wcsIdx]); if (repeat >= 0) { backtrack.push(new int[] { wcsIdx, repeat }); } } else { // matching from current position if (!caseSensitivity.checkRegionMatches(name, textIdx, wcs[wcsIdx])) { // couldnt match token break; } } // matched text token, move text index to end of matched token textIdx += wcs[wcsIdx].length(); anyChars = false; } wcsIdx++; } // full match if (wcsIdx == wcs.length && textIdx == length) { return true; } } while (backtrack.size() > 0); return false; }
From source file:com.holonplatform.core.internal.utils.TypeUtils.java
/** * Return the type parameter of a generic type. * @param clazz subClass of <code>baseClass</code> to analyze. * @param baseClass base class having the type parameter the value of which we need to retrieve * @return the parameterized type value/*from www . j av a 2s. c o m*/ */ @SuppressWarnings("rawtypes") public static Type getTypeArgument(Class<?> clazz, Class<?> baseClass) { Stack<Type> superclasses = new Stack<>(); Type currentType; Class<?> currentClass = clazz; if (clazz.getGenericSuperclass() == Object.class) { currentType = clazz; superclasses.push(currentType); } else { do { currentType = currentClass.getGenericSuperclass(); superclasses.push(currentType); if (currentType instanceof Class) { currentClass = (Class) currentType; } else if (currentType instanceof ParameterizedType) { currentClass = (Class) ((ParameterizedType) currentType).getRawType(); } } while (!currentClass.equals(baseClass)); } // find which one supplies type argument and return it TypeVariable tv = baseClass.getTypeParameters()[0]; while (!superclasses.isEmpty()) { currentType = superclasses.pop(); if (currentType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) currentType; Class<?> rawType = (Class) pt.getRawType(); int argIndex = Arrays.asList(rawType.getTypeParameters()).indexOf(tv); if (argIndex > -1) { Type typeArg = pt.getActualTypeArguments()[argIndex]; if (typeArg instanceof TypeVariable) { // type argument is another type variable - look for the value of that // variable in subclasses tv = (TypeVariable) typeArg; continue; } else { // found the value - return it return typeArg; } } } // needed type argument not supplied - break and throw exception break; } throw new IllegalArgumentException(currentType + " does not specify a type parameter"); }
From source file:cn.vlabs.duckling.vwb.tags.ParentTag.java
private String printFullPath(Stack<PathElement> path) { StringBuffer html = new StringBuffer(); PathElement pe;/* www . ja v a 2 s.co m*/ int count = 0; while (path.size() > 0) { count++; String fragment; pe = path.pop(); if (path.size() > 0) { fragment = pe.makeLink(parentclass); } else { fragment = pe.makeLink(linkclass); } if (count > 1) html.append(" > "); html.append(fragment); } return html.toString(); }
From source file:cn.vlabs.duckling.vwb.tags.ParentTag.java
private String printCollapsePath(Stack<PathElement> path) { StringBuffer html = new StringBuffer(); PathElement pe;/*from w ww .j a v a 2s . c o m*/ boolean first = true; boolean collapsePrinted = false; while (path.size() > 0) { pe = path.pop(); if (!first) { if (path.size() > 3) { if (!collapsePrinted) { collapsePrinted = true; html.append(" > "); html.append("<font color=\"#c0c0c0\">......</font>"); } } else { // print ">" split html.append(" > "); html.append(pe.makeCollpaseLink(linkclass)); } } else { first = false; html.append(pe.makeCollpaseLink(parentclass)); } } return html.toString(); }
From source file:FlightInfo.java
void route(String to) { Stack rev = new Stack(); int dist = 0; FlightInfo f;//from w w w . j ava 2s . c om int num = btStack.size(); // Reverse the stack to display route. for (int i = 0; i < num; i++) rev.push(btStack.pop()); for (int i = 0; i < num; i++) { f = (FlightInfo) rev.pop(); System.out.print(f.from + " to "); dist += f.distance; } System.out.println(to); System.out.println("Distance is " + dist); }
From source file:DOM2SAX.java
private void endPrefixMapping(String prefix) throws SAXException { final Stack uriStack = (Stack) prefixes.get(prefix); if (uriStack != null) { contentHandler.endPrefixMapping(prefix); uriStack.pop(); }/*from www .j ava2 s. c om*/ }
From source file:com.pinterest.hdfsbackup.distcp.DistCp.java
/** Delete the dst files/dirs which do not exist in src */ static private void deleteNonexisting(FileSystem dstfs, FileStatus dstroot, Path dstsorted, FileSystem jobfs, Path jobdir, JobConf jobconf, Configuration conf) throws IOException { if (!dstroot.isDir()) { throw new IOException("dst must be a directory when option " + Options.DELETE.cmd + " is set, but dst (= " + dstroot.getPath() + ") is not a directory."); }// w w w. j a v a 2 s . c om //write dst lsr results final Path dstlsr = new Path(jobdir, "_distcp_dst_lsr"); final SequenceFile.Writer writer = SequenceFile.createWriter(jobfs, jobconf, dstlsr, Text.class, FileStatus.class, SequenceFile.CompressionType.NONE); try { //do lsr to get all file statuses in dstroot final Stack<FileStatus> lsrstack = new Stack<FileStatus>(); for (lsrstack.push(dstroot); !lsrstack.isEmpty();) { final FileStatus status = lsrstack.pop(); if (status.isDir()) { for (FileStatus child : dstfs.listStatus(status.getPath())) { String relative = makeRelative(dstroot.getPath(), child.getPath()); writer.append(new Text(relative), child); lsrstack.push(child); } } } } finally { checkAndClose(writer); } //sort lsr results final Path sortedlsr = new Path(jobdir, "_distcp_dst_lsr_sorted"); SequenceFile.Sorter sorter = new SequenceFile.Sorter(jobfs, new Text.Comparator(), Text.class, FileStatus.class, jobconf); sorter.sort(dstlsr, sortedlsr); //compare lsr list and dst list SequenceFile.Reader lsrin = null; SequenceFile.Reader dstin = null; try { lsrin = new SequenceFile.Reader(jobfs, sortedlsr, jobconf); dstin = new SequenceFile.Reader(jobfs, dstsorted, jobconf); //compare sorted lsr list and sorted dst list final Text lsrpath = new Text(); final FileStatus lsrstatus = new FileStatus(); final Text dstpath = new Text(); final Text dstfrom = new Text(); final FsShell shell = new FsShell(conf); final String[] shellargs = { "-rmr", null }; boolean hasnext = dstin.next(dstpath, dstfrom); for (; lsrin.next(lsrpath, lsrstatus);) { int dst_cmp_lsr = dstpath.compareTo(lsrpath); for (; hasnext && dst_cmp_lsr < 0;) { hasnext = dstin.next(dstpath, dstfrom); dst_cmp_lsr = dstpath.compareTo(lsrpath); } if (dst_cmp_lsr == 0) { //lsrpath exists in dst, skip it hasnext = dstin.next(dstpath, dstfrom); } else { //lsrpath does not exist, delete it String s = new Path(dstroot.getPath(), lsrpath.toString()).toString(); if (shellargs[1] == null || !isAncestorPath(shellargs[1], s)) { shellargs[1] = s; int r = 0; try { r = shell.run(shellargs); } catch (Exception e) { throw new IOException("Exception from shell.", e); } if (r != 0) { throw new IOException( "\"" + shellargs[0] + " " + shellargs[1] + "\" returns non-zero value " + r); } } } } } finally { checkAndClose(lsrin); checkAndClose(dstin); } }
From source file:edu.umn.cs.spatialHadoop.nasa.StockQuadTree.java
/** * Perform a selection query that retrieves all points in the given range. * The range is specified in the two-dimensional array positions. * @param in/*from www. jav a 2s. c om*/ * @param query_mbr * @param output * @return number of matched records * @throws IOException */ public static int selectionQuery(FSDataInputStream in, Rectangle query_mbr, ResultCollector<PointValue> output) throws IOException { long treeStartPosition = in.getPos(); int numOfResults = 0; int resolution = in.readInt(); short fillValue = in.readShort(); int cardinality = in.readInt(); long[] timestamps = new long[cardinality]; for (int i = 0; i < cardinality; i++) timestamps[i] = in.readLong(); Vector<Integer> selectedStarts = new Vector<Integer>(); Vector<Integer> selectedEnds = new Vector<Integer>(); StockQuadTree stockQuadTree = getOrCreateStockQuadTree(resolution); // Nodes to be searched. Contains node positions in the array of nodes Stack<Integer> nodes_2b_searched = new Stack<Integer>(); nodes_2b_searched.add(0); // Root node (ID=1) Rectangle node_mbr = new Rectangle(); while (!nodes_2b_searched.isEmpty()) { int node_pos = nodes_2b_searched.pop(); stockQuadTree.getNodeMBR(node_pos, node_mbr); if (query_mbr.contains(node_mbr)) { // Add this node to the selection list and stop this branch if (!selectedEnds.isEmpty() && selectedEnds.lastElement() == stockQuadTree.nodesStartPosition[node_pos]) { // Merge with an adjacent range selectedEnds.set(selectedEnds.size() - 1, stockQuadTree.nodesEndPosition[node_pos]); } else { // add a new range selectedStarts.add(stockQuadTree.nodesStartPosition[node_pos]); selectedEnds.add(stockQuadTree.nodesEndPosition[node_pos]); } numOfResults += stockQuadTree.nodesEndPosition[node_pos] - stockQuadTree.nodesStartPosition[node_pos]; } else if (query_mbr.intersects(node_mbr)) { int first_child_id = stockQuadTree.nodesID[node_pos] * 4 + 0; int first_child_pos = Arrays.binarySearch(stockQuadTree.nodesID, first_child_id); if (first_child_pos < 0) { // No children. Hit a leaf node // Scan and add matching points only java.awt.Point record_coords = new Point(); for (int record_pos = stockQuadTree.nodesStartPosition[node_pos]; record_pos < stockQuadTree.nodesEndPosition[node_pos]; record_pos++) { stockQuadTree.getRecordCoords(record_pos, record_coords); if (query_mbr.contains(record_coords)) { // matched a record. if (!selectedEnds.isEmpty() && selectedEnds.lastElement() == record_pos) { // Merge with an adjacent range selectedEnds.set(selectedEnds.size() - 1, record_pos + 1); } else { // Add a new range of unit width selectedStarts.add(record_pos); selectedEnds.add(record_pos + 1); } numOfResults++; } } } else { // Non-leaf node. Add all children to the list of nodes to search // Add in reverse order to the stack so that results come in sorted order nodes_2b_searched.add(first_child_pos + 3); nodes_2b_searched.add(first_child_pos + 2); nodes_2b_searched.add(first_child_pos + 1); nodes_2b_searched.add(first_child_pos + 0); } } } if (output != null) { PointValue returnValue = new PointValue(); long dataStartPosition = treeStartPosition + getValuesStartOffset(cardinality); // Return all values in the selected ranges for (int iRange = 0; iRange < selectedStarts.size(); iRange++) { int treeStart = selectedStarts.get(iRange); int treeEnd = selectedEnds.get(iRange); long startPosition = dataStartPosition + selectedStarts.get(iRange) * cardinality * 2; in.seek(startPosition); for (int treePos = treeStart; treePos < treeEnd; treePos++) { // Retrieve the coords for the point at treePos stockQuadTree.getRecordCoords(treePos, returnValue); // Read all entries at current position for (int iValue = 0; iValue < cardinality; iValue++) { short value = in.readShort(); if (value != fillValue) { returnValue.value = value; returnValue.timestamp = timestamps[iValue]; output.collect(returnValue); } } } } } return numOfResults; }