Example usage for java.util SortedSet first

List of usage examples for java.util SortedSet first

Introduction

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

Prototype

E first();

Source Link

Document

Returns the first (lowest) element currently in this set.

Usage

From source file:com.github.robozonky.app.version.UpdateMonitor.java

static VersionIdentifier parseNodeList(final NodeList nodeList) {
    final SortedSet<String> versions = IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item)
            .map(Node::getTextContent)
            .collect(Collectors.toCollection(() -> new TreeSet<>(new VersionComparator().reversed())));
    // find latest stable
    final String firstStable = UpdateMonitor.findFirstStable(versions);
    // and check if it is followed by any other unstable versions
    final String first = versions.first();
    if (Objects.equals(first, firstStable)) {
        return new VersionIdentifier(firstStable);
    } else {/*from   w w w  .j a v a  2  s.com*/
        return new VersionIdentifier(firstStable, first);
    }
}

From source file:org.apache.ambari.server.utils.StageUtils.java

/**
 * Finds ranges in sorted set and replaces ranges by compact notation
 * <p/>/*from  www  .  j  a  v a2 s  .  c  o  m*/
 * <p>For example, suppose <tt>set</tt> comprises<tt> [1, 2, 3, 4, 7]</tt>.
 * After invoking <tt>rangedSet = StageUtils.replaceRanges(set)</tt>
 * <tt>rangedSet</tt> will comprise
 * <tt>["1-4", "7"]</tt>..
 *
 * @param set the source set to be ranged
 */
public static Set<String> replaceRanges(SortedSet<Integer> set) {

    if (set == null) {
        return null;
    }

    Set<String> rangedSet = new HashSet<String>();

    Integer prevElement = null;
    Integer startOfRange = set.first();

    for (Integer i : set) {
        if (prevElement != null && (i - prevElement) > 1) {
            String rangeItem = getRangedItem(startOfRange, prevElement);
            rangedSet.add(rangeItem);
            startOfRange = i;
        }
        prevElement = i;
    }

    rangedSet.add(getRangedItem(startOfRange, prevElement));

    return rangedSet;
}

From source file:com.espertech.esper.schedule.ScheduleComputeHelper.java

private static int nextValue(SortedSet<Integer> valueSet, int startValue) {
    if (valueSet == null) {
        return startValue;
    }// w  w  w . j av  a  2s .c  om

    if (valueSet.contains(startValue)) {
        return startValue;
    }

    SortedSet<Integer> tailSet = valueSet.tailSet(startValue + 1);

    if (tailSet.isEmpty()) {
        return -1;
    } else {
        return tailSet.first();
    }
}

From source file:edu.brown.utils.CollectionUtil.java

/**
 * Return the ith element of a set. Super lame
 * //from  w ww. jav  a2 s .co m
 * @param <T>
 * @param items
 * @param idx
 * @return
 */
public static <T> T get(Iterable<T> items, int idx) {
    if (items == null) {
        return (null);
    } else if (items instanceof List<?>) {
        return ((List<T>) items).get(idx);
    } else if (items instanceof ListOrderedSet<?>) {
        return ((ListOrderedSet<T>) items).get(idx);
    } else if (items instanceof SortedSet<?> && idx == 0) {
        SortedSet<T> set = (SortedSet<T>) items;
        return (set.isEmpty() ? null : set.first());
    }
    int ctr = 0;
    for (T t : items) {
        if (ctr++ == idx)
            return (t);
    }
    return (null);
}

From source file:org.artifactory.maven.MavenModelUtils.java

public static Metadata buildReleasesMavenMetadata(String organization, String module,
        SortedSet<String> sortedVersions) {
    Metadata metadata = new Metadata();
    metadata.setGroupId(organization);/* ww w . j a v  a  2  s .  co m*/
    metadata.setArtifactId(module);
    if (!sortedVersions.isEmpty()) {
        metadata.setVersion(sortedVersions.first());
        Versioning versioning = new Versioning();
        metadata.setVersioning(versioning);
        versioning.setVersions(Lists.newArrayList(sortedVersions));
        versioning.setLastUpdatedTimestamp(new Date());
        versioning.setLatest(sortedVersions.last());
        versioning.setRelease(sortedVersions.last());
    }
    return metadata;
}

From source file:org.fao.geonet.component.csw.GetDomain.java

/**
 * Create value element for each item of the string array
 *//*from w ww . ja v  a2  s  .  c  o  m*/
private static List<Element> createValuesElement(SortedSet<String> sortedValues, boolean isRange) {
    List<Element> valuesList = new ArrayList<Element>();
    if (!isRange) {
        for (String value : sortedValues) {
            valuesList.add(new Element("Value", Csw.NAMESPACE_CSW).setText(value));
        }
    } else {
        valuesList.add(new Element("MinValue", Csw.NAMESPACE_CSW).setText(sortedValues.first()));
        valuesList.add(new Element("MaxValue", Csw.NAMESPACE_CSW).setText(sortedValues.last()));
    }
    return valuesList;
}

From source file:org.mwc.debrief.track_shift.views.StackedDotHelper.java

/**
 * sort out data of interest// w  w  w  .  j a  v  a 2  s . co  m
 * 
 */
public static TreeSet<Doublet> getDoublets(final TrackWrapper sensorHost, final ISecondaryTrack targetTrack,
        final boolean onlyVis, final boolean needBearing, final boolean needFrequency) {
    final TreeSet<Doublet> res = new TreeSet<Doublet>();

    // friendly fix-wrapper to save us repeatedly creating it
    final FixWrapper index = new FixWrapper(new Fix(null, new WorldLocation(0, 0, 0), 0.0, 0.0));

    // loop through our sensor data
    final Enumeration<Editable> sensors = sensorHost.getSensors().elements();
    if (sensors != null) {
        while (sensors.hasMoreElements()) {
            final SensorWrapper wrapper = (SensorWrapper) sensors.nextElement();
            if (!onlyVis || (onlyVis && wrapper.getVisible())) {
                final Enumeration<Editable> cuts = wrapper.elements();
                while (cuts.hasMoreElements()) {
                    final SensorContactWrapper scw = (SensorContactWrapper) cuts.nextElement();
                    if (!onlyVis || (onlyVis && scw.getVisible())) {
                        // is this cut suitable for what we're looking for?
                        if (needBearing) {
                            if (!scw.getHasBearing())
                                continue;
                        }

                        // aaah, but does it meet the frequency requirement?
                        if (needFrequency) {
                            if (!scw.getHasFrequency())
                                continue;
                        }

                        FixWrapper targetFix = null;
                        TrackSegment targetParent = null;

                        if (targetTrack != null) {
                            // right, get the track segment and fix nearest to
                            // this
                            // DTG
                            final Enumeration<Editable> trkData = targetTrack.segments();
                            final Vector<TrackSegment> _theSegments = new Vector<TrackSegment>();

                            while (trkData.hasMoreElements()) {

                                final Editable thisI = trkData.nextElement();
                                if (thisI instanceof SegmentList) {
                                    final SegmentList thisList = (SegmentList) thisI;
                                    final Enumeration<Editable> theElements = thisList.elements();
                                    while (theElements.hasMoreElements()) {
                                        final TrackSegment ts = (TrackSegment) theElements.nextElement();
                                        _theSegments.add(ts);
                                    }

                                }
                                if (thisI instanceof TrackSegment) {
                                    final TrackSegment ts = (TrackSegment) thisI;
                                    _theSegments.add(ts);
                                }
                            }

                            if (_theSegments.size() > 0) {
                                final Iterator<TrackSegment> iter = _theSegments.iterator();
                                while (iter.hasNext()) {
                                    final TrackSegment ts = iter.next();

                                    final TimePeriod validPeriod = new TimePeriod.BaseTimePeriod(ts.startDTG(),
                                            ts.endDTG());
                                    if (validPeriod.contains(scw.getDTG())) {
                                        // sorted. here we go
                                        targetParent = ts;

                                        // create an object with the right time
                                        index.getFix().setTime(scw.getDTG());

                                        // and find any matching items
                                        final SortedSet<Editable> items = ts.tailSet(index);
                                        if (items.size() > 0) {
                                            targetFix = (FixWrapper) items.first();
                                        }
                                    }

                                }
                            }
                        }

                        final Watchable[] matches = sensorHost.getNearestTo(scw.getDTG());
                        if ((matches != null) && (matches.length > 0) && (targetFix != null)) {
                            final FixWrapper hostFix = (FixWrapper) matches[0];

                            final Doublet thisDub = new Doublet(scw, targetFix, targetParent, hostFix);

                            // if we've no target track add all the points
                            if (targetTrack == null) {
                                // store our data
                                res.add(thisDub);
                            } else {
                                // if we've got a target track we only add points
                                // for which we
                                // have
                                // a target location
                                if (targetFix != null) {
                                    // store our data
                                    res.add(thisDub);
                                }
                            } // if we know the track
                        } // if there are any matching items
                          // if we find a match
                    } // if cut is visible
                } // loop through cuts
            } // if sensor is visible
        } // loop through sensors
    } // if there are sensors

    return res;
}

From source file:org.apache.hadoop.hdfs.qjournal.client.TestQJMWithFaults.java

/**
 * Run through the creation of a log without any faults injected,
 * and count how many RPCs are made to each node. This sets the
 * bounds for the other test cases, so they can exhaustively explore
 * the space of potential failures.//from w w w  . j  a  v a  2s.  c  o  m
 */
private static long determineMaxIpcNumber() throws Exception {
    Configuration conf = new Configuration();
    MiniJournalCluster cluster = new MiniJournalCluster.Builder(conf).build();
    QuorumJournalManager qjm = null;
    long ret;
    try {
        qjm = createInjectableQJM(cluster);
        qjm.format(FAKE_NSINFO);
        doWorkload(cluster, qjm);

        SortedSet<Integer> ipcCounts = Sets.newTreeSet();
        for (AsyncLogger l : qjm.getLoggerSetForTests().getLoggersForTests()) {
            InvocationCountingChannel ch = (InvocationCountingChannel) l;
            ch.waitForAllPendingCalls();
            ipcCounts.add(ch.getRpcCount());
        }

        // All of the loggers should have sent the same number of RPCs, since there
        // were no failures.
        assertEquals(1, ipcCounts.size());

        ret = ipcCounts.first();
        LOG.info("Max IPC count = " + ret);
    } finally {
        IOUtils.closeStream(qjm);
        cluster.shutdown();
    }
    return ret;
}

From source file:org.ngrinder.perftest.repository.PerfTestRepositoryTest.java

@SuppressWarnings("serial")
@Test/* ww w. j  a va 2s .  c o  m*/
public void testPerfTestTag() {
    PerfTest entity = new PerfTest();
    entity.setTestName("test1");
    entity.setTags(new TreeSet<Tag>() {
        {
            add(new Tag("hello"));
            add(new Tag("world"));
        }
    });
    entity = perfTestRepository.save(entity);
    PerfTest findOne = perfTestRepository.findOne(entity.getId());
    SortedSet<Tag> tags = findOne.getTags();
    assertThat(tags.first(), is(new Tag("hello")));
    assertThat(tags.last(), is(new Tag("world")));
}

From source file:org.ngrinder.perftest.repository.PerfTestRepositoryTest.java

@SuppressWarnings("serial")
@Test// w  w  w  . j  a  v a2 s.c  om
public void testPerfTestTag2() {
    final Tag hello = tagRepository.save(new Tag("hello"));
    final Tag world = tagRepository.save(new Tag("world"));
    final Tag world2 = tagRepository.save(new Tag("world2"));

    PerfTest entity = new PerfTest();
    entity.setTestName("test1");
    entity.setTags(new TreeSet<Tag>() {
        {
            add(hello);
            add(world);
        }
    });
    entity = perfTestRepository.save(entity);
    SortedSet<Tag> tags2 = entity.getTags();
    assertThat(tags2.first(), is(hello));
    assertThat(tags2.last(), is(world));

    PerfTest entity2 = new PerfTest();
    entity2.setTestName("test1");
    entity2.setTags(new TreeSet<Tag>() {
        {
            add(hello);
            add(world2);
        }
    });
    perfTestRepository.save(entity2);
    assertThat(tagRepository.findAll().size(), is(3));
    assertThat(perfTestRepository.findAll(PerfTestSpecification.hasTag("world")).size(), is(1));
    assertThat(perfTestRepository.findAll(PerfTestSpecification.hasTag("hello")).size(), is(2));
    assertThat(tagRepository.findAll().size(), is(3));
}