Example usage for java.util SortedSet subSet

List of usage examples for java.util SortedSet subSet

Introduction

In this page you can find the example usage for java.util SortedSet subSet.

Prototype

SortedSet<E> subSet(E fromElement, E toElement);

Source Link

Document

Returns a view of the portion of this set whose elements range from fromElement , inclusive, to toElement , exclusive.

Usage

From source file:com.enitalk.controllers.youtube.CalendarTest.java

public static void main(String[] args) throws IOException {

    InputStream is = new ClassPathResource("events.json").getInputStream();
    ObjectMapper jackson = new ObjectMapper();
    JsonNode tree = jackson.readTree(is);
    IOUtils.closeQuietly(is);//ww w  .j  a  v a 2 s  .  co  m

    DateTimeFormatter fmtDateTime = ISODateTimeFormat.dateTimeNoMillis();
    DateTimeFormatter fmt = ISODateTimeFormat.date();

    TreeMultimap<DateTime, DateTime> set = CalendarTest.getPeriodSet(10, 18);

    Iterator<JsonNode> nodes = tree.elements();
    while (nodes.hasNext()) {
        JsonNode ev = nodes.next();
        boolean isFullDay = ev.path("start").has("date");

        DateTime stDate = isFullDay ? fmt.parseDateTime(ev.path("start").path("date").asText())
                : fmtDateTime.parseDateTime(ev.path("start").path("dateTime").asText());

        DateTime enDate = isFullDay ? fmt.parseDateTime(ev.path("end").path("date").asText())
                : fmtDateTime.parseDateTime(ev.path("end").path("dateTime").asText());

        System.out.println("St " + stDate + " en " + enDate);

        int days = Days.daysBetween(stDate, enDate).getDays();
        System.out.println("Days between " + days);
        if (isFullDay) {
            switch (days) {
            case 1:
                set.removeAll(stDate);
                break;
            default:
                while (days-- > 0) {
                    set.removeAll(stDate.plusDays(days));
                }
            }
        } else {
            DateTime copySt = stDate.minuteOfHour().setCopy(0).secondOfMinute().setCopy(0);
            DateTime copyEn = enDate.plusHours(1).minuteOfHour().setCopy(0).secondOfMinute().setCopy(0);

            //                System.out.println("Dates truncated " + copySt + " " + copyEn);
            //                System.out.println("Ll set " + set);

            //                System.out.println("Getting set for key " + stDate.millisOfDay().setCopy(0));
            SortedSet<DateTime> ss = set.get(stDate.millisOfDay().setCopy(0));
            SortedSet<DateTime> subset = ss.subSet(copySt, copyEn);
            subset.clear();
            set.remove(enDate.millisOfDay().setCopy(0), copyEn);
        }

    }

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
}

From source file:Tail.java

public static void main(String args[]) throws Exception {
    String elements[] = { "Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter\0"));
    System.out.println(set.tailSet("Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Poodle\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter"));
}

From source file:Main.java

public static void main(String[] args) {
    SortedSet<String> names = new TreeSet<>();
    names.add("HTML");
    names.add("Java");
    names.add("SQL");
    names.add("CSS");
    System.out.println("Sorted Set: " + names);
    System.out.println("First: " + names.first());
    System.out.println("Last: " + names.last());

    SortedSet<String> ssBeforeCSS = names.headSet("CSS");
    System.out.println(ssBeforeCSS);

    SortedSet<String> ssBetwenCSSAndHTML = names.subSet("CSS", "HTML");
    System.out.println(ssBetwenCSSAndHTML);

    SortedSet<String> ssBetwenCSSAndHTML2 = names.subSet("CSS", "HTML");
    System.out.println(ssBetwenCSSAndHTML2);

    SortedSet<String> ssCSSAndAfter = names.tailSet("CSS");
    System.out.println(ssCSSAndAfter);

}

From source file:SortedSetDemo.java

public static void main(String[] args) {
    SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
    System.out.println(sortedSet);
    Object low = sortedSet.first(), high = sortedSet.last();
    System.out.println(low);//from w w  w  .  j a  v  a2s  .co  m
    System.out.println(high);
    Iterator it = sortedSet.iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedSet.subSet(low, high));
    System.out.println(sortedSet.headSet(high));
    System.out.println(sortedSet.tailSet(low));
}

From source file:annis.gui.flatquerybuilder.SpanBox.java

@Override
public void textChange(FieldEvents.TextChangeEvent event) {
    String txt = event.getText();
    HashMap<Integer, Collection> levdistvals = new HashMap<Integer, Collection>();
    if (txt.length() > 1) {
        cb.removeAllItems();/*from  w  w w. j  ava  2 s.  com*/
        for (String s : annonames) {
            Integer d = StringUtils.getLevenshteinDistance(removeAccents(txt), removeAccents(s));
            if (levdistvals.containsKey(d)) {
                levdistvals.get(d).add(s);
            }
            if (!levdistvals.containsKey(d)) {
                Set<String> newc = new TreeSet<String>();
                newc.add(s);
                levdistvals.put(d, newc);
            }
        }
        SortedSet<Integer> keys = new TreeSet<Integer>(levdistvals.keySet());
        for (Integer k : keys.subSet(0, 5)) {
            List<String> values = new ArrayList(levdistvals.get(k));
            Collections.sort(values, String.CASE_INSENSITIVE_ORDER);
            for (String v : values) {
                cb.addItem(v);
            }
        }
    }
}

From source file:annis.gui.flatquerybuilder.SearchBox.java

@Override
public void textChange(TextChangeEvent event) {
    if ("specific".equals(sq.getFilterMechanism())) {
        ConcurrentSkipListSet<String> notInYet = new ConcurrentSkipListSet<String>();
        reducingStringComparator esc = new reducingStringComparator();
        String txt = event.getText();
        if (!txt.equals("")) {
            cb.removeAllItems();/*from  www  .jav  a  2  s  .c o m*/
            for (Iterator<String> it = annonames.iterator(); it.hasNext();) {
                String s = it.next();
                if (esc.compare(s, txt) == 0) {
                    cb.addItem(s);
                } else {
                    notInYet.add(s);
                }
            }
            //startsWith
            for (String s : notInYet) {
                if (esc.startsWith(s, txt)) {
                    cb.addItem(s);
                    notInYet.remove(s);
                }
            }
            //contains
            for (String s : notInYet) {
                if (esc.contains(s, txt)) {
                    cb.addItem(s);
                }
            }
        } else {
            //have a look and speed it up
            SpanBox.buildBoxValues(cb, ebene, sq);
        }
    }

    if ("levenshtein".equals(sq.getFilterMechanism())) {
        String txt = event.getText();
        HashMap<Integer, Collection> levdistvals = new HashMap<Integer, Collection>();
        if (txt.length() > 1) {
            cb.removeAllItems();
            for (String s : annonames) {
                Integer d = StringUtils.getLevenshteinDistance(removeAccents(txt), removeAccents(s));
                if (levdistvals.containsKey(d)) {
                    levdistvals.get(d).add(s);
                }
                if (!levdistvals.containsKey(d)) {
                    Set<String> newc = new TreeSet<String>();
                    newc.add(s);
                    levdistvals.put(d, newc);
                }
            }
            SortedSet<Integer> keys = new TreeSet<Integer>(levdistvals.keySet());
            for (Integer k : keys.subSet(0, 5)) {
                List<String> values = new ArrayList(levdistvals.get(k));
                Collections.sort(values, String.CASE_INSENSITIVE_ORDER);
                for (String v : values) {
                    cb.addItem(v);
                }
            }
        }
    }
}

From source file:annis.gui.flatquerybuilder.ValueField.java

@Override
public void textChange(TextChangeEvent event) {
    ReducingStringComparator rsc = sq.getRSC();
    String fm = sq.getFilterMechanism();
    if (!"generic".equals(fm)) {
        ConcurrentSkipListSet<String> notInYet = new ConcurrentSkipListSet<>();
        String txt = event.getText();
        if (!txt.equals("")) {
            scb.removeAllItems();/*  ww w  . j  a v a 2  s .c o  m*/
            for (Iterator<String> it = values.keySet().iterator(); it.hasNext();) {
                String s = it.next();
                if (rsc.compare(s, txt, fm) == 0) {
                    scb.addItem(s);
                } else {
                    notInYet.add(s);
                }
            }
            //startsWith
            for (String s : notInYet) {
                if (rsc.startsWith(s, txt, fm)) {
                    scb.addItem(s);
                    notInYet.remove(s);
                }
            }
            //contains
            for (String s : notInYet) {
                if (rsc.contains(s, txt, fm)) {
                    scb.addItem(s);
                }
            }
        } else {
            buildValues(this.vm);
        }
    } else {
        String txt = event.getText();
        HashMap<Integer, Collection> levdistvals = new HashMap<>();
        if (txt.length() > 1) {
            scb.removeAllItems();
            for (String s : values.keySet()) {
                Integer d = StringUtils.getLevenshteinDistance(removeAccents(txt).toLowerCase(),
                        removeAccents(s).toLowerCase());
                if (levdistvals.containsKey(d)) {
                    levdistvals.get(d).add(s);
                }
                if (!levdistvals.containsKey(d)) {
                    Set<String> newc = new TreeSet<>();
                    newc.add(s);
                    levdistvals.put(d, newc);
                }
            }
            SortedSet<Integer> keys = new TreeSet<>(levdistvals.keySet());
            for (Integer k : keys.subSet(0, 10)) {
                List<String> valueList = new ArrayList(levdistvals.get(k));
                Collections.sort(valueList, String.CASE_INSENSITIVE_ORDER);
                for (String v : valueList) {
                    scb.addItem(v);
                }
            }
        }
    }
}

From source file:com.cyberway.issue.net.PublicSuffixes.java

protected static void buildRegex(String stem, StringBuilder regex, SortedSet<String> prefixes) {
    if (prefixes.isEmpty()) {
        return;/*from   ww  w .j  a va 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
    }
}

From source file:com.palantir.atlasdb.sweep.SweepTaskRunner.java

private Set<Long> getTimestampsToSweep(Cell cell, Collection<Long> timestamps /* start timestamps */,
        @Modified Map<Long, Long> startTsToCommitTs, @Output Set<Cell> sentinelsToAdd, long sweepTimestamp,
        boolean sweepLastCommitted, SweepStrategy sweepStrategy) {
    Set<Long> uncommittedTimestamps = Sets.newHashSet();
    SortedSet<Long> committedTimestampsToSweep = Sets.newTreeSet();
    long maxStartTs = TransactionConstants.FAILED_COMMIT_TS;
    boolean maxStartTsIsCommitted = false;
    for (long startTs : timestamps) {
        long commitTs = ensureCommitTimestampExists(startTs, startTsToCommitTs);

        if (startTs > maxStartTs && commitTs < sweepTimestamp) {
            maxStartTs = startTs;/*  w w w.j a  va 2s .co m*/
            maxStartTsIsCommitted = commitTs != TransactionConstants.FAILED_COMMIT_TS;
        }
        // Note: there could be an open transaction whose start timestamp is equal to
        // sweepTimestamp; thus we want to sweep all cells such that:
        // (1) their commit timestamp is less than sweepTimestamp
        // (2) their start timestamp is NOT the greatest possible start timestamp
        //     passing condition (1)
        if (commitTs > 0 && commitTs < sweepTimestamp) {
            committedTimestampsToSweep.add(startTs);
        } else if (commitTs == TransactionConstants.FAILED_COMMIT_TS) {
            uncommittedTimestamps.add(startTs);
        }
    }

    if (committedTimestampsToSweep.isEmpty()) {
        return uncommittedTimestamps;
    }

    if (sweepStrategy == SweepStrategy.CONSERVATIVE && committedTimestampsToSweep.size() > 1) {
        // We need to add a sentinel if we are removing a committed value
        sentinelsToAdd.add(cell);
    }

    if (sweepLastCommitted && maxStartTsIsCommitted) {
        return Sets.union(uncommittedTimestamps, committedTimestampsToSweep);
    }
    return Sets.union(uncommittedTimestamps,
            committedTimestampsToSweep.subSet(0L, committedTimestampsToSweep.last()));
}