List of usage examples for java.util Stack empty
public boolean empty()
From source file:cf.adriantodt.utils.HTML2Discord.java
public static String toPlainText(String discordFormatMessage) { String strippedContent;//from w w w . java 2 s . com //all the formatting keys to keep track of String[] keys = new String[] { "*", "_", "`", "~~" }; //find all tokens (formatting strings described above) TreeSet<FormatToken> tokens = new TreeSet<>((t1, t2) -> Integer.compare(t1.start, t2.start)); for (String key : keys) { Matcher matcher = Pattern.compile(Pattern.quote(key)).matcher(discordFormatMessage); while (matcher.find()) { tokens.add(new FormatToken(key, matcher.start())); } } //iterate over all tokens, find all matching pairs, and add them to the list toRemove Stack<FormatToken> stack = new Stack<>(); List<FormatToken> toRemove = new ArrayList<>(); boolean inBlock = false; for (FormatToken token : tokens) { if (stack.empty() || !stack.peek().format.equals(token.format) || stack.peek().start + token.format.length() == token.start) { //we are at opening tag if (!inBlock) { //we are outside of block -> handle normally if (token.format.equals("`")) { //block start... invalidate all previous tags stack.clear(); inBlock = true; } stack.push(token); } else if (token.format.equals("`")) { //we are inside of a block -> handle only block tag stack.push(token); } } else if (!stack.empty()) { //we found a matching close-tag toRemove.add(stack.pop()); toRemove.add(token); if (token.format.equals("`") && stack.empty()) { //close tag closed the block inBlock = false; } } } //sort tags to remove by their start-index and iteratively build the remaining string Collections.sort(toRemove, (t1, t2) -> Integer.compare(t1.start, t2.start)); StringBuilder out = new StringBuilder(); int currIndex = 0; for (FormatToken formatToken : toRemove) { if (currIndex < formatToken.start) { out.append(discordFormatMessage.substring(currIndex, formatToken.start)); } currIndex = formatToken.start + formatToken.format.length(); } if (currIndex < discordFormatMessage.length()) { out.append(discordFormatMessage.substring(currIndex)); } //return the stripped text, escape all remaining formatting characters (did not have matching open/close before or were left/right of block strippedContent = out.toString().replace("*", "\\*").replace("_", "\\_").replace("~", "\\~"); return strippedContent; }
From source file:org.orbisgis.framework.Main.java
private static void parseCommandLine(String[] args) { //Read parameters Stack<String> sargs = new Stack<String>(); for (String arg : args) { sargs.insertElementAt(arg, 0);//from w ww .ja v a 2 s.c o m } while (!sargs.empty()) { String argument = sargs.pop(); if (argument.contentEquals("--debug")) { debugMode = true; } else if (argument.contentEquals("--nofailmode")) { noFailMode = true; } } }
From source file:Main.java
public static void replaceFragment(FragmentManager fragmentManager, int frameId, Fragment fragmentToShow, String fragmentTag, Stack<Fragment> fragmentStack) { if (fragmentToShow == null) { return;/*from w w w. j av a2 s .c o m*/ } List<Fragment> fragmentList = fragmentManager.getFragments(); for (Fragment fragment : fragmentList) { if (fragment == null) { continue; } fragmentManager.beginTransaction().remove(fragment).commit(); if (!fragmentStack.empty()) { fragmentStack.pop(); } } fragmentManager.beginTransaction().add(frameId, fragmentToShow, fragmentTag).commit(); fragmentStack.push(fragmentToShow); }
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 a v a 2 s. c o m 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:org.onosproject.yangutils.utils.io.impl.YangIoUtils.java
/** * Searches and deletes generated temporary directories. * * @param root root directory//from w ww .jav a2 s . co m * @throws IOException when fails to do IO operations. */ public static void searchAndDeleteTempDir(String root) throws IOException { List<File> store = new LinkedList<>(); Stack<String> stack = new Stack<>(); stack.push(root); while (!stack.empty()) { root = stack.pop(); File file = new File(root); File[] filelist = file.listFiles(); if (filelist == null || filelist.length == 0) { continue; } for (File current : filelist) { if (current.isDirectory()) { stack.push(current.toString()); if (current.getName().endsWith("-Temp")) { store.add(current); } } } } for (File dir : store) { FileUtils.deleteDirectory(dir); } }
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;/* w ww .ja v a 2 s .c om*/ 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:de.mpg.escidoc.services.citationmanager.utils.XsltHelper.java
/** * Check of the balanced tags sup/sub//from ww w . j a v a2s. 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(); }
From source file:org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.java
/** * Searches and deletes generated temporary directories. * * @param root root directory//from w w w . j a v a 2s. c o m * @throws IOException when fails to do IO operations. */ public static void searchAndDeleteTempDir(String root) throws IOException { List<File> store = new LinkedList<>(); Stack<String> stack = new Stack<>(); stack.push(root); while (!stack.empty()) { root = stack.pop(); File file = new File(root); File[] fileList = file.listFiles(); if (fileList == null || fileList.length == 0) { continue; } for (File current : fileList) { if (current.isDirectory()) { stack.push(current.toString()); if (current.getName().endsWith(HYPHEN + TEMP)) { store.add(current); } } } } for (File dir : store) { FileUtils.deleteDirectory(dir); } }
From source file:org.apache.hadoop.hdfs.TestDFSStripedOutputStreamWithFailure.java
private static void getComb(int n, int k, Stack<Integer> stack, List<List<Integer>> res) { if (stack.size() == k) { List<Integer> list = new ArrayList<Integer>(stack); res.add(list);/*from w w w . j a v a 2 s.c o m*/ } else { int next = stack.empty() ? 0 : stack.peek() + 1; while (next < n) { stack.push(next); getComb(n, k, stack, res); next++; } } if (!stack.empty()) { stack.pop(); } }
From source file:org.onehippo.forge.channelmanager.pagesupport.channel.event.DocumentCopyingPageCopyEventListener.java
private static <T> List<T> popAllToList(final Stack<T> stack) { if (stack == null) { return Collections.emptyList(); }//from w w w . j a va2 s.c o m List<T> list = new LinkedList<>(); T object; while (!stack.empty()) { object = stack.pop(); list.add(object); } return list; }