List of usage examples for java.util Stack push
public E push(E item)
From source file:ch.zhaw.icclab.tnova.expressionsolver.OTFlyEval.java
String evaluateExpression(String exp, Stack<Double> param, double threshold) { String[] literals = exp.split("(?!^)"); //array of characters String tempVal = ""; Stack<String> sTable = new Stack<String>(); for (int i = 0; i < literals.length; i++) { if (literals[i].equals("(")) { if (tempVal.trim().length() != 0) { sTable.push("fn " + tempVal.trim()); logger.info("pushed function [" + tempVal.trim() + "] into stack."); } else { //parsing error this stage is not allowed }//from ww w . j ava2s . c o m tempVal = ""; } else if (literals[i].equals(",")) { if (tempVal.trim().length() != 0) { sTable.push("pm " + tempVal.trim()); logger.info("pushed parameter [" + tempVal.trim() + "] into stack."); } else { //parsing error this stage is not allowed } tempVal = ""; } else if (literals[i].equals(")")) { if (tempVal.trim().length() != 0) { sTable.push("pm " + tempVal.trim()); logger.info("pushed parameter [" + tempVal.trim() + "] into stack."); } logger.info("Proceeding for partial stack evaluation."); tempVal = ""; //proceed to partial evaluation try { String output = partialEvaluation(sTable, param, threshold); //push the result back into stack as a literal sTable.push("pm " + output); logger.info("pushed parameter [" + output + "] into stack for further processing."); } catch (EmptyStackException emex) { logger.warn("Malformed expression and value set received."); if (App.showExceptions) emex.printStackTrace(); return null; } } else tempVal += literals[i]; } //if stack has more than 1 element error, else the only element is the result String result = ""; if (sTable.size() != 1) { //error result = null; } else { result = sTable.pop().split(" ")[1]; } return result; }
From source file:com.spidertracks.datanucleus.spring.ConsistencyLevelAspect.java
/** * Validates any method that has the valid annotation on it and is wired as * a spring service/*from w w w . j a v a2 s . co m*/ * * @param jp * @throws Throwable */ @Around("@annotation(com.spidertracks.datanucleus.spring.Consistency)") public Object setConsistency(ProceedingJoinPoint pjp) throws Throwable { logger.debug("Invoking before advice for @Consistency annotation. Target object is {} on method {}", pjp.getTarget(), pjp.getSignature()); MethodSignature sig = (MethodSignature) pjp.getSignature(); Object[] args = pjp.getArgs(); Method signatureMethod = sig.getMethod(); Class<?>[] signatureTypes = signatureMethod.getParameterTypes(); // we do this because we want to get the best match from the child // classes Class<?>[] runtimeArgs = new Class<?>[signatureTypes.length]; for (int i = 0; i < signatureTypes.length; i++) { if (args[i] != null) { runtimeArgs[i] = args[i].getClass(); } else { runtimeArgs[i] = signatureTypes[i]; } } Class<?> runtimeClass = pjp.getTarget().getClass(); // check if this is annotated, if not proceed and execute it ConsistencyLevel level = consistency(runtimeClass, signatureMethod.getName(), runtimeArgs); if (level == null) { return pjp.proceed(args); } Stack<ConsistencyLevel> stack = threadStack.get(); stack.push(level); com.spidertracks.datanucleus.client.Consistency.set(level); Object result = null; try { result = pjp.proceed(args); } finally { stack.pop(); if (stack.size() > 0) { com.spidertracks.datanucleus.client.Consistency.set(stack.peek()); } else { com.spidertracks.datanucleus.client.Consistency.remove(); } } return result; }
From source file:com.datastax.hectorjpa.spring.ConsistencyLevelAspect.java
/** * Validates any method that has the valid annotation on it and is wired as * a spring service/*from www . ja v a 2 s. c o m*/ * * @param jp * @throws Throwable */ @Around("@annotation(com.datastax.hectorjpa.spring.Consistency)") public Object setConsistency(ProceedingJoinPoint pjp) throws Throwable { logger.debug("Invoking before advice for @Consistency annotation. Target object is {} on method {}", pjp.getTarget(), pjp.getSignature()); MethodSignature sig = (MethodSignature) pjp.getSignature(); Object[] args = pjp.getArgs(); Method signatureMethod = sig.getMethod(); Class<?>[] signatureTypes = signatureMethod.getParameterTypes(); // we do this because we want to get the best match from the child // classes Class<?>[] runtimeArgs = new Class<?>[signatureTypes.length]; for (int i = 0; i < signatureTypes.length; i++) { if (args[i] != null) { runtimeArgs[i] = args[i].getClass(); } else { runtimeArgs[i] = signatureTypes[i]; } } Class<?> runtimeClass = pjp.getTarget().getClass(); // check if this is annotated, if not proceed and execute it HConsistencyLevel level = consistency(runtimeClass, signatureMethod.getName(), runtimeArgs); if (level == null) { return pjp.proceed(args); } Stack<HConsistencyLevel> stack = threadStack.get(); stack.push(level); JPAConsistency.set(level); Object result = null; try { result = pjp.proceed(args); } finally { stack.pop(); if (stack.size() > 0) { JPAConsistency.set(stack.peek()); } else { JPAConsistency.remove(); } } return result; }
From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.ContainerLocalizer.java
private void createParentDirs(Path destDirPath) throws IOException { Path parent = destDirPath.getParent(); Path cacheRoot = LocalCacheDirectoryManager.getCacheDirectoryRoot(parent); Stack<Path> dirs = new Stack<Path>(); while (!parent.equals(cacheRoot)) { dirs.push(parent); parent = parent.getParent();/* w ww .ja v a 2s. c om*/ } // Create directories with user cache permission while (!dirs.isEmpty()) { createDir(lfs, dirs.pop(), USERCACHE_FOLDER_PERMS); } }
From source file:edu.mit.lib.tools.Modernize.java
public void importToMds(String targetUrl) throws IOException { if (manif.isEmpty()) { manif.read();// ww w . ja v a2s . co m } Stack<String> parents = new Stack<>(); parents.push(null); // indicates no parent object for (int i = 0; i < manif.entries.size(); i++) { String handle = manif.entries.get(i); uploadPackage(getPackage(handle), getPostUrl(targetUrl, parents.peek(), manif.ctypes.get(i))); if (i < manif.entries.size() - 1) { int diff = manif.levels.get(i) - manif.levels.get(i + 1); if (diff < 0) { // I have kids - put myself on the parents stack parents.push(handle); } else if (diff > 0) { // expose grandparents while (diff-- > 0) { parents.pop(); } } // if diff == 0 - next entry is a sibling, nothing to do } } }
From source file:com.altoukhov.svsync.fileviews.SmbFileSpace.java
@Override protected Snapshot scan(List<Pattern> filters) { try {//w w w .j ava2s . c o m Map<String, FileSnapshot> files = new LinkedHashMap<>(); Set<String> dirs = new HashSet<>(); SmbFile root = new SmbFile(rootPath, auth); if (root.exists()) { Stack<SmbFile> stack = new Stack<>(); stack.push(root); dirs.add(""); while (!stack.isEmpty()) { SmbFile currentFolder = stack.pop(); for (final SmbFile file : listFiles(currentFolder)) { String path = file.getPath(); boolean isFile = isFile(file); boolean isDirectory = isDirectory(file); if (isFile && !isExcluded(path) && !isFiltered(toRelativePath(path), filters)) { FileSnapshot fileSnapshot = createFileSnapshot(file, path); files.put(fileSnapshot.getRelativePath(), fileSnapshot); } else if (isDirectory && !isExcluded(path) && !isFiltered(toRelativePath(path, true), filters)) { stack.push(file); dirs.add(toRelativePath(path)); System.out.println("Scanning " + path); } } } } Snapshot snapshot = new Snapshot(files, dirs); return snapshot; } catch (MalformedURLException | SmbException ex) { System.out.println("Failed to scan file space"); System.out.println(ex.getMessage()); System.out.println(ex); } return null; }
From source file:com.webcohesion.ofx4j.io.nanoxml.TestNanoXMLOFXReader.java
/** * tests using sax to parse an OFX doc.//from w ww . j a va 2s . co m */ public void testSimpleVersion1() throws Exception { NanoXMLOFXReader reader = new NanoXMLOFXReader(); final Map<String, List<String>> headers = new HashMap<String, List<String>>(); final Stack<Map<String, List<Object>>> aggregateStack = new Stack<Map<String, List<Object>>>(); TreeMap<String, List<Object>> root = new TreeMap<String, List<Object>>(); aggregateStack.push(root); reader.setContentHandler(getNewDefaultHandler(headers, aggregateStack)); reader.parse(TestNanoXMLOFXReader.class.getResourceAsStream("simple.ofx")); assertEquals(9, headers.size()); assertEquals(1, aggregateStack.size()); assertSame(root, aggregateStack.pop()); TreeMap<String, List<Object>> OFX = (TreeMap<String, List<Object>>) root.remove("OFX").get(0); assertNotNull(OFX); TreeMap<String, List<Object>> SIGNONMSGSRSV1 = (TreeMap<String, List<Object>>) OFX.remove("SIGNONMSGSRSV1") .get(0); assertNotNull(SIGNONMSGSRSV1); TreeMap<String, List<Object>> SONRS = (TreeMap<String, List<Object>>) SIGNONMSGSRSV1.remove("SONRS").get(0); assertNotNull(SONRS); TreeMap<String, List<Object>> STATUS = (TreeMap<String, List<Object>>) SONRS.remove("STATUS").get(0); assertNotNull(STATUS); assertEquals("0", STATUS.remove("CODE").get(0).toString().trim()); assertEquals("INFO", STATUS.remove("SEVERITY").get(0).toString().trim()); assertTrue(STATUS.isEmpty()); assertEquals("20071015021529.000[-8:PST]", SONRS.remove("DTSERVER").get(0).toString().trim()); assertEquals("ENG", SONRS.remove("LANGUAGE").get(0).toString().trim()); assertEquals("19900101000000", SONRS.remove("DTACCTUP").get(0).toString().trim()); assertTrue(SONRS.isEmpty()); assertTrue(SIGNONMSGSRSV1.isEmpty()); assertTrue(OFX.isEmpty()); assertTrue(root.isEmpty()); }
From source file:aula1.Aula1.java
public static String calculaPolonesaINversa(String entrada, String info) { Stack<String> pilha = new Stack<>(); Stack<Op> operadores = new Stack<>(); String[] operadoresSuportados = { "+", "-", "*", "/", "^", "(", ")", "sen" }; for (int i = 0; i < entrada.length(); i++) { String s = ""; if (entrada.charAt(i) == 's') { if (entrada.contains("sen")) { int ind = entrada.indexOf("sen"); ind += 2;//from w w w . j a v a 2 s. c o m i = 0; entrada = entrada.substring(ind + 1); s = "sen"; } } else s = String.valueOf(entrada.charAt(i)); if (Character.isDigit(entrada.charAt(i)) || entrada.charAt(i) == 'x') { if (Character.isDigit(entrada.charAt(i))) { s = String.valueOf(entrada.charAt(i)); while (Character.isDigit(entrada.charAt(i + 1))) { s += String.valueOf(entrada.charAt(i + 1)); i++; } } pilha.push(s); } else if (Arrays.asList(operadoresSuportados).contains(s)) { String n1 = ""; String n2 = ""; if (s.equals("sen")) n1 = pilha.pop(); else { n1 = pilha.pop(); n2 = pilha.pop(); } String resultado; if (n1.equals("x")) n1 = info; else if (n2.equals("x")) n2 = info; try { switch (s) { case "+": resultado = Operadores.soma(n2, n1); pilha.push(resultado); break; case "-": resultado = Operadores.sub(n2, n1); pilha.push(resultado); break; case "*": resultado = Operadores.M(n2, n1); pilha.push(resultado); break; case "/": resultado = Operadores.D(n2, n1); pilha.push(resultado); break; case "^": resultado = Operadores.pow(n2, n1); pilha.push(resultado); break; case "sen": resultado = Operadores.sen(n1); pilha.push(resultado); break; } } catch (Exception e) { System.out.println("Erro: " + e.getMessage()); } } } return pilha.peek(); }
From source file:com.webcohesion.ofx4j.io.nanoxml.TestNanoXMLOFXReader.java
/** * tests for closing tags in v1/*from w w w. j a va 2 s.co m*/ */ public void testClosingTagsVersion1() throws Exception { NanoXMLOFXReader reader = new NanoXMLOFXReader(); final Map<String, List<String>> headers = new HashMap<String, List<String>>(); final Stack<Map<String, List<Object>>> aggregateStack = new Stack<Map<String, List<Object>>>(); TreeMap<String, List<Object>> root = new TreeMap<String, List<Object>>(); aggregateStack.push(root); reader.setContentHandler(getNewDefaultHandler(headers, aggregateStack)); reader.parse(TestNanoXMLOFXReader.class.getResourceAsStream("closing-tags.ofx")); assertEquals(9, headers.size()); assertEquals(1, aggregateStack.size()); assertSame(root, aggregateStack.pop()); TreeMap<String, List<Object>> OFX = (TreeMap<String, List<Object>>) root.remove("OFX").get(0); assertNotNull(OFX); TreeMap<String, List<Object>> SIGNONMSGSRSV1 = (TreeMap<String, List<Object>>) OFX.remove("SIGNONMSGSRSV1") .get(0); assertNotNull(SIGNONMSGSRSV1); TreeMap<String, List<Object>> SONRS = (TreeMap<String, List<Object>>) SIGNONMSGSRSV1.remove("SONRS").get(0); assertNotNull(SONRS); TreeMap<String, List<Object>> STATUS = (TreeMap<String, List<Object>>) SONRS.remove("STATUS").get(0); assertNotNull(STATUS); TreeMap<String, List<Object>> FI = (TreeMap<String, List<Object>>) SONRS.remove("FI").get(0); assertNotNull(FI); assertEquals("0", STATUS.remove("CODE").get(0).toString().trim()); assertEquals("INFO", STATUS.remove("SEVERITY").get(0).toString().trim()); assertTrue(STATUS.isEmpty()); assertEquals("20100717152132", SONRS.remove("DTSERVER").get(0).toString().trim()); assertEquals("ENG", SONRS.remove("LANGUAGE").get(0).toString().trim()); assertEquals("ameritrade.com", FI.remove("ORG").get(0).toString().trim()); assertTrue(SONRS.isEmpty()); assertTrue(SIGNONMSGSRSV1.isEmpty()); assertTrue(OFX.isEmpty()); assertTrue(root.isEmpty()); }
From source file:DOM2SAX.java
/** * Begin the scope of namespace prefix. Forward the event to the SAX handler * only if the prefix is unknown or it is mapped to a different URI. *//*w ww.j a va 2s.c om*/ private boolean startPrefixMapping(String prefix, String uri) throws SAXException { boolean pushed = true; Stack uriStack = (Stack) prefixes.get(prefix); if (uriStack != null) { if (uriStack.isEmpty()) { contentHandler.startPrefixMapping(prefix, uri); uriStack.push(uri); } else { final String lastUri = (String) uriStack.peek(); if (!lastUri.equals(uri)) { contentHandler.startPrefixMapping(prefix, uri); uriStack.push(uri); } else { pushed = false; } } } else { contentHandler.startPrefixMapping(prefix, uri); uriStack = new Stack(); prefixes.put(prefix, uriStack); uriStack.push(uri); } return pushed; }