List of usage examples for java.util SortedSet iterator
Iterator<E> iterator();
From source file:Main.java
public static <T> T findLastItem(SortedSet<T> queryResult, int lastItemPosition) { if (queryResult == null || queryResult.isEmpty()) throw new IllegalArgumentException("Provided result set is 'null' or empty."); Iterator<T> resultSetIterator = queryResult.iterator(); int nbItemInList = 0; T lastItem = null;/*from w w w . j av a 2 s .c om*/ while (resultSetIterator.hasNext() && nbItemInList++ < lastItemPosition) lastItem = resultSetIterator.next(); return lastItem; }
From source file:com.trailmagic.image.ui.WebSupport.java
public static ImageManifestation getMFBySize(Image image, int size, boolean original) { if (original) { return findOriginal(image); }/* w w w .ja v a 2 s .com*/ SortedSet mfs = image.getManifestations(); ImageManifestation closest, tmpMF; Iterator iter = mfs.iterator(); if (iter.hasNext()) { closest = (ImageManifestation) iter.next(); } else { return null; } while (iter.hasNext()) { tmpMF = (ImageManifestation) iter.next(); if (tmpMF.isOriginal()) { continue; } // if tmpMF's area is closer to the target size... if (Math.abs(size - (tmpMF.getArea())) < Math.abs(size - (closest.getArea()))) { closest = tmpMF; } } return closest; }
From source file:org.zaproxy.clientapi.core.WavsepStatic.java
private static String getQueryParamString(SortedSet<String> querySet) { StringBuilder sb = new StringBuilder(); Iterator<String> iterator = querySet.iterator(); for (int i = 0; iterator.hasNext(); i++) { String name = iterator.next(); if (name == null) { continue; }/*from w w w . j av a 2 s . c om*/ if (i > 0) { sb.append(','); } if (name.length() > 40) { // Truncate name = name.substring(0, 40); } sb.append(name); } String result = ""; if (sb.length() > 0) { result = sb.insert(0, '(').append(')').toString(); } return result; }
From source file:Main.java
public static boolean containsAll(Collection a, Collection b) { // fast paths if (a == b)/* w ww . j av a 2s .c o m*/ return true; if (b.size() == 0) return true; if (a.size() < b.size()) return false; if (a instanceof SortedSet && b instanceof SortedSet) { SortedSet aa = (SortedSet) a; SortedSet bb = (SortedSet) b; Comparator bbc = bb.comparator(); Comparator aac = aa.comparator(); if (bbc == null && aac == null) { Iterator ai = aa.iterator(); Iterator bi = bb.iterator(); Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0 Comparable bo = (Comparable) bi.next(); while (true) { int rel = ao.compareTo(bo); if (rel == 0) { if (!bi.hasNext()) return true; if (!ai.hasNext()) return false; bo = (Comparable) bi.next(); ao = (Comparable) ai.next(); } else if (rel < 0) { if (!ai.hasNext()) return false; ao = (Comparable) ai.next(); } else { return false; } } } else if (bbc.equals(aac)) { Iterator ai = aa.iterator(); Iterator bi = bb.iterator(); Object ao = ai.next(); // these are ok, since the sizes are != 0 Object bo = bi.next(); while (true) { int rel = aac.compare(ao, bo); if (rel == 0) { if (!bi.hasNext()) return true; if (!ai.hasNext()) return false; bo = bi.next(); ao = ai.next(); } else if (rel < 0) { if (!ai.hasNext()) return false; ao = ai.next(); } else { return false; } } } } return a.containsAll(b); }
From source file:Main.java
/** * @param a// w w w .ja va 2s. c om * @param b * @return */ public static boolean containsSome(Collection a, Collection b) { // fast paths if (a.size() == 0 || b.size() == 0) return false; if (a == b) return true; // must test after size test. if (a instanceof SortedSet && b instanceof SortedSet) { SortedSet aa = (SortedSet) a; SortedSet bb = (SortedSet) b; Comparator bbc = bb.comparator(); Comparator aac = aa.comparator(); if (bbc == null && aac == null) { Iterator ai = aa.iterator(); Iterator bi = bb.iterator(); Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0 Comparable bo = (Comparable) bi.next(); while (true) { int rel = ao.compareTo(bo); if (rel < 0) { if (!ai.hasNext()) return false; ao = (Comparable) ai.next(); } else if (rel > 0) { if (!bi.hasNext()) return false; bo = (Comparable) bi.next(); } else { return true; } } } else if (bbc.equals(a)) { Iterator ai = aa.iterator(); Iterator bi = bb.iterator(); Object ao = ai.next(); // these are ok, since the sizes are != 0 Object bo = bi.next(); while (true) { int rel = aac.compare(ao, bo); if (rel < 0) { if (!ai.hasNext()) return false; ao = ai.next(); } else if (rel > 0) { if (!bi.hasNext()) return false; bo = bi.next(); } else { return true; } } } } for (Iterator it = a.iterator(); it.hasNext();) { if (b.contains(it.next())) return true; } return false; }
From source file:Main.java
public static int removeUnusedLines(String filename, SortedSet<Integer> linesSet) { try {//from www .ja v a2 s.co m BufferedReader br = new BufferedReader(new FileReader(filename)); //String buffer to store contents of the file StringBuffer sb = new StringBuffer(""); int lineNumber = 1; String line; Iterator iterator = linesSet.iterator(); int lineNumberToBeDeleted = (int) iterator.next(); int count = 0; while ((line = br.readLine()) != null) { if (lineNumber == lineNumberToBeDeleted) { if (iterator.hasNext()) { lineNumberToBeDeleted = (int) iterator.next(); count++; } } else { sb.append(line + "\n"); } lineNumber++; } FileWriter fw = new FileWriter(new File(filename)); //Write entire string buffer into the file fw.write(sb.toString()); fw.close(); System.err.println("deleted lines" + count); } catch (Exception e) { System.err.println("error:" + e.getMessage()); e.printStackTrace(); return -10; } return 10; }
From source file:ai.susi.json.JsonRepository.java
private static SortedSet<File> tailSet(SortedSet<File> set, int count) { if (count >= set.size()) return set; TreeSet<File> t = new TreeSet<File>(); Iterator<File> fi = set.iterator(); for (int i = 0; i < set.size() - count; i++) fi.next();//from ww w. j av a 2 s . co m while (fi.hasNext()) t.add(fi.next()); return t; }
From source file:uniol.apt.analysis.synthesize.FindWords.java
static private String normalizeWord(List<Character> word, SortedSet<Character> alphabet) { StringBuilder result = new StringBuilder(); Map<Character, Character> morphism = new HashMap<>(); Iterator<Character> alphabetIter = alphabet.iterator(); for (Character letter : word) { Character replacement = morphism.get(letter); if (replacement == null) { assert alphabetIter.hasNext(); replacement = alphabetIter.next(); morphism.put(letter, replacement); }// ww w . ja va 2 s . c o m result.append(replacement); } return result.toString(); }
From source file:net.commerce.zocalo.freechart.ChartGenerator.java
static public void initializeSeries(Dictionary<String, TimePeriodValues> positions, TimePeriodValuesCollection allValues, Claim claim) { Position[] allPositions = claim.positions(); if (allPositions.length == 2) { int less = allPositions[0].comparePersistentId(allPositions[1]); Position pos = allPositions[less < 0 ? 0 : 1]; // pick one stably TimePeriodValues values = new TimePeriodValues(pos.getName()); positions.put(pos.getName(), values); allValues.addSeries(values);//w ww .ja va 2 s .c om } else { SortedSet<Position> sortedPositions = claim.sortPositions(); for (Iterator<Position> positionIterator = sortedPositions.iterator(); positionIterator.hasNext();) { Position pos = positionIterator.next(); TimePeriodValues values = new TimePeriodValues(pos.getName()); positions.put(pos.getName(), values); allValues.addSeries(values); } } }
From source file:com.cyberway.issue.net.PublicSuffixes.java
protected static void buildRegex(String stem, StringBuilder regex, SortedSet<String> prefixes) { if (prefixes.isEmpty()) { return;// ww w .ja v a 2s . co m } if (prefixes.size() == 1 && prefixes.first().equals(stem)) { // avoid unnecessary "(?:)" return; } regex.append("(?:"); if (stem.length() == 0) { regex.append("\n "); // linebreak-space before first character } Iterator<String> iter = prefixes.iterator(); char c = 0; while (iter.hasNext()) { String s = iter.next(); if (s.length() > stem.length()) { char d = s.charAt(stem.length()); if (d == '+') { // convert exception to zero-width-positive-lookahead regex.append("(?=" + s.substring(stem.length() + 1) + ")"); } else { if (d == c) { continue; } c = d; regex.append(c); String newStem = s.substring(0, stem.length() + 1); SortedSet<String> tail = prefixes.tailSet(newStem); SortedSet<String> range = null; successor: for (String candidate : tail) { if (!candidate.equals(newStem)) { range = prefixes.subSet(s, candidate); break successor; } } if (range == null) { range = prefixes.tailSet(s); } buildRegex(newStem, regex, range); } regex.append('|'); } else { // empty suffix; insert dummy to be eaten when loop exits regex.append('@'); } } // eat the trailing '|' (if no empty '@') or dummy regex.deleteCharAt(regex.length() - 1); regex.append(')'); if (stem.length() == 1) { regex.append('\n'); // linebreak for TLDs } }