Example usage for java.util SortedMap lastKey

List of usage examples for java.util SortedMap lastKey

Introduction

In this page you can find the example usage for java.util SortedMap lastKey.

Prototype

K lastKey();

Source Link

Document

Returns the last (highest) key currently in this map.

Usage

From source file:org.jasig.schedassist.web.VisibleScheduleTag.java

@Override
public int doStartTagInternal() {
    final ServletContext servletContext = pageContext.getServletContext();

    final Date scheduleStart = visibleSchedule.getScheduleStart();
    if (null == scheduleStart) {
        // the visibleSchedule is empty, short circuit
        try {//from w ww .  j  av a2s. c o  m
            StringBuilder noappointments = new StringBuilder();
            noappointments.append("<span class=\"none-available\">");
            noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null));
            noappointments.append("</span>");
            pageContext.getOut().write(noappointments.toString());
        } catch (IOException e) {
            LOG.error("IOException occurred in doStartTag", e);
        }
        // SKIP_BODY means don't print any content from body of tag
        return SKIP_BODY;
    }

    LOG.debug("scheduleStart: " + scheduleStart);
    SortedMap<Date, List<AvailableBlock>> dailySchedules = new TreeMap<Date, List<AvailableBlock>>();
    Date index = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE);
    Date scheduleEnd = visibleSchedule.getScheduleEnd();
    while (index.before(scheduleEnd)) {
        dailySchedules.put(index, new ArrayList<AvailableBlock>());
        index = DateUtils.addDays(index, 1);
    }
    final Date lastMapKey = dailySchedules.lastKey();
    LOG.debug("visibleSchedule spans " + dailySchedules.keySet().size() + " days");

    try {
        SortedMap<AvailableBlock, AvailableStatus> scheduleBlockMap = visibleSchedule.getBlockMap();
        int numberOfEventsToDisplay = 0;
        for (AvailableBlock block : scheduleBlockMap.keySet()) {
            Date eventStartDate = block.getStartTime();
            LOG.debug("event start date: " + eventStartDate);
            Date mapKey = DateUtils.truncate(eventStartDate, java.util.Calendar.DATE);
            if (CommonDateOperations.equalsOrAfter(eventStartDate, scheduleStart)
                    && dailySchedules.containsKey(mapKey)) {
                dailySchedules.get(mapKey).add(block);
                numberOfEventsToDisplay++;
            }
        }
        LOG.debug("number of events to display: " + numberOfEventsToDisplay);
        if (numberOfEventsToDisplay == 0) {
            // no available times in this range!
            StringBuilder noappointments = new StringBuilder();
            noappointments.append("<span class=\"none-available\">");
            noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null));
            noappointments.append("</span>");
            pageContext.getOut().write(noappointments.toString());
        } else {
            int weekNumber = 1;
            Date currentWeekStart = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE);
            Date currentWeekFinish = DateUtils.addDays(currentWeekStart,
                    CommonDateOperations.numberOfDaysUntilSunday(currentWeekStart));
            currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1);

            boolean renderAnotherWeek = true;

            while (renderAnotherWeek) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("will render another week using currentWeekStart " + currentWeekStart
                            + " and currentWeekFinish " + currentWeekFinish);
                }
                SortedMap<Date, List<AvailableBlock>> subMap = dailySchedules.subMap(currentWeekStart,
                        currentWeekFinish);
                renderWeek(servletContext, pageContext.getOut(), weekNumber++, subMap, scheduleBlockMap);

                currentWeekStart = DateUtils.addMinutes(currentWeekFinish, 1);
                currentWeekFinish = DateUtils.addDays(currentWeekStart, 7);
                currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("recalculated currentWeekStart " + currentWeekStart + ", currentWeekFinish "
                            + currentWeekFinish);
                }

                if (currentWeekStart.after(lastMapKey)) {
                    renderAnotherWeek = false;
                    LOG.debug("will not render another week");
                }
            }
        }

    } catch (IOException e) {
        LOG.error("IOException occurred in doStartTag", e);
    }
    // SKIP_BODY means don't print any content from body of tag
    return SKIP_BODY;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see edu.umd.cfar.lamp.viper.util.IntervalIndexList#firstBefore(java.lang.Comparable)
 *//*from  w  w w.ja  v a 2  s  . c  o m*/
public Comparable firstBefore(Comparable oldStart) {
    SortedMap head = spans.headMap(oldStart);
    if (head.size() > 0) {
        return (Comparable) head.lastKey();
    }
    return null;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

public Comparable firstBeforeOrAt(Comparable oldStart) {
    if (spans.containsKey(oldStart)) {
        return oldStart;
    }//from  w  w w  .ja  v a2s . c  o m
    SortedMap head = spans.headMap(oldStart);
    if (head.size() > 0) {
        return (Comparable) head.lastKey();
    }
    return null;
}

From source file:org.jasig.schedassist.portlet.VisibleScheduleTag.java

@Override
public int doStartTagInternal() {
    RenderRequest renderRequest = (RenderRequest) pageContext.getRequest().getAttribute(PORTLET_REQUEST);
    RenderResponse renderResponse = (RenderResponse) pageContext.getRequest().getAttribute(PORTLET_RESPONSE);

    final Date scheduleStart = visibleSchedule.getScheduleStart();
    if (null == scheduleStart) {
        // the visibleSchedule is empty, short circuit
        try {/*from  w  ww. jav  a  2s.com*/
            StringBuilder noappointments = new StringBuilder();
            noappointments.append("<span class=\"none-available\">");
            noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null));
            noappointments.append("</span>");
            pageContext.getOut().write(noappointments.toString());
        } catch (IOException e) {
            LOG.error("IOException occurred in doStartTag", e);
        }
        // SKIP_BODY means don't print any content from body of tag
        return SKIP_BODY;
    }

    LOG.debug("scheduleStart: " + scheduleStart);
    SortedMap<Date, List<AvailableBlock>> dailySchedules = new TreeMap<Date, List<AvailableBlock>>();
    Date index = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE);
    Date scheduleEnd = visibleSchedule.getScheduleEnd();
    while (index.before(scheduleEnd)) {
        dailySchedules.put(index, new ArrayList<AvailableBlock>());
        index = DateUtils.addDays(index, 1);
    }
    final Date lastMapKey = dailySchedules.lastKey();
    LOG.debug("visibleSchedule spans " + dailySchedules.keySet().size() + " days");

    try {
        SortedMap<AvailableBlock, AvailableStatus> scheduleBlockMap = visibleSchedule.getBlockMap();
        int numberOfEventsToDisplay = 0;
        for (AvailableBlock block : scheduleBlockMap.keySet()) {
            Date eventStartDate = block.getStartTime();
            LOG.debug("event start date: " + eventStartDate);
            Date mapKey = DateUtils.truncate(eventStartDate, java.util.Calendar.DATE);
            if (CommonDateOperations.equalsOrAfter(eventStartDate, scheduleStart)
                    && dailySchedules.containsKey(mapKey)) {
                dailySchedules.get(mapKey).add(block);
                numberOfEventsToDisplay++;
            }
        }
        LOG.debug("number of events to display: " + numberOfEventsToDisplay);
        if (numberOfEventsToDisplay == 0) {
            // no available times in this range!
            StringBuilder noappointments = new StringBuilder();
            noappointments.append("<span class=\"none-available\">");
            noappointments.append(getMessageSource().getMessage("no.available.appointments", null, null));
            noappointments.append("</span>");
            pageContext.getOut().write(noappointments.toString());
        } else {
            int weekNumber = 1;
            Date currentWeekStart = DateUtils.truncate(scheduleStart, java.util.Calendar.DATE);
            Date currentWeekFinish = DateUtils.addDays(currentWeekStart,
                    CommonDateOperations.numberOfDaysUntilSunday(currentWeekStart));
            currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1);

            boolean renderAnotherWeek = true;

            while (renderAnotherWeek) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("will render another week using currentWeekStart " + currentWeekStart
                            + " and currentWeekFinish " + currentWeekFinish);
                }
                SortedMap<Date, List<AvailableBlock>> subMap = dailySchedules.subMap(currentWeekStart,
                        currentWeekFinish);
                renderWeek(pageContext.getOut(), weekNumber++, subMap, scheduleBlockMap, renderRequest,
                        renderResponse);

                currentWeekStart = DateUtils.addMinutes(currentWeekFinish, 1);
                currentWeekFinish = DateUtils.addDays(currentWeekStart, 7);
                currentWeekFinish = DateUtils.addMinutes(currentWeekFinish, -1);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("recalculated currentWeekStart " + currentWeekStart + ", currentWeekFinish "
                            + currentWeekFinish);
                }

                if (currentWeekStart.after(lastMapKey)) {
                    renderAnotherWeek = false;
                    LOG.debug("will not render another week");
                }
            }
        }

    } catch (IOException e) {
        LOG.error("IOException occurred in doStartTag", e);
    }
    // SKIP_BODY means don't print any content from body of tag
    return SKIP_BODY;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see java.util.Set#contains(java.lang.Object)
 *//*from www  .  ja v  a 2s.  c o  m*/
public boolean contains(Object o) {
    if (spans.size() == 0) {
        return false;
    } else if (o instanceof Comparable) {
        SortedMap m = spans.headMap(o);
        if (m.size() > 0) {
            Comparable e = (Comparable) m.get(m.lastKey());
            if (e.compareTo(o) > 0) {
                return true;
            }
        }
        m = spans.tailMap(o);
        if (m.size() > 0) {
            Comparable s = (Comparable) m.firstKey();
            return s.compareTo(o) == 0;
        }
        return false;
    } else {
        return withinRange((Interval) o);
    }
}

From source file:hudson.plugins.jobConfigHistory.FileHistoryDaoTest.java

private void testGetRevisions(SortedMap<String, HistoryDescr> result) {
    assertEquals(5, result.size());/*from w  w w.j  a  v  a 2 s .  co m*/
    assertEquals("2012-11-21_11-29-12", result.firstKey());
    assertEquals("2012-11-21_11-42-05", result.lastKey());
    final HistoryDescr firstValue = result.get(result.firstKey());
    final HistoryDescr lastValue = result.get(result.lastKey());
    assertEquals("Created", firstValue.getOperation());
    assertEquals("anonymous", firstValue.getUserID());
    assertEquals("Changed", lastValue.getOperation());
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * Subsumes the Instants in the Span into this Range. 
 * @param start the first instant to add
 * @param stop the stop instant, exclusive
 * @return <code>true</code> iff the operation modified this Range
 *//*  w w  w.ja va2 s  .  c o m*/
public boolean add(Comparable start, Comparable stop) {
    Comparable old = (Comparable) spans.get(start);
    if (old != null && old.compareTo(stop) >= 0) {
        return false;
    }
    SortedMap head = spans.headMap(start);
    if (!head.isEmpty()) {
        Comparable oldStart = (Comparable) head.lastKey();
        Comparable oldEnd = (Comparable) head.get(oldStart);
        if (oldEnd.compareTo(stop) >= 0) {
            return false;
        } else {
            if (oldEnd.compareTo(start) >= 0) {
                start = oldStart;
                spans.remove(oldStart);
            }
        }
    }
    SortedMap sub = spans.subMap(start, stop);
    if (!sub.isEmpty()) {
        Comparable oldStart = (Comparable) sub.lastKey();
        Comparable oldEnd = (Comparable) sub.get(oldStart);
        if (oldStart.compareTo(start) == 0 && oldEnd.compareTo(stop) >= 0) {
            return false;
        } else if (oldEnd.compareTo(stop) > 0) {
            stop = oldEnd;
        }
        sub.clear();
    }
    if (spans.containsKey(stop)) {
        stop = (Comparable) spans.remove(stop);
    }
    spans.put(start, stop);
    return true;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see edu.umd.cfar.lamp.viper.util.IntervalIndexList#remove(java.lang.Comparable, java.lang.Comparable)
 *//*from   w  w  w .  ja  v a2s  .  c  o m*/
public boolean remove(Comparable start, Comparable end) {
    boolean someFound = false;
    SortedMap head = spans.headMap(start);
    if (!head.isEmpty()) {
        Comparable oldStart = (Comparable) head.lastKey();
        Comparable oldEnd = (Comparable) head.get(oldStart);
        if (oldEnd.compareTo(start) > 0) {
            // if there is a span that goes into the span to
            // be removed, replace it.
            head.put(oldStart, start);
            someFound = true;
            double toCheck = oldEnd.compareTo(end);
            if (toCheck > 0) {
                // if the span to be removed is a strict subset 
                // of some existing span, you also have
                // to add back the end.
                spans.put(end, oldEnd);
                return true;
            } else if (toCheck == 0) {
                return true;
            }
        }
    }
    SortedMap sub = spans.subMap(start, end);
    if (!sub.isEmpty()) {
        someFound = true;
        Comparable oldStart = (Comparable) sub.lastKey();
        Comparable oldEnd = (Comparable) sub.get(oldStart);
        if (oldEnd.compareTo(end) > 0) {
            // if there is a span that starts during the
            // span to removed that goes past the end,
            // have to add back the difference.
            spans.put(end, oldEnd);
        }
        sub.clear();
    }
    return someFound;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see edu.umd.cfar.lamp.viper.util.IntervalIndexList#endOf(java.lang.Comparable)
 *///from ww w  .  j a v a 2 s . c  o  m
public Comparable endOf(Comparable c) {
    if (c == null) {
        return null;
    }
    Object o = spans.get(c);
    if (o == null) {
        SortedMap head = spans.headMap(c);
        if (head != null && !head.isEmpty()) {
            Comparable last = (Comparable) spans.get(head.lastKey());
            if (last.compareTo(c) >= 0) {
                return last;
            }
        }
        return null;
    } else {
        return (Comparable) o;
    }
}

From source file:viper.api.time.TimeEncodedList.java

/**
 * Get the value at the specified index in the list.
 * @param index the index into the list/* ww  w. j a v  a 2  s .co  m*/
 * @return the value at the specified index
 */
public Object get(Instant index) {
    assert index != null;
    SortedMap m = values.tailMap(index);
    if (!m.isEmpty() && m.firstKey().equals(index)) {
        return ((LelNode) m.get(index)).getValue();
    }
    m = values.headMap(index);
    if (!m.isEmpty()) {
        LelNode v = (LelNode) m.get(m.lastKey());
        if (v.getEnd().compareTo(index) > 0) {
            return v.getValue();
        }
    }
    return null;
}