Example usage for org.apache.commons.lang.math LongRange overlapsRange

List of usage examples for org.apache.commons.lang.math LongRange overlapsRange

Introduction

In this page you can find the example usage for org.apache.commons.lang.math LongRange overlapsRange.

Prototype

public boolean overlapsRange(Range range) 

Source Link

Document

Tests whether the specified range overlaps with this range using long comparison.

null is handled and returns false.

Usage

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

private void renderRange(LongRange pRange, LongRange pStartRange, LongRange pArriveRange, boolean pIsStartRange,
        boolean pIsArriveRange, Graphics2D pG2D, TimeSpan pSpanForRange, HashMap<String, Object> pPopupInfo) {
    int rangeStart = 0;
    int rangeWidth = 0;

    if (pRange.overlapsRange(pStartRange)) {
        //start range rendering
        long startDelta = pStartRange.getMinimumLong();
        rangeStart = Math.round((pRange.getMinimumLong() - startDelta) / DateUtils.MILLIS_PER_MINUTE);
        // int rangeEnd = Math.round((pRange.getMaximumLong() - startDelta) / DateUtils.MILLIS_PER_MINUTE);
        rangeWidth = Math//from  ww  w  . j  a v a 2 s  .  c  o m
                .round((pRange.getMaximumLong() - pRange.getMinimumLong()) / DateUtils.MILLIS_PER_MINUTE);
    } else if (pRange.overlapsRange(pArriveRange)) {
        //end range rendering
        long startDelta = pStartRange.getMinimumLong();
        rangeStart = Math.round((pRange.getMinimumLong() - startDelta) / DateUtils.MILLIS_PER_MINUTE);
        // int rangeEnd = Math.round((pRange.getMaximumLong() - arriveDelta) / DateUtils.MILLIS_PER_MINUTE);
        rangeWidth = Math
                .round((pRange.getMaximumLong() - pRange.getMinimumLong()) / DateUtils.MILLIS_PER_MINUTE);
    }
    //correct small widths
    if (rangeWidth == 0) {
        rangeWidth = 5;
    }

    long max = Math.round(
            (pArriveRange.getMaximumLong() - pStartRange.getMinimumLong()) / DateUtils.MILLIS_PER_MINUTE);

    if (rangeStart > max) {
        return;
    }

    SimpleDateFormat f = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
    String labelString = "";
    if (pSpanForRange != null) {
        labelString = pSpanForRange.toString();
    } else {
        labelString = f.format(new Date(pRange.getMinimumLong())) + " bis "
                + f.format(new Date(pRange.getMaximumLong()));
    }
    Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(labelString, pG2D);
    if (pIsStartRange) {
        pG2D.setColor(Color.RED);
        pG2D.fillRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.BLACK);
        pG2D.drawRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.RED);
        pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 14.0f));
        pG2D.drawString(labelString, rangeStart, (int) labelBounds.getHeight());
    } else if (pIsArriveRange) {
        pG2D.setColor(Color.GREEN.darker());
        pG2D.fillRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.BLACK);
        pG2D.drawRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.GREEN.darker());
        pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 14.0f));
        pG2D.drawString(labelString, rangeStart, (int) labelBounds.getHeight() + 40);
    } else {
        pG2D.fillRect(rangeStart, 20, rangeWidth, 20);
        pG2D.setColor(Color.BLACK);
        pG2D.drawRect(rangeStart, 20, rangeWidth, 20);
        Point loc = getMousePosition();
        if (loc != null && new Rectangle(rangeStart, 20, rangeWidth, 20).contains(loc)) {
            pPopupInfo.put("popup.location", loc);
            pPopupInfo.put("popup.label", labelString);
            pPopupInfo.put("active.range", pRange);
            pPopupInfo.put("span.for.range", pSpanForRange);

        }
    }
}

From source file:VASSAL.chat.CgiServerStatus.java

private ServerStatus.ModuleSummary[] getHistory(long time) {
    if (time <= 0)
        return getStatus();

    final long now = System.currentTimeMillis();

    // start with new interval
    final LongRange req = new LongRange(now - time, now);
    final ArrayList<LongRange> toRequest = new ArrayList<LongRange>();
    toRequest.add(req);//w w  w  . j a va  2  s. co  m

    // subtract each old interval from new interval
    for (LongRange y : requests) {
        for (ListIterator<LongRange> i = toRequest.listIterator(); i.hasNext();) {
            final LongRange x = i.next();

            if (!x.overlapsRange(y))
                continue; // no overlap, nothing to subtract

            // otherwise, remove x and add what remains after subtracting y
            i.remove();

            final long xl = x.getMinimumLong();
            final long xr = x.getMaximumLong();
            final long yl = y.getMinimumLong();
            final long yr = y.getMaximumLong();

            if (xl < yl && yl <= xr)
                i.add(new LongRange(xl, yl));
            if (xl <= yr && yr < xr)
                i.add(new LongRange(yr, xr));
        }
    }

    // now toRequest contains the intervals we are missing; request those
    for (LongRange i : toRequest) {
        for (String s : getInterval(i)) {
            final SequenceEncoder.Decoder st = new SequenceEncoder.Decoder(s, '\t');
            try {
                final String moduleName = st.nextToken();
                final String roomName = st.nextToken();
                final String playerName = st.nextToken();
                final Long when = Long.valueOf(st.nextToken());

                List<String[]> l = records.get(when);
                if (l == null) {
                    l = new ArrayList<String[]>();
                    records.put(when, l);
                }

                l.add(new String[] { moduleName, roomName, playerName });
            }
            // FIXME: review error message
            catch (NoSuchElementException e) {
                e.printStackTrace();
            }
            // FIXME: review error message
            catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        requests.add(i);
    }

    // Join intervals to minimize the number we store.
    // Note: This is simple, but quadratic in the number of intervals.
    // For large numbers of intervals, use an interval tree instead.
    for (int i = 0; i < requests.size(); i++) {
        final LongRange a = requests.get(i);
        for (int j = i + 1; j < requests.size(); j++) {
            final LongRange b = requests.get(j);
            if (a.overlapsRange(b)) {
                final long al = a.getMinimumLong();
                final long ar = a.getMaximumLong();
                final long bl = b.getMinimumLong();
                final long br = b.getMaximumLong();

                requests.set(i, new LongRange(Math.min(al, bl), Math.max(ar, br)));
                requests.remove(j--);
            }
        }
    }

    // pull what we need from the records
    final HashMap<String, ServerStatus.ModuleSummary> entries = new HashMap<String, ServerStatus.ModuleSummary>();

    for (List<String[]> l : records.subMap(req.getMinimumLong(), req.getMaximumLong()).values()) {
        for (String[] r : l) {
            final String moduleName = r[0];
            final String roomName = r[1];
            final String playerName = r[2];

            final ServerStatus.ModuleSummary entry = entries.get(moduleName);
            if (entry == null) {
                entries.put(moduleName, createEntry(moduleName, roomName, playerName));
            } else {
                updateEntry(entry, roomName, playerName);
            }
        }
    }

    return sortEntriesByModuleName(entries);
}