Java Code Examples for org.jfree.chart.axis.SegmentedTimeline#getSegment()

The following examples show how to use org.jfree.chart.axis.SegmentedTimeline#getSegment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that a timeline's included and excluded segments are being
 * calculated correctly.
 *
 * @param timeline the timeline to verify
 * @param n the first segment number to start verifying
 */
public void verifyIncludedAndExcludedSegments(SegmentedTimeline timeline,
                                              long n) {
    // clear any exceptions in this timeline
    timeline.setExceptionSegments(new java.util.ArrayList());

    // test some included and excluded segments
    SegmentedTimeline.Segment segment = timeline.getSegment(n);
    for (int i = 0; i < 1000; i++) {
        int d = (i % timeline.getGroupSegmentCount());
        if (d < timeline.getSegmentsIncluded()) {
            // should be an included segment
            assertTrue(segment.inIncludeSegments());
            assertTrue(!segment.inExcludeSegments());
            assertTrue(!segment.inExceptionSegments());
        }
        else {
            // should be an excluded segment
            assertTrue(!segment.inIncludeSegments());
            assertTrue(segment.inExcludeSegments());
            assertTrue(!segment.inExceptionSegments());
        }
        segment.inc();
    }
}
 
Example 2
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that a timeline's included and excluded segments are being 
 * calculated correctly.
 * 
 * @param timeline the timeline to verify
 * @param n the first segment number to start verifying
 */
public void verifyIncludedAndExcludedSegments(SegmentedTimeline timeline, 
                                              long n) {
    // clear any exceptions in this timeline
    timeline.setExceptionSegments(new java.util.ArrayList());

    // test some included and excluded segments
    SegmentedTimeline.Segment segment = timeline.getSegment(n);
    for (int i = 0; i < 1000; i++) {
        int d = (i % timeline.getGroupSegmentCount());
        if (d < timeline.getSegmentsIncluded()) {
            // should be an included segment
            assertTrue(segment.inIncludeSegments());
            assertTrue(!segment.inExcludeSegments());
            assertTrue(!segment.inExceptionSegments());
        } 
        else {
            // should be an excluded segment
            assertTrue(!segment.inIncludeSegments());
            assertTrue(segment.inExcludeSegments());
            assertTrue(!segment.inExceptionSegments());
        }
        segment.inc();
    }
}
 
Example 3
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests methods related to adding exceptions.
 *
 * @param timeline the timeline to verify
 * @param exceptionString array of Strings that represent the exceptions
 * @param fmt Format object that can parse the exceptionString strings
 *
 * @throws ParseException if there is a parsing error.
 */
public void verifyExceptionSegments(SegmentedTimeline timeline,
                                    String[] exceptionString,
                                    Format fmt)
    throws ParseException {

    // fill in the exceptions
    long[] exception = verifyFillInExceptions(timeline, exceptionString,
            fmt);

    int m = exception.length;

    // verify list of exceptions
    assertEquals(exception.length, timeline.getExceptionSegments().size());
    SegmentedTimeline.Segment lastSegment = timeline.getSegment(
            exception[m - 1]);
    for (int i = 0; i < m; i++) {
        SegmentedTimeline.Segment segment = timeline.getSegment(
                exception[i]);
        assertTrue(segment.inExceptionSegments());
        // include current exception and last one
        assertEquals(m - i, timeline.getExceptionSegmentCount(
                segment.getSegmentStart(), lastSegment.getSegmentEnd()));
        // exclude current exception and last one
        assertEquals(Math.max(0, m - i - 2),
                timeline.getExceptionSegmentCount(exception[i] + 1,
                exception[m - 1] - 1));
    }

}
 
Example 4
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests translations between timelines.
 *
 * @param timeline the timeline to use for verifications.
 * @param startTest  ??.
 */
public void verifyTranslations(SegmentedTimeline timeline, long startTest) {
    for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
         testCycle += TEST_CYCLE_INC) {

        long millisecond = startTest + testCycle
                           * timeline.getSegmentSize();
        SegmentedTimeline.Segment segment = timeline.getSegment(
                millisecond);

        for (int i = 0; i < 1000; i++) {
            long translatedValue = timeline.toTimelineValue(
                    segment.getMillisecond());
            long newValue = timeline.toMillisecond(translatedValue);

            if (segment.inExcludeSegments()
                    || segment.inExceptionSegments()) {
                // the reverse transformed value will be in the start of the
                // next non-excluded and non-exception segment
                SegmentedTimeline.Segment tempSegment = segment.copy();
                tempSegment.moveIndexToStart();
                do {
                    tempSegment.inc();
                }
                while (!tempSegment.inIncludeSegments());
                assertEquals(tempSegment.getMillisecond(), newValue);
            }

            else {
                assertEquals(segment.getMillisecond(), newValue);
            }
            segment.inc();
        }
    }
}
 
Example 5
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds an array of exceptions relative to the base timeline.
 *
 * @param timeline The timeline where the exceptions will be stored
 * @param exceptionString The exceptions to load
 * @param fmt The date formatter to use to parse each exceptions[i] value
 * @throws ParseException If there is any exception parsing each
 *                        exceptions[i] value.
 */
private void fillInBaseTimelineExceptions(SegmentedTimeline timeline,
                                         String[] exceptionString,
                                         Format fmt) throws ParseException {
    SegmentedTimeline baseTimeline = timeline.getBaseTimeline();
    for (int i = 0; i < exceptionString.length; i++) {
        long e;
        if (fmt instanceof NumberFormat) {
            e = ((NumberFormat) fmt).parse(exceptionString[i]).longValue();
        }
        else {
            e = timeline.getTime(((SimpleDateFormat) fmt)
                    .parse(exceptionString[i]));
        }
        timeline.addBaseTimelineException(e);

        // verify all timeline segments included in the
        // baseTimeline.segment are now exceptions
        SegmentedTimeline.Segment segment1 = baseTimeline.getSegment(e);
        for (SegmentedTimeline.Segment segment2
                = timeline.getSegment(segment1.getSegmentStart());
             segment2.getSegmentStart() <= segment1.getSegmentEnd();
             segment2.inc()) {
            if (!segment2.inExcludeSegments()) {
                assertTrue(segment2.inExceptionSegments());
            }
        }

    }
}
 
Example 6
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds new exceptions to a timeline. The exceptions are the excluded
 * segments from its base timeline.
 *
 * @param timeline  the timeline.
 * @param from  the start.
 * @param to  the end.
 */
private void fillInBaseTimelineExclusionsAsExceptions(
        SegmentedTimeline timeline, long from, long to) {

    // add the base timeline exclusions as timeline's esceptions
    timeline.addBaseTimelineExclusions(from, to);

    // validate base timeline exclusions added as timeline's esceptions
    for (SegmentedTimeline.Segment segment1 = timeline.getBaseTimeline()
            .getSegment(from);
         segment1.getSegmentStart() <= to;
         segment1.inc()) {

        if (segment1.inExcludeSegments()) {

            // verify all timeline segments included in the
            // baseTimeline.segment are now exceptions
            for (SegmentedTimeline.Segment segment2 = timeline.getSegment(
                    segment1.getSegmentStart());
                segment2.getSegmentStart() <= segment1.getSegmentEnd();
                segment2.inc()) {
                if (!segment2.inExcludeSegments()) {
                    assertTrue(segment2.inExceptionSegments());
                }
            }
        }
    }
}
 
Example 7
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests methods related to adding exceptions.
 * 
 * @param timeline the timeline to verify
 * @param exceptionString array of Strings that represent the exceptions
 * @param fmt Format object that can parse the exceptionString strings
 * 
 * @throws ParseException if there is a parsing error.
 */
public void verifyExceptionSegments(SegmentedTimeline timeline,
                                    String[] exceptionString,
                                    Format fmt)
    throws ParseException {

    // fill in the exceptions
    long[] exception = verifyFillInExceptions(timeline, exceptionString, 
            fmt);

    int m = exception.length;

    // verify list of exceptions
    assertEquals(exception.length, timeline.getExceptionSegments().size());
    SegmentedTimeline.Segment lastSegment = timeline.getSegment(
            exception[m - 1]);
    for (int i = 0; i < m; i++) {
        SegmentedTimeline.Segment segment = timeline.getSegment(
                exception[i]);
        assertTrue(segment.inExceptionSegments());
        // include current exception and last one
        assertEquals(m - i, timeline.getExceptionSegmentCount(
                segment.getSegmentStart(), lastSegment.getSegmentEnd()));
        // exclude current exception and last one
        assertEquals(Math.max(0, m - i - 2), 
                timeline.getExceptionSegmentCount(exception[i] + 1, 
                exception[m - 1] - 1));
    }

}
 
Example 8
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests translations between timelines.
 * 
 * @param timeline the timeline to use for verifications.
 * @param startTest  ??.
 */
public void verifyTranslations(SegmentedTimeline timeline, long startTest) {
    for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
         testCycle += TEST_CYCLE_INC) {

        long millisecond = startTest + testCycle 
                           * timeline.getSegmentSize();
        SegmentedTimeline.Segment segment = timeline.getSegment(
                millisecond);
        
        for (int i = 0; i < 1000; i++) {
            long translatedValue = timeline.toTimelineValue(
                    segment.getMillisecond());
            long newValue = timeline.toMillisecond(translatedValue);

            if (segment.inExcludeSegments() 
                    || segment.inExceptionSegments()) {
                // the reverse transformed value will be in the start of the
                // next non-excluded and non-exception segment
                SegmentedTimeline.Segment tempSegment = segment.copy();
                tempSegment.moveIndexToStart();
                do {
                    tempSegment.inc();
                }
                while (!tempSegment.inIncludeSegments());
                assertEquals(tempSegment.getMillisecond(), newValue);
            }

            else {
                assertEquals(segment.getMillisecond(), newValue);
            }
            segment.inc();
        }
    }
}
 
Example 9
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds an array of exceptions relative to the base timeline.
 *
 * @param timeline The timeline where the exceptions will be stored
 * @param exceptionString The exceptions to load
 * @param fmt The date formatter to use to parse each exceptions[i] value
 * @throws ParseException If there is any exception parsing each 
 *                        exceptions[i] value.
 */
private void fillInBaseTimelineExceptions(SegmentedTimeline timeline,
                                         String[] exceptionString,
                                         Format fmt) throws ParseException {
    SegmentedTimeline baseTimeline = timeline.getBaseTimeline();
    for (int i = 0; i < exceptionString.length; i++) {
        long e;
        if (fmt instanceof NumberFormat) {
            e = ((NumberFormat) fmt).parse(exceptionString[i]).longValue();
        }
        else {
            e = timeline.getTime(((SimpleDateFormat) fmt)
                    .parse(exceptionString[i]));
        }
        timeline.addBaseTimelineException(e);

        // verify all timeline segments included in the 
        // baseTimeline.segment are now exceptions
        SegmentedTimeline.Segment segment1 = baseTimeline.getSegment(e);
        for (SegmentedTimeline.Segment segment2 
                = timeline.getSegment(segment1.getSegmentStart());
             segment2.getSegmentStart() <= segment1.getSegmentEnd();
             segment2.inc()) {
            if (!segment2.inExcludeSegments()) {
                assertTrue(segment2.inExceptionSegments());
            }
        }

    }
}
 
Example 10
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds new exceptions to a timeline. The exceptions are the excluded 
 * segments from its base timeline.
 *
 * @param timeline  the timeline.
 * @param from  the start.
 * @param to  the end.
 */
private void fillInBaseTimelineExclusionsAsExceptions(
        SegmentedTimeline timeline, long from, long to) {

    // add the base timeline exclusions as timeline's esceptions
    timeline.addBaseTimelineExclusions(from, to);

    // validate base timeline exclusions added as timeline's esceptions
    for (SegmentedTimeline.Segment segment1 = timeline.getBaseTimeline()
            .getSegment(from);
         segment1.getSegmentStart() <= to;
         segment1.inc()) {

        if (segment1.inExcludeSegments()) {

            // verify all timeline segments included in the 
            // baseTimeline.segment are now exceptions
            for (SegmentedTimeline.Segment segment2 = timeline.getSegment(
                    segment1.getSegmentStart()); 
                segment2.getSegmentStart() <= segment1.getSegmentEnd();
                segment2.inc()) {
                if (!segment2.inExcludeSegments()) {
                    assertTrue(segment2.inExceptionSegments());
                }
            }
        }
    }
}
 
Example 11
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests one segment of the Monday through Friday timeline. Internal indices
 * inside one segment as well as adjacent segments are verified.
 * @param timeline the timeline to use for verifications.
 */
public void verifyOneSegment(SegmentedTimeline timeline) {

    for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
         testCycle += TEST_CYCLE_INC) {

        // get two consecutive segments for various tests
        SegmentedTimeline.Segment segment1 = timeline.getSegment(
                this.monday.getTime().getTime() + testCycle);
        SegmentedTimeline.Segment segment2 = timeline.getSegment(
                segment1.getSegmentEnd() + 1);

        // verify segments are consecutive and correct
        assertEquals(segment1.getSegmentNumber() + 1,
                segment2.getSegmentNumber());
        assertEquals(segment1.getSegmentEnd() + 1,
                segment2.getSegmentStart());
        assertEquals(segment1.getSegmentStart()
                + timeline.getSegmentSize() - 1, segment1.getSegmentEnd());
        assertEquals(segment1.getSegmentStart() + timeline.getSegmentSize(),
                segment2.getSegmentStart());
        assertEquals(segment1.getSegmentEnd() + timeline.getSegmentSize(),
                segment2.getSegmentEnd());

        // verify various indices inside a segment are the same segment
        long delta;
        if (timeline.getSegmentSize() > 1000000) {
            delta = timeline.getSegmentSize() / 10000;
        }
        else if (timeline.getSegmentSize() > 100000) {
            delta = timeline.getSegmentSize() / 1000;
        }
        else if (timeline.getSegmentSize() > 10000) {
            delta = timeline.getSegmentSize() / 100;
        }
        else if (timeline.getSegmentSize() > 1000) {
            delta = timeline.getSegmentSize() / 10;
        }
        else if (timeline.getSegmentSize() > 100) {
            delta = timeline.getSegmentSize() / 5;
        }
        else {
            delta = 1;
        }

        long start = segment1.getSegmentStart() + delta;
        long end = segment1.getSegmentStart()
                   + timeline.getSegmentSize() - 1;
        SegmentedTimeline.Segment lastSeg = timeline.getSegment(
                segment1.getSegmentStart());
        SegmentedTimeline.Segment seg;
        for (long i = start; i < end; i += delta) {
            seg = timeline.getSegment(i);
            assertEquals(lastSeg.getSegmentNumber(),
                    seg.getSegmentNumber());
            assertEquals(lastSeg.getSegmentStart(), seg.getSegmentStart());
            assertEquals(lastSeg.getSegmentEnd(), seg.getSegmentEnd());
            assertTrue(lastSeg.getMillisecond() < seg.getMillisecond());
            lastSeg = seg;
        }

        // try next segment
        seg = timeline.getSegment(end + 1);
        assertEquals(segment2.getSegmentNumber(), seg.getSegmentNumber());
        assertEquals(segment2.getSegmentStart(), seg.getSegmentStart());
        assertEquals(segment2.getSegmentEnd(), seg.getSegmentEnd());
    }
}
 
Example 12
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests the inc methods.
 * @param timeline the timeline to use for verifications.
 */
public void verifyInc(SegmentedTimeline timeline) {
    for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
         testCycle += TEST_CYCLE_INC) {

        long m = timeline.getSegmentSize();
        SegmentedTimeline.Segment segment = timeline.getSegment(testCycle);
        SegmentedTimeline.Segment seg1 = segment.copy();
        for (int i = 0; i < 1000; i++) {

            // test inc() method
            SegmentedTimeline.Segment seg2 = seg1.copy();
            seg2.inc();

            if ((seg1.getSegmentEnd() + 1) != seg2.getSegmentStart()) {
                // logically consecutive segments non-physically consecutive
                // (with non-contained time in between)
                assertTrue(!timeline.containsDomainRange(
                        seg1.getSegmentEnd() + 1,
                        seg2.getSegmentStart() - 1));
                assertEquals(0, (seg2.getSegmentStart()
                        - seg1.getSegmentStart()) % m);
                assertEquals(0, (seg2.getSegmentEnd()
                        - seg1.getSegmentEnd()) % m);
                assertEquals(0, (seg2.getMillisecond()
                        - seg1.getMillisecond()) % m);
            }
            else {
                // physically consecutive
                assertEquals(seg1.getSegmentStart() + m,
                        seg2.getSegmentStart());
                assertEquals(seg1.getSegmentEnd() + m,
                        seg2.getSegmentEnd());
                assertEquals(seg1.getMillisecond() + m,
                        seg2.getMillisecond());
            }

            // test inc(n) method
            SegmentedTimeline.Segment seg3 = seg1.copy();
            SegmentedTimeline.Segment seg4 = seg1.copy();

            for (int j = 0; j < i; j++) {
                seg3.inc();
            }
            seg4.inc(i);

            assertEquals(seg3.getSegmentStart(), seg4.getSegmentStart());
            assertEquals(seg3.getSegmentEnd(), seg4.getSegmentEnd());
            assertEquals(seg3.getMillisecond(), seg4.getMillisecond());

            // go to another segment to continue test
            seg1.inc();
        }
    }
}
 
Example 13
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests one segment of the Monday through Friday timeline. Internal indices
 * inside one segment as well as adjacent segments are verified.
 * @param timeline the timeline to use for verifications.
 */
public void verifyOneSegment(SegmentedTimeline timeline) {
    
    for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
         testCycle += TEST_CYCLE_INC) {

        // get two consecutive segments for various tests
        SegmentedTimeline.Segment segment1 = timeline.getSegment(
                this.monday.getTime().getTime() + testCycle);
        SegmentedTimeline.Segment segment2 = timeline.getSegment(
                segment1.getSegmentEnd() + 1);

        // verify segments are consecutive and correct
        assertEquals(segment1.getSegmentNumber() + 1, 
                segment2.getSegmentNumber());
        assertEquals(segment1.getSegmentEnd() + 1, 
                segment2.getSegmentStart());
        assertEquals(segment1.getSegmentStart() 
                + timeline.getSegmentSize() - 1, segment1.getSegmentEnd());
        assertEquals(segment1.getSegmentStart() + timeline.getSegmentSize(),
                segment2.getSegmentStart());
        assertEquals(segment1.getSegmentEnd() + timeline.getSegmentSize(),
                segment2.getSegmentEnd());

        // verify various indices inside a segment are the same segment
        long delta;
        if (timeline.getSegmentSize() > 1000000) {
            delta = timeline.getSegmentSize() / 10000;
        } 
        else if (timeline.getSegmentSize() > 100000) {
            delta = timeline.getSegmentSize() / 1000;
        } 
        else if (timeline.getSegmentSize() > 10000) {
            delta = timeline.getSegmentSize() / 100;
        }
        else if (timeline.getSegmentSize() > 1000) {
            delta = timeline.getSegmentSize() / 10;
        }
        else if (timeline.getSegmentSize() > 100) {
            delta = timeline.getSegmentSize() / 5;
        }
        else {
            delta = 1;
        }

        long start = segment1.getSegmentStart() + delta;
        long end = segment1.getSegmentStart() 
                   + timeline.getSegmentSize() - 1;
        SegmentedTimeline.Segment lastSeg = timeline.getSegment(
                segment1.getSegmentStart());
        SegmentedTimeline.Segment seg;
        for (long i = start; i < end; i += delta) {
            seg = timeline.getSegment(i);
            assertEquals(lastSeg.getSegmentNumber(), 
                    seg.getSegmentNumber());
            assertEquals(lastSeg.getSegmentStart(), seg.getSegmentStart());
            assertEquals(lastSeg.getSegmentEnd(), seg.getSegmentEnd());
            assertTrue(lastSeg.getMillisecond() < seg.getMillisecond());
            lastSeg = seg;
        }

        // try next segment
        seg = timeline.getSegment(end + 1);
        assertEquals(segment2.getSegmentNumber(), seg.getSegmentNumber());
        assertEquals(segment2.getSegmentStart(), seg.getSegmentStart());
        assertEquals(segment2.getSegmentEnd(), seg.getSegmentEnd());
    }
}
 
Example 14
Source File: SegmentedTimelineTests.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests the inc methods.
 * @param timeline the timeline to use for verifications.
 */
public void verifyInc(SegmentedTimeline timeline) {
    for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
         testCycle += TEST_CYCLE_INC) {

        long m = timeline.getSegmentSize();
        SegmentedTimeline.Segment segment = timeline.getSegment(testCycle);
        SegmentedTimeline.Segment seg1 = segment.copy();
        for (int i = 0; i < 1000; i++) {

            // test inc() method
            SegmentedTimeline.Segment seg2 = seg1.copy();
            seg2.inc();

            if ((seg1.getSegmentEnd() + 1) != seg2.getSegmentStart()) {
                // logically consecutive segments non-physically consecutive
                // (with non-contained time in between)
                assertTrue(!timeline.containsDomainRange(
                        seg1.getSegmentEnd() + 1, 
                        seg2.getSegmentStart() - 1));
                assertEquals(0, (seg2.getSegmentStart() 
                        - seg1.getSegmentStart()) % m);
                assertEquals(0, (seg2.getSegmentEnd() 
                        - seg1.getSegmentEnd()) % m);
                assertEquals(0, (seg2.getMillisecond() 
                        - seg1.getMillisecond()) % m);
            } 
            else {
                // physically consecutive
                assertEquals(seg1.getSegmentStart() + m, 
                        seg2.getSegmentStart());
                assertEquals(seg1.getSegmentEnd() + m, 
                        seg2.getSegmentEnd());
                assertEquals(seg1.getMillisecond() + m, 
                        seg2.getMillisecond());
            }

            // test inc(n) method
            SegmentedTimeline.Segment seg3 = seg1.copy();
            SegmentedTimeline.Segment seg4 = seg1.copy();

            for (int j = 0; j < i; j++) {
                seg3.inc();
            }
            seg4.inc(i);

            assertEquals(seg3.getSegmentStart(), seg4.getSegmentStart());
            assertEquals(seg3.getSegmentEnd(), seg4.getSegmentEnd());
            assertEquals(seg3.getMillisecond(), seg4.getMillisecond());

            // go to another segment to continue test
            seg1.inc();
        }
    }
}