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:net.sourceforge.fenixedu.domain.Lesson.java

public YearMonthDay getNextPossibleLessonInstanceDate() {

    SortedSet<YearMonthDay> allLessonDates = getAllLessonDates();
    LessonInstance lastLessonInstance = getLastLessonInstance();

    if (lastLessonInstance != null) {
        YearMonthDay day = lastLessonInstance.getDay();
        SortedSet<YearMonthDay> nextLessonDates = allLessonDates.tailSet(day);
        nextLessonDates.remove(day);//  w  w w  . jav a2  s  . c  o  m
        return nextLessonDates.isEmpty() ? null : nextLessonDates.first();
    }

    return allLessonDates.isEmpty() ? null : allLessonDates.first();
}

From source file:net.sourceforge.fenixedu.domain.Lesson.java

public LessonInstance getFirstLessonInstance() {
    SortedSet<LessonInstance> result = new TreeSet<LessonInstance>(
            LessonInstance.COMPARATOR_BY_BEGIN_DATE_TIME);
    result.addAll(getLessonInstancesSet());
    return !result.isEmpty() ? result.first() : null;
}

From source file:controllers.Statistics.java

private static void getElapsedTimeStatisticsForSeconds(long secondInMillis, int previousSeconds, String target,
        ObjectNode objectNode) {/* w w  w.  j av a 2s  .  c  o  m*/
    TitanGraph g = Global.getGraph();
    long second = (secondInMillis / 1000) * 1000;

    SortedSet<Integer> numSet = new TreeSet<Integer>();
    int value = 0;
    int sum = 0;
    int count = 0;

    for (int i = (previousSeconds - 1); i > -1; i--) {
        //Logger.info("previousSeconds=" + (second - (i * 1000)));
        Iterator<Vertex> it = g.getVertices("second", second - (i * 1000)).iterator();
        Vertex v = null;

        //if(it.hasNext()) {
        while (it.hasNext()) {
            v = it.next();
            for (Vertex vertex : v.query().labels("include").has("event", target).vertices()) {
                if (vertex == null) {
                    continue;
                }
                value = (Integer) vertex.getProperty("elapsedTime");
                count++;
                sum += value;
                numSet.add(value);
            }
        }
    }

    objectNode.put("cnt", count);
    if (count > 0) {
        objectNode.put("avg", sum / count);
        objectNode.put("min", numSet.first());
        objectNode.put("max", numSet.last());
    } else {
        objectNode.put("avg", 0);
        objectNode.put("min", 0);
        objectNode.put("max", 0);
    }

}

From source file:org.apache.hadoop.hbase.regionserver.Memcache.java

private void getRowKeyAtOrBefore(final ConcurrentSkipListSet<KeyValue> set, final KeyValue kv,
        final NavigableSet<KeyValue> candidates, final NavigableSet<KeyValue> deletes, final long now) {
    if (set.isEmpty()) {
        return;/*from   w  w  w.  j av a2 s.co m*/
    }
    // We want the earliest possible to start searching from.  Start before
    // the candidate key in case it turns out a delete came in later.
    KeyValue search = candidates.isEmpty() ? kv : candidates.first();

    // Get all the entries that come equal or after our search key
    SortedSet<KeyValue> tailset = set.tailSet(search);

    // if there are items in the tail map, there's either a direct match to
    // the search key, or a range of values between the first candidate key
    // and the ultimate search key (or the end of the cache)
    if (!tailset.isEmpty() && this.comparator.compareRows(tailset.first(), search) <= 0) {
        // Keep looking at cells as long as they are no greater than the 
        // ultimate search key and there's still records left in the map.
        KeyValue deleted = null;
        KeyValue found = null;
        for (Iterator<KeyValue> iterator = tailset.iterator(); iterator.hasNext()
                && (found == null || this.comparator.compareRows(found, kv) <= 0);) {
            found = iterator.next();
            if (this.comparator.compareRows(found, kv) <= 0) {
                if (found.isDeleteType()) {
                    Store.handleDeletes(found, candidates, deletes);
                    if (deleted == null) {
                        deleted = found;
                    }
                } else {
                    if (Store.notExpiredAndNotInDeletes(this.ttl, found, now, deletes)) {
                        candidates.add(found);
                    } else {
                        if (deleted == null) {
                            deleted = found;
                        }
                        // TODO: Check this removes the right key.
                        // Its expired.  Remove it.
                        iterator.remove();
                    }
                }
            }
        }
        if (candidates.isEmpty() && deleted != null) {
            getRowKeyBefore(set, deleted, candidates, deletes, now);
        }
    } else {
        // The tail didn't contain any keys that matched our criteria, or was 
        // empty. Examine all the keys that proceed our splitting point.
        getRowKeyBefore(set, search, candidates, deletes, now);
    }
}

From source file:org.jclouds.rackspace.cloudfiles.CloudFilesClientLiveTest.java

@Test
public void testListOwnedContainers() throws Exception {
    String containerPrefix = getContainerName();
    try {/*w  w w  .  java 2  s . c  om*/
        SortedSet<ContainerMetadata> response = context.getApi().listContainers();
        assertNotNull(response);
        long initialContainerCount = response.size();
        assertTrue(initialContainerCount >= 0);

        // Create test containers
        String[] containerJsr330 = new String[] { containerPrefix + ".testListOwnedContainers1",
                containerPrefix + ".testListOwnedContainers2" };
        assertTrue(context.getApi().createContainer(containerJsr330[0]));
        assertTrue(context.getApi().createContainer(containerJsr330[1]));

        // Test default listing
        response = context.getApi().listContainers();
        // assertEquals(response.size(), initialContainerCount + 2);// if the containers already
        // exist, this will fail

        // Test listing with options
        response = context.getApi().listContainers(ListContainerOptions.Builder
                .afterMarker(containerJsr330[0].substring(0, containerJsr330[0].length() - 1)).maxResults(1));
        assertEquals(response.size(), 1);
        assertEquals(response.first().getName(), containerJsr330[0]);

        response = context.getApi()
                .listContainers(ListContainerOptions.Builder.afterMarker(containerJsr330[0]).maxResults(1));
        assertEquals(response.size(), 1);
        assertEquals(response.first().getName(), containerJsr330[1]);

        // Cleanup and test containers have been removed
        assertTrue(context.getApi().deleteContainerIfEmpty(containerJsr330[0]));
        assertTrue(context.getApi().deleteContainerIfEmpty(containerJsr330[1]));
        response = context.getApi().listContainers();
        // assertEquals(response.size(), initialContainerCount + 2);// if the containers already
        // exist, this will fail
    } finally {
        returnContainer(containerPrefix);
    }
}

From source file:com.google.gwt.emultest.java.util.TreeSetTest.java

/**
 * Test method for 'java.util.SortedSet.first()'.
 *
 * @see java.util.SortedSet#first()//from ww w. ja  v a2  s  . c  o m
 */
public void testFirst() {
    SortedSet<E> sortedSet = createNavigableSet();
    // test with a single entry set
    sortedSet.add(getKeys()[0]);
    assertEquals(getKeys()[0], sortedSet.first());
    // is it consistent with other methods
    assertEquals(sortedSet.toArray()[0], sortedSet.first());
    assertEquals(getKeys()[0], sortedSet.last());
    assertEquals(sortedSet.last(), sortedSet.first());

    // test with two entry set
    sortedSet.add(getKeys()[1]);
    assertEquals(getKeys()[0], sortedSet.first());
    assertFalse(getKeys()[1].equals(sortedSet.first()));
    // is it consistent with other methods
    assertEquals(sortedSet.toArray()[0], sortedSet.first());
    assertFalse(getKeys()[0].equals(sortedSet.last()));
    assertFalse(sortedSet.last().equals(sortedSet.first()));
}

From source file:net.sourceforge.fenixedu.domain.Lesson.java

public YearMonthDay getNextPossibleSummaryDate() {

    YearMonthDay currentDate = new YearMonthDay();
    HourMinuteSecond now = new HourMinuteSecond();
    Summary lastSummary = getLastSummary();

    if (lastSummary != null) {

        SortedSet<YearMonthDay> datesEvenToday = getAllLessonDatesUntil(currentDate);
        SortedSet<YearMonthDay> possibleDates = datesEvenToday
                .tailSet(lastSummary.getSummaryDateYearMonthDay());

        possibleDates.remove(lastSummary.getSummaryDateYearMonthDay());
        if (!possibleDates.isEmpty()) {
            YearMonthDay nextPossibleDate = possibleDates.first();
            return isTimeValidToInsertSummary(now, nextPossibleDate) ? nextPossibleDate : null;
        }/*from w  w  w.j  a  va2 s.  c om*/

    } else {
        YearMonthDay nextPossibleDate = hasAnyLessonInstances() ? getFirstLessonInstance().getDay()
                : getLessonStartDay();
        return isTimeValidToInsertSummary(now, nextPossibleDate) ? nextPossibleDate : null;
    }

    return null;
}

From source file:com.redhat.rhn.manager.configuration.test.ConfigurationManagerTest.java

/**
 * 1) Create a central config channel (A) add a bunch of files &amp; dirs
 * 2) Call ConfigurationManager.countCentrallyManagedFiles and verify
 *      that we have the correct answer//from w w w .  ja  v a  2  s.  c  om
 * 3) Create a new channel and add ONE file thats new and One file
 *      duplicate of a file in Channel (A) ...
 * 4) Call ConfigurationManager.countCentrallyManagedFiles and verify
 *      that the answer is number of files in Channel A + 1
 * @throws Exception under exceptional circumstances
 */

public void testCountCentrallyManagedFiles() throws Exception {
    user.getOrg().addRole(RoleFactory.CONFIG_ADMIN);
    user.addPermanentRole(RoleFactory.CONFIG_ADMIN);
    Server s = makeServerForChannelCountTests();
    ConfigFileCount actual = cm.countCentrallyManagedPaths(s, user);

    assertEquals(EXPECTED_COUNT, actual);

    final ConfigChannel c = s.getConfigChannels().get(0);

    SortedSet files = c.getConfigFiles();
    assertEquals(files.size(), EXPECTED_COUNT.getFiles() + EXPECTED_COUNT.getDirectories());
    //now add a new channel -
    // (with some file/ directory intersections)

    ConfigFile fl = (ConfigFile) files.first();
    final String path = fl.getConfigFileName().getPath();

    ConfigChannel cc = ConfigTestUtils.createConfigChannel(user.getOrg());

    //Create a Duplicate Path and add it to the new channel
    final ConfigFile fl2 = cc.createConfigFile(ConfigFileState.normal(), path);
    ConfigTestUtils.createConfigRevision(fl2, ConfigFileType.file());

    //Now Create a NEW Path and add it to the new channel
    ConfigFile fl3 = ConfigTestUtils.createConfigFile(cc);
    ConfigTestUtils.createConfigRevision(fl3, ConfigFileType.file());

    s.subscribeAt(cc, 0);
    ServerFactory.save(s);
    actual = cm.countCentrallyManagedPaths(s, user);

    ConfigFileCount expected = ConfigFileCount.create(EXPECTED_COUNT.getFiles() + 1,
            EXPECTED_COUNT.getDirectories(), 0);
    assertEquals(expected, actual);
}

From source file:com.tesora.dve.tools.aitemplatebuilder.AiTemplateBuilder.java

private String getCommonTableNamePrefix(final SortedSet<TableStats> tables) {
    final String firstName = tables.first().getTableName();
    final String lastName = tables.last().getTableName();

    final int minLength = Math.min(firstName.length(), lastName.length());
    for (int i = 0; i < minLength; ++i) {
        if ((i > (TABLE_NAME_MIN_PREFIX_LENGTH - 1)) && (firstName.charAt(i) != lastName.charAt(i))) {
            return firstName.substring(0, i);
        }//from  w w w.  j  av  a 2s .c  o m
    }

    return firstName.substring(0, minLength);
}

From source file:com.google.gwt.emultest.java.util.TreeSetTest.java

/**
 * Test method for 'java.util.SortedSet.last()'.
 *
 * @see java.util.SortedSet#last()/*from   ww w .  ja  v  a 2 s .  c om*/
 */
public void testLastKey() {
    SortedSet<E> sortedSet = createNavigableSet();

    // test with a single entry set
    sortedSet.add(getKeys()[0]);
    assertEquals(getKeys()[0], sortedSet.last());
    // is it consistent with other methods
    assertEquals(sortedSet.toArray()[0], sortedSet.last());
    assertEquals(getKeys()[0], sortedSet.first());
    assertEquals(sortedSet.first(), sortedSet.last());

    // test with two entry set
    sortedSet.add(getKeys()[1]);
    assertEquals(getKeys()[1], sortedSet.last());
    assertFalse(getKeys()[0].equals(sortedSet.last()));
    // is it consistent with other methods
    assertEquals(sortedSet.toArray()[1], sortedSet.last());
    assertEquals(getKeys()[0], sortedSet.first());
    assertFalse(sortedSet.first().equals(sortedSet.last()));
}