Sample code of segmentedtimeline
Sample code of segmentedtimeline
can anybody give a sample code or rules for 'SegmentedTimeline'.
I have tried to use but i am unable to set the include and exclude
parameters. How should I have to pass them?
becoz sometimes graph is not shown.
Thanx,
Have a good day!
I have tried to use but i am unable to set the include and exclude
parameters. How should I have to pass them?
becoz sometimes graph is not shown.
Thanx,
Have a good day!
SegmentedTimeline'
hai sarika
This is the sample code for 'SegmentedTimeline'
if u have any quires let me know...
================================================
public class SegmentedTimelineTests extends TestCase {
/** These constants control test cycles in the validateXXXX methods. */
private static final int TEST_CYCLE_START = 0;
/** These constants control test cycles in the validateXXXX methods. */
private static final int TEST_CYCLE_END = 1000;
/** These constants control test cycles in the validateXXXX methods. */
private static final int TEST_CYCLE_INC = 55;
/** Number of ms in five years */
private static final long FIVE_YEARS = 5 * 365 * SegmentedTimeline.DAY_SEGMENT_SIZE;
/** Number format object for ms tests. */
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance();
/** Date format object for Monday through Friday tests. */
private static final SimpleDateFormat DATE_FORMAT;
/** Date format object 9:00 AM to 4:00 PM tests. */
private static final SimpleDateFormat DATE_TIME_FORMAT;
/** Some ms exceptions for ms testing. */
private static final String[] MS_EXCEPTIONS =
{"0", "2", "4", "10", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"47", "58", "100", "101"};
/** Some ms4 exceptions for ms testing. */
private static final String[] MS2_BASE_TIMELINE_EXCEPTIONS =
{"0", "8", "16", "24", "32", "40", "48", "56", "64", "72", "80", "88", "96", "104",
"112", "120", "128", "136"};
/** US non-trading dates in 2000 through 2002 to test exceptions. */
private static final String[] US_HOLIDAYS =
{"2000-01-17", "2000-02-21", "2000-04-21", "2000-05-29", "2000-07-04",
"2000-09-04", "2000-11-23", "2000-12-25", "2001-01-01", "2001-01-15",
"2001-02-19", "2001-04-13", "2001-05-28", "2001-07-04", "2001-09-03",
"2001-09-11", "2001-09-12", "2001-09-13", "2001-09-14", "2001-11-22",
"2001-12-25", "2002-01-01", "2002-01-21", "2002-02-18", "2002-03-29",
"2002-05-27", "2002-07-04", "2002-09-02", "2002-11-28", "2002-12-25"};
/** Some test exceptions for the fifteen min timeline. */
private static final String[] FIFTEEN_MIN_EXCEPTIONS =
{"2000-01-10 09:00:00", "2000-01-10 09:15:00", "2000-01-10 09:30:00",
"2000-01-10 09:45:00", "2000-01-10 10:00:00", "2000-01-10 10:15:00",
"2000-02-15 09:00:00", "2000-02-15 09:15:00", "2000-02-15 09:30:00",
"2000-02-15 09:45:00", "2000-02-15 10:00:00", "2000-02-15 10:15:00",
"2000-02-16 11:00:00", "2000-02-16 11:15:00", "2000-02-16 11:30:00",
"2000-02-16 11:45:00", "2000-02-16 12:00:00", "2000-02-16 12:15:00",
"2000-02-16 12:30:00", "2000-02-16 12:45:00", "2000-02-16 01:00:00",
"2000-02-16 01:15:00", "2000-02-16 01:30:00", "2000-02-16 01:45:00",
"2000-05-17 11:45:00", "2000-05-17 12:00:00", "2000-05-17 12:15:00",
"2000-05-17 12:30:00", "2000-05-17 12:45:00", "2000-05-17 01:00:00",
"2000-05-17 01:15:00", "2000-05-17 01:30:00", "2000-05-17 01:45:00",
"2000-05-17 02:00:00", "2000-05-17 02:15:00", "2000-05-17 02:30:00",
"2000-05-17 02:45:00", "2000-05-17 03:00:00", "2000-05-17 03:15:00",
"2000-05-17 03:30:00", "2000-05-17 03:45:00", "2000-05-17 04:00:00"};
/** Our 1-ms test timeline using 5 included and 2 excluded segments. */
private SegmentedTimeline msTimeline;
/** Our 1-ms test timeline (with baseTimeline) using 2 included and 2 excluded segments. */
private SegmentedTimeline ms2Timeline;
/** Our 4-ms test base timeline for ms2Timeline using 1 included and 1 excluded segments */
private SegmentedTimeline ms2BaseTimeline;
/** Our test Monday through Friday test timeline. */
private SegmentedTimeline mondayFridayTimeline;
/** Our 9:00 AM to 4:00 PM fifteen minute timeline. */
private SegmentedTimeline fifteenMinTimeline;
/** ms from 1970-01-01 to first monday after 2001-01-01. */
private Calendar monday;
/** ms from 1970-01-01 to 9 am first monday after 2001-01-01. */
private Calendar monday9am;
/** Static initialization block. */
static {
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
DATE_FORMAT.setTimeZone(SegmentedTimeline.NO_DST_TIME_ZONE);
DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DATE_TIME_FORMAT.setTimeZone(SegmentedTimeline.NO_DST_TIME_ZONE);
}
/**
* Returns the tests as a test suite.
*
* @return the test suite.
*/
public static Test suite() {
return new TestSuite(SegmentedTimelineTests.class);
}
/**
* Constructs a new set of tests.
*
* @param name the name of the tests.
*/
public SegmentedTimelineTests(String name) {
super(name);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @throws Exception if there is a problem.
*/
protected void setUp() throws Exception {
// setup our test timelines
//
// Legend for comments below:
// <spaces> = Segments included in the final timeline
// EE = Excluded segments via timeline rules
// xx = Exception segments inherited from base timeline exclusions
// 1-ms test timeline using 5 included and 2 excluded segments.
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | | |EE|EE| | | | | |EE|EE| | | | | | |EE|EE| <-- msTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \_________ ________/ \_/
// \/ |
// segment group segment size = 1 ms
//
msTimeline = new SegmentedTimeline(1, 5, 2);
msTimeline.setStartTime(0);
// 4-ms test base timeline for ms2Timeline using 1 included and 1 excluded segments
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \__________ _________/ \____ _____/
// \/ \/
// segment group segment size = 4 ms
//
ms2BaseTimeline = new SegmentedTimeline(4, 1, 1);
ms2BaseTimeline.setStartTime(0);
// 1-ms test timeline (with a baseTimeline) using 2 included and 2 excluded segments
// centered inside each base segment
//
// The ms2Timeline without a base would look like this:
//
// timeline start time = 1
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \____ _____/ \_/
// \/ |
// segment group segment size = 1 ms
//
// With the base timeline some originally included segments are now removed (see "xx"
// below):
//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE|xx|xx|EE|EE| | |EE|EE|xx|xx|EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
//
ms2Timeline = new SegmentedTimeline(1, 2, 2);
ms2Timeline.setStartTime(1);
ms2Timeline.setBaseTimeline(ms2BaseTimeline);
// test monday though friday timeline
mondayFridayTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
// test 9am-4pm Monday through Friday timeline
fifteenMinTimeline = SegmentedTimeline.newFifteenMinuteTimeline();
// find first Monday after 2001-01-01
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.set(2001, 0, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, 1);
}
monday = (Calendar) cal.clone();
// calculate 9am on the first Monday after 2001-01-01
cal.add(Calendar.HOUR, 9);
monday9am = (Calendar) cal.clone();
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @throws Exception if there is a problem.
*/
protected void tearDown() throws Exception {
}
//////////////////////////////////////////////////////////////////////////
// test construction process
//////////////////////////////////////////////////////////////////////////
/**
* Tests that the new method that created the msTimeline segmented
* timeline did so correctly.
*/
public void testMsSegmentedTimeline() {
// verify attributes set during object construction
assertEquals(1, msTimeline.getSegmentSize());
assertEquals(0, msTimeline.getStartTime());
assertEquals(5, msTimeline.getSegmentsIncluded());
assertEquals(2, msTimeline.getSegmentsExcluded());
}
/**
* Tests that the new method that created the ms2Timeline segmented
* timeline did so correctly.
*/
public void testMs2SegmentedTimeline() {
// verify attributes set during object construction
assertEquals(1, ms2Timeline.getSegmentSize());
assertEquals(1, ms2Timeline.getStartTime());
assertEquals(2, ms2Timeline.getSegmentsIncluded());
assertEquals(2, ms2Timeline.getSegmentsExcluded());
assertEquals(ms2BaseTimeline, ms2Timeline.getBaseTimeline());
}
/**
* Tests that the factory method that creates Monday through Friday segmented
* timeline does so correctly.
*/
public void testMondayThroughFridaySegmentedTimeline() {
// verify attributes set during object construction
assertEquals(SegmentedTimeline.DAY_SEGMENT_SIZE, mondayFridayTimeline.getSegmentSize());
assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900,
mondayFridayTimeline.getStartTime());
assertEquals(5, mondayFridayTimeline.getSegmentsIncluded());
assertEquals(2, mondayFridayTimeline.getSegmentsExcluded());
}
/**
* Tests that the factory method that creates a 15-min 9:00 AM 4:00 PM
* segmented axis does so correctly.
*/
public void testFifteenMinSegmentedTimeline() {
assertEquals(SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE,
fifteenMinTimeline.getSegmentSize());
assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900 + 36
* fifteenMinTimeline.getSegmentSize(),
fifteenMinTimeline.getStartTime());
assertEquals(28, fifteenMinTimeline.getSegmentsIncluded());
assertEquals(68, fifteenMinTimeline.getSegmentsExcluded());
}
//////////////////////////////////////////////////////////////////////////
// test one-segment and adjacent segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests one segment of the ms timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMsSegment() {
verifyOneSegment(msTimeline);
}
/**
* Tests one segment of the ms timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMs2Segment() {
verifyOneSegment(ms2Timeline);
}
/**
* Tests one segment of the Monday through Friday timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMondayThroughFridaySegment() {
verifyOneSegment(mondayFridayTimeline);
}
/**
* Tests one segment of the Fifteen timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testFifteenMinSegment() {
verifyOneSegment(fifteenMinTimeline);
}
/**
* 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(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());
}
}
//////////////////////////////////////////////////////////////////////////
// test inc methods
//////////////////////////////////////////////////////////////////////////
/**
* Tests the inc methods on the msTimeline.
*/
public void testMsInc() {
verifyInc(msTimeline);
}
/**
* Tests the inc methods on the msTimeline.
*/
public void testMs2Inc() {
verifyInc(ms2Timeline);
}
/**
* Tests the inc methods on the Monday through Friday timeline.
*/
public void testMondayThroughFridayInc() {
verifyInc(mondayFridayTimeline);
}
/**
* Tests the inc methods on the Fifteen minute timeline.
*/
public void testFifteenMinInc() {
verifyInc(fifteenMinTimeline);
}
/**
* 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();
}
}
}
//////////////////////////////////////////////////////////////////////////
// main include and excluded segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests that the msTimeline's included and excluded
* segments are being calculated correctly.
*/
public void testMsIncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(msTimeline, 0);
}
/**
* Tests that the ms2Timeline's included and excluded
* segments are being calculated correctly.
*/
public void testMs2IncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(ms2Timeline, 1);
}
/**
* Tests that the Monday through Friday timeline's included and excluded
* segments are being calculated correctly. The test is performed starting
* on the first monday after 1/1/2000 and for five years.
*/
public void testMondayThroughFridayIncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(mondayFridayTimeline, monday.getTime().getTime());
}
/**
* Tests that the Fifteen-Min timeline's included and excluded
* segments are being calculated correctly. The test is performed starting
* on the first monday after 1/1/2000 and for five years.
*/
public void testFifteenMinIncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(fifteenMinTimeline, monday9am.getTime().getTime());
}
/**
* 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();
}
}
//////////////////////////////////////////////////////////////////////////
// test exception segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests methods related to exceptions methods in the msTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMsExceptionSegments() throws ParseException {
verifyExceptionSegments(msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
}
/**
* Tests methods related to exceptions methods in the ms2BaseTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2BaseTimelineExceptionSegments() throws ParseException {
verifyExceptionSegments(ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
}
/**
* Tests methods related to exceptions methods in the mondayFridayTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMondayThoughFridayExceptionSegments() throws ParseException {
verifyExceptionSegments(mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT);
}
/**
* Tests methods related to exceptions methods in the fifteenMinTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testFifteenMinExceptionSegments() throws ParseException {
verifyExceptionSegments(fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
}
/**
* 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);
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 + 1, exception[m - 1] - 1));
}
}
//////////////////////////////////////////////////////////////////////////
// test timeline translations
//////////////////////////////////////////////////////////////////////////
/**
* Tests translations for 1-ms timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMsTranslations() throws ParseException {
verifyFillInExceptions(msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
verifyTranslations(msTimeline, 0);
}
/**
* Tests translations for the base timeline used for the ms2Timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2BaseTimelineTranslations() throws ParseException {
verifyFillInExceptions(ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
verifyTranslations(ms2BaseTimeline, 0);
}
/**
* Tests translations for the Monday through Friday timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2Translations() throws ParseException {
fillInBaseTimelineExceptions(ms2Timeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
fillInBaseTimelineExclusionsAsExceptions(ms2Timeline, 0, 5000);
verifyTranslations(ms2Timeline, 1);
}
/**
* Tests translations for the Monday through Friday timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMondayThroughFridayTranslations() throws ParseException {
verifyFillInExceptions(mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT);
verifyTranslations(mondayFridayTimeline, monday.getTime().getTime());
}
/**
* Tests translations for the Fifteen Min timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testFifteenMinTranslations() throws ParseException {
verifyFillInExceptions(fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
fillInBaseTimelineExceptions(fifteenMinTimeline, US_HOLIDAYS, DATE_FORMAT);
fillInBaseTimelineExclusionsAsExceptions(fifteenMinTimeline,
monday9am.getTime().getTime(),
monday9am.getTime().getTime() + FIVE_YEARS);
verifyTranslations(fifteenMinTimeline, monday9am.getTime().getTime());
}
/**
* 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();
}
}
}
//////////////////////////////////////////////////////////////////////////
// test serialization
//////////////////////////////////////////////////////////////////////////
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
verifySerialization(msTimeline);
verifySerialization(ms2Timeline);
verifySerialization(ms2BaseTimeline);
verifySerialization(SegmentedTimeline.newMondayThroughFridayTimeline());
verifySerialization(SegmentedTimeline.newFifteenMinuteTimeline());
}
/**
* Tests serialization of an instance.
* @param a1 The timeline to verify the serialization
*/
private void verifySerialization(SegmentedTimeline a1) {
SegmentedTimeline a2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(a1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
a2 = (SegmentedTimeline) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertEquals(a1, a2);
}
/**
* Adds an array of exceptions to the timeline. The timeline exception list
* is first cleared.
* @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 value
* @throws ParseException If there is any exception parsing each exceptions
* value.
* @return An array of Dates[] containing each exception date.
*/
private long[] verifyFillInExceptions(SegmentedTimeline timeline,
String[] exceptionString,
Format fmt) throws ParseException {
// make sure there are no exceptions
timeline.setExceptionSegments(new java.util.ArrayList());
assertEquals(0, timeline.getExceptionSegments().size());
// add our exceptions and store locally in ArrayList of Longs
ArrayList exceptionList = new ArrayList();
for (int i = 0; i < exceptionString.length; i++) {
long e;
if (fmt instanceof NumberFormat) {
e = ((NumberFormat) fmt).parse(exceptionString).longValue();
}
else {
e = timeline.getTime(((SimpleDateFormat) fmt).parse(exceptionString));
}
// only add an exception if it is currently an included segment
SegmentedTimeline.Segment segment = timeline.getSegment(e);
if (segment.inIncludeSegments()) {
timeline.addException(e);
exceptionList.add(new Long(e));
assertEquals(exceptionList.size(), timeline.getExceptionSegments().size());
assertTrue(segment.inExceptionSegments());
}
}
// make array of exceptions
long[] exception = new long[exceptionList.size()];
int i = 0;
for (Iterator iter = exceptionList.iterator(); iter.hasNext();) {
Long l = (Long) iter.next();
exception[i++] = l.longValue();
}
return (exception);
}
/**
* 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 value
* @throws ParseException If there is any exception parsing each exceptions
* 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).longValue();
}
else {
e = timeline.getTime(((SimpleDateFormat) fmt).parse(exceptionString));
}
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());
}
}
}
}
/**
* 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());
}
}
}
}
}
/**
* Confirm that cloning works.
*/
public void testCloning() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = null;
try {
l2 = (SegmentedTimeline) l1.clone();
}
catch (CloneNotSupportedException e) {
System.err.println("SegmentedTimelineTests2.testCloning: failed to clone.");
}
assertTrue(l1 != l2);
assertTrue(l1.getClass() == l2.getClass());
assertTrue(l1.equals(l2));
}
/**
* Confirm that the equals method can distinguish all the required fields.
*/
public void testEquals() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = new SegmentedTimeline(1000, 5, 2);
assertTrue(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1001, 5, 2);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 4, 2);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 5, 1);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 5, 2);
// start time...
l1.setStartTime(1234L);
assertFalse(l1.equals(l2));
l2.setStartTime(1234L);
assertTrue(l1.equals(l2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization2() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(l1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
l2 = (SegmentedTimeline) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
boolean b = l1.equals(l2);
assertTrue(b);
}
//////////////////////////////////////////////////////////////////////////
// utility methods
//////////////////////////////////////////////////////////////////////////
/**
* Formats a Calendar object avoiding converting into a Date so the local
* TimeZone is not used to convert the output. The Calendar object used as
* parameter is assumed to use the SegmentedTimeline.TIME_ZONE time zone.
*
* @param cal Calendar object to format
* @return
*/
/*
private String calToString(Calendar cal) {
DecimalFormat fmt2 = new DecimalFormat("00");
DecimalFormat fmt4 = new DecimalFormat("0000");
return (getDayOfWeekString(cal)+" "+
fmt2.format(cal.get(Calendar.MONTH)+1)+"/"+
fmt2.format(cal.get(Calendar.DATE))+"/"+
fmt4.format(cal.get(Calendar.YEAR))+" "+
fmt2.format(cal.get(Calendar.HOUR_OF_DAY))+":"+
fmt2.format(cal.get(Calendar.MINUTE))+":"+
fmt2.format(cal.get(Calendar.SECOND))+":"+
fmt4.format(cal.get(Calendar.MILLISECOND)));
}
*/
/**
* Formats time avoiding converting into a Date so the local
* TimeZone is not used to convert the output. The time used as
* parameter is assumed to be relative to SegmentedTimeline.TIME_ZONE time zone.
*
* @param time time to format
* @return
*/
/*
private String calToString(long time) {
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.setTimeInMillis(time);
return (calToString(cal));
}
*/
/**
* Returns a three letter day of the week string for Calendar formatting.
* @param cal Calendar to check
* @return
*/
/*
private String getDayOfWeekString(Calendar cal) {
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY: return ("Sun");
case Calendar.MONDAY: return ("Mon");
case Calendar.TUESDAY: return ("Tue");
case Calendar.WEDNESDAY: return ("Wed");
case Calendar.THURSDAY: return ("Thu");
case Calendar.FRIDAY: return ("Fri");
case Calendar.SATURDAY: return ("Sat");
default: return ("???");
}
}
*/
/**
* Tests a basic segmented timeline.
*/
public void testBasicSegmentedTimeline() {
SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3);
stl.setStartTime(946684800000L); // 1-Jan-2000
assertFalse(stl.containsDomainValue(946684799999L));
assertTrue(stl.containsDomainValue(946684800000L));
assertTrue(stl.containsDomainValue(946684800019L));
assertFalse(stl.containsDomainValue(946684800020L));
assertFalse(stl.containsDomainValue(946684800049L));
assertTrue(stl.containsDomainValue(946684800050L));
assertTrue(stl.containsDomainValue(946684800069L));
assertFalse(stl.containsDomainValue(946684800070L));
assertFalse(stl.containsDomainValue(946684800099L));
assertTrue(stl.containsDomainValue(946684800100L));
assertEquals(0, stl.toTimelineValue(946684800000L));
assertEquals(19, stl.toTimelineValue(946684800019L));
assertEquals(20, stl.toTimelineValue(946684800020L));
assertEquals(20, stl.toTimelineValue(946684800049L));
assertEquals(20, stl.toTimelineValue(946684800050L));
assertEquals(39, stl.toTimelineValue(946684800069L));
assertEquals(40, stl.toTimelineValue(946684800070L));
assertEquals(40, stl.toTimelineValue(946684800099L));
assertEquals(40, stl.toTimelineValue(946684800100L));
assertEquals(946684800000L, stl.toMillisecond(0));
assertEquals(946684800019L, stl.toMillisecond(19));
assertEquals(946684800050L, stl.toMillisecond(20));
assertEquals(946684800069L, stl.toMillisecond(39));
assertEquals(946684800100L, stl.toMillisecond(40));
}
/**
* Tests a basic time line with one exception.
*/
public void testSegmentedTimelineWithException1() {
SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3);
stl.setStartTime(946684800000L); // 1-Jan-2000
stl.addException(946684800050L);
assertFalse(stl.containsDomainValue(946684799999L));
assertTrue(stl.containsDomainValue(946684800000L));
assertTrue(stl.containsDomainValue(946684800019L));
assertFalse(stl.containsDomainValue(946684800020L));
assertFalse(stl.containsDomainValue(946684800049L));
assertFalse(stl.containsDomainValue(946684800050L));
assertFalse(stl.containsDomainValue(946684800059L));
assertTrue(stl.containsDomainValue(946684800060L));
assertTrue(stl.containsDomainValue(946684800069L));
assertFalse(stl.containsDomainValue(946684800070L));
assertFalse(stl.containsDomainValue(946684800099L));
assertTrue(stl.containsDomainValue(946684800100L));
//long v = stl.toTimelineValue(946684800020L);
assertEquals(0, stl.toTimelineValue(946684800000L));
assertEquals(19, stl.toTimelineValue(946684800019L));
assertEquals(20, stl.toTimelineValue(946684800020L));
assertEquals(20, stl.toTimelineValue(946684800049L));
assertEquals(20, stl.toTimelineValue(946684800050L));
assertEquals(29, stl.toTimelineValue(946684800069L));
assertEquals(30, stl.toTimelineValue(946684800070L));
assertEquals(30, stl.toTimelineValue(946684800099L));
assertEquals(30, stl.toTimelineValue(946684800100L));
assertEquals(946684800000L, stl.toMillisecond(0));
assertEquals(946684800019L, stl.toMillisecond(19));
assertEquals(946684800060L, stl.toMillisecond(20));
assertEquals(946684800069L, stl.toMillisecond(29));
assertEquals(946684800100L, stl.toMillisecond(30));
}
//////////////////////////////////////////////////////////////////////////
// main method only for debug
//////////////////////////////////////////////////////////////////////////
/**
* Only use to debug JUnit suite.
*
* @param args ignored.
*
* @throws Exception if there is some problem.
*/
public static void main(String[] args) throws Exception {
SegmentedTimelineTests test = new SegmentedTimelineTests("Test");
test.setUp();
test.testMondayThoughFridayExceptionSegments();
test.tearDown();
}
}
============================================
Thanks
venuGopal
This is the sample code for 'SegmentedTimeline'
if u have any quires let me know...
================================================
public class SegmentedTimelineTests extends TestCase {
/** These constants control test cycles in the validateXXXX methods. */
private static final int TEST_CYCLE_START = 0;
/** These constants control test cycles in the validateXXXX methods. */
private static final int TEST_CYCLE_END = 1000;
/** These constants control test cycles in the validateXXXX methods. */
private static final int TEST_CYCLE_INC = 55;
/** Number of ms in five years */
private static final long FIVE_YEARS = 5 * 365 * SegmentedTimeline.DAY_SEGMENT_SIZE;
/** Number format object for ms tests. */
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance();
/** Date format object for Monday through Friday tests. */
private static final SimpleDateFormat DATE_FORMAT;
/** Date format object 9:00 AM to 4:00 PM tests. */
private static final SimpleDateFormat DATE_TIME_FORMAT;
/** Some ms exceptions for ms testing. */
private static final String[] MS_EXCEPTIONS =
{"0", "2", "4", "10", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"47", "58", "100", "101"};
/** Some ms4 exceptions for ms testing. */
private static final String[] MS2_BASE_TIMELINE_EXCEPTIONS =
{"0", "8", "16", "24", "32", "40", "48", "56", "64", "72", "80", "88", "96", "104",
"112", "120", "128", "136"};
/** US non-trading dates in 2000 through 2002 to test exceptions. */
private static final String[] US_HOLIDAYS =
{"2000-01-17", "2000-02-21", "2000-04-21", "2000-05-29", "2000-07-04",
"2000-09-04", "2000-11-23", "2000-12-25", "2001-01-01", "2001-01-15",
"2001-02-19", "2001-04-13", "2001-05-28", "2001-07-04", "2001-09-03",
"2001-09-11", "2001-09-12", "2001-09-13", "2001-09-14", "2001-11-22",
"2001-12-25", "2002-01-01", "2002-01-21", "2002-02-18", "2002-03-29",
"2002-05-27", "2002-07-04", "2002-09-02", "2002-11-28", "2002-12-25"};
/** Some test exceptions for the fifteen min timeline. */
private static final String[] FIFTEEN_MIN_EXCEPTIONS =
{"2000-01-10 09:00:00", "2000-01-10 09:15:00", "2000-01-10 09:30:00",
"2000-01-10 09:45:00", "2000-01-10 10:00:00", "2000-01-10 10:15:00",
"2000-02-15 09:00:00", "2000-02-15 09:15:00", "2000-02-15 09:30:00",
"2000-02-15 09:45:00", "2000-02-15 10:00:00", "2000-02-15 10:15:00",
"2000-02-16 11:00:00", "2000-02-16 11:15:00", "2000-02-16 11:30:00",
"2000-02-16 11:45:00", "2000-02-16 12:00:00", "2000-02-16 12:15:00",
"2000-02-16 12:30:00", "2000-02-16 12:45:00", "2000-02-16 01:00:00",
"2000-02-16 01:15:00", "2000-02-16 01:30:00", "2000-02-16 01:45:00",
"2000-05-17 11:45:00", "2000-05-17 12:00:00", "2000-05-17 12:15:00",
"2000-05-17 12:30:00", "2000-05-17 12:45:00", "2000-05-17 01:00:00",
"2000-05-17 01:15:00", "2000-05-17 01:30:00", "2000-05-17 01:45:00",
"2000-05-17 02:00:00", "2000-05-17 02:15:00", "2000-05-17 02:30:00",
"2000-05-17 02:45:00", "2000-05-17 03:00:00", "2000-05-17 03:15:00",
"2000-05-17 03:30:00", "2000-05-17 03:45:00", "2000-05-17 04:00:00"};
/** Our 1-ms test timeline using 5 included and 2 excluded segments. */
private SegmentedTimeline msTimeline;
/** Our 1-ms test timeline (with baseTimeline) using 2 included and 2 excluded segments. */
private SegmentedTimeline ms2Timeline;
/** Our 4-ms test base timeline for ms2Timeline using 1 included and 1 excluded segments */
private SegmentedTimeline ms2BaseTimeline;
/** Our test Monday through Friday test timeline. */
private SegmentedTimeline mondayFridayTimeline;
/** Our 9:00 AM to 4:00 PM fifteen minute timeline. */
private SegmentedTimeline fifteenMinTimeline;
/** ms from 1970-01-01 to first monday after 2001-01-01. */
private Calendar monday;
/** ms from 1970-01-01 to 9 am first monday after 2001-01-01. */
private Calendar monday9am;
/** Static initialization block. */
static {
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
DATE_FORMAT.setTimeZone(SegmentedTimeline.NO_DST_TIME_ZONE);
DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DATE_TIME_FORMAT.setTimeZone(SegmentedTimeline.NO_DST_TIME_ZONE);
}
/**
* Returns the tests as a test suite.
*
* @return the test suite.
*/
public static Test suite() {
return new TestSuite(SegmentedTimelineTests.class);
}
/**
* Constructs a new set of tests.
*
* @param name the name of the tests.
*/
public SegmentedTimelineTests(String name) {
super(name);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @throws Exception if there is a problem.
*/
protected void setUp() throws Exception {
// setup our test timelines
//
// Legend for comments below:
// <spaces> = Segments included in the final timeline
// EE = Excluded segments via timeline rules
// xx = Exception segments inherited from base timeline exclusions
// 1-ms test timeline using 5 included and 2 excluded segments.
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | | |EE|EE| | | | | |EE|EE| | | | | | |EE|EE| <-- msTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \_________ ________/ \_/
// \/ |
// segment group segment size = 1 ms
//
msTimeline = new SegmentedTimeline(1, 5, 2);
msTimeline.setStartTime(0);
// 4-ms test base timeline for ms2Timeline using 1 included and 1 excluded segments
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \__________ _________/ \____ _____/
// \/ \/
// segment group segment size = 4 ms
//
ms2BaseTimeline = new SegmentedTimeline(4, 1, 1);
ms2BaseTimeline.setStartTime(0);
// 1-ms test timeline (with a baseTimeline) using 2 included and 2 excluded segments
// centered inside each base segment
//
// The ms2Timeline without a base would look like this:
//
// timeline start time = 1
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \____ _____/ \_/
// \/ |
// segment group segment size = 1 ms
//
// With the base timeline some originally included segments are now removed (see "xx"
// below):
//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE|xx|xx|EE|EE| | |EE|EE|xx|xx|EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
//
ms2Timeline = new SegmentedTimeline(1, 2, 2);
ms2Timeline.setStartTime(1);
ms2Timeline.setBaseTimeline(ms2BaseTimeline);
// test monday though friday timeline
mondayFridayTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
// test 9am-4pm Monday through Friday timeline
fifteenMinTimeline = SegmentedTimeline.newFifteenMinuteTimeline();
// find first Monday after 2001-01-01
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.set(2001, 0, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, 1);
}
monday = (Calendar) cal.clone();
// calculate 9am on the first Monday after 2001-01-01
cal.add(Calendar.HOUR, 9);
monday9am = (Calendar) cal.clone();
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @throws Exception if there is a problem.
*/
protected void tearDown() throws Exception {
}
//////////////////////////////////////////////////////////////////////////
// test construction process
//////////////////////////////////////////////////////////////////////////
/**
* Tests that the new method that created the msTimeline segmented
* timeline did so correctly.
*/
public void testMsSegmentedTimeline() {
// verify attributes set during object construction
assertEquals(1, msTimeline.getSegmentSize());
assertEquals(0, msTimeline.getStartTime());
assertEquals(5, msTimeline.getSegmentsIncluded());
assertEquals(2, msTimeline.getSegmentsExcluded());
}
/**
* Tests that the new method that created the ms2Timeline segmented
* timeline did so correctly.
*/
public void testMs2SegmentedTimeline() {
// verify attributes set during object construction
assertEquals(1, ms2Timeline.getSegmentSize());
assertEquals(1, ms2Timeline.getStartTime());
assertEquals(2, ms2Timeline.getSegmentsIncluded());
assertEquals(2, ms2Timeline.getSegmentsExcluded());
assertEquals(ms2BaseTimeline, ms2Timeline.getBaseTimeline());
}
/**
* Tests that the factory method that creates Monday through Friday segmented
* timeline does so correctly.
*/
public void testMondayThroughFridaySegmentedTimeline() {
// verify attributes set during object construction
assertEquals(SegmentedTimeline.DAY_SEGMENT_SIZE, mondayFridayTimeline.getSegmentSize());
assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900,
mondayFridayTimeline.getStartTime());
assertEquals(5, mondayFridayTimeline.getSegmentsIncluded());
assertEquals(2, mondayFridayTimeline.getSegmentsExcluded());
}
/**
* Tests that the factory method that creates a 15-min 9:00 AM 4:00 PM
* segmented axis does so correctly.
*/
public void testFifteenMinSegmentedTimeline() {
assertEquals(SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE,
fifteenMinTimeline.getSegmentSize());
assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900 + 36
* fifteenMinTimeline.getSegmentSize(),
fifteenMinTimeline.getStartTime());
assertEquals(28, fifteenMinTimeline.getSegmentsIncluded());
assertEquals(68, fifteenMinTimeline.getSegmentsExcluded());
}
//////////////////////////////////////////////////////////////////////////
// test one-segment and adjacent segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests one segment of the ms timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMsSegment() {
verifyOneSegment(msTimeline);
}
/**
* Tests one segment of the ms timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMs2Segment() {
verifyOneSegment(ms2Timeline);
}
/**
* Tests one segment of the Monday through Friday timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMondayThroughFridaySegment() {
verifyOneSegment(mondayFridayTimeline);
}
/**
* Tests one segment of the Fifteen timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testFifteenMinSegment() {
verifyOneSegment(fifteenMinTimeline);
}
/**
* 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(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());
}
}
//////////////////////////////////////////////////////////////////////////
// test inc methods
//////////////////////////////////////////////////////////////////////////
/**
* Tests the inc methods on the msTimeline.
*/
public void testMsInc() {
verifyInc(msTimeline);
}
/**
* Tests the inc methods on the msTimeline.
*/
public void testMs2Inc() {
verifyInc(ms2Timeline);
}
/**
* Tests the inc methods on the Monday through Friday timeline.
*/
public void testMondayThroughFridayInc() {
verifyInc(mondayFridayTimeline);
}
/**
* Tests the inc methods on the Fifteen minute timeline.
*/
public void testFifteenMinInc() {
verifyInc(fifteenMinTimeline);
}
/**
* 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();
}
}
}
//////////////////////////////////////////////////////////////////////////
// main include and excluded segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests that the msTimeline's included and excluded
* segments are being calculated correctly.
*/
public void testMsIncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(msTimeline, 0);
}
/**
* Tests that the ms2Timeline's included and excluded
* segments are being calculated correctly.
*/
public void testMs2IncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(ms2Timeline, 1);
}
/**
* Tests that the Monday through Friday timeline's included and excluded
* segments are being calculated correctly. The test is performed starting
* on the first monday after 1/1/2000 and for five years.
*/
public void testMondayThroughFridayIncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(mondayFridayTimeline, monday.getTime().getTime());
}
/**
* Tests that the Fifteen-Min timeline's included and excluded
* segments are being calculated correctly. The test is performed starting
* on the first monday after 1/1/2000 and for five years.
*/
public void testFifteenMinIncludedAndExcludedSegments() {
verifyIncludedAndExcludedSegments(fifteenMinTimeline, monday9am.getTime().getTime());
}
/**
* 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();
}
}
//////////////////////////////////////////////////////////////////////////
// test exception segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests methods related to exceptions methods in the msTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMsExceptionSegments() throws ParseException {
verifyExceptionSegments(msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
}
/**
* Tests methods related to exceptions methods in the ms2BaseTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2BaseTimelineExceptionSegments() throws ParseException {
verifyExceptionSegments(ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
}
/**
* Tests methods related to exceptions methods in the mondayFridayTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMondayThoughFridayExceptionSegments() throws ParseException {
verifyExceptionSegments(mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT);
}
/**
* Tests methods related to exceptions methods in the fifteenMinTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testFifteenMinExceptionSegments() throws ParseException {
verifyExceptionSegments(fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
}
/**
* 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);
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 + 1, exception[m - 1] - 1));
}
}
//////////////////////////////////////////////////////////////////////////
// test timeline translations
//////////////////////////////////////////////////////////////////////////
/**
* Tests translations for 1-ms timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMsTranslations() throws ParseException {
verifyFillInExceptions(msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
verifyTranslations(msTimeline, 0);
}
/**
* Tests translations for the base timeline used for the ms2Timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2BaseTimelineTranslations() throws ParseException {
verifyFillInExceptions(ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
verifyTranslations(ms2BaseTimeline, 0);
}
/**
* Tests translations for the Monday through Friday timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2Translations() throws ParseException {
fillInBaseTimelineExceptions(ms2Timeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
fillInBaseTimelineExclusionsAsExceptions(ms2Timeline, 0, 5000);
verifyTranslations(ms2Timeline, 1);
}
/**
* Tests translations for the Monday through Friday timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMondayThroughFridayTranslations() throws ParseException {
verifyFillInExceptions(mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT);
verifyTranslations(mondayFridayTimeline, monday.getTime().getTime());
}
/**
* Tests translations for the Fifteen Min timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testFifteenMinTranslations() throws ParseException {
verifyFillInExceptions(fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
fillInBaseTimelineExceptions(fifteenMinTimeline, US_HOLIDAYS, DATE_FORMAT);
fillInBaseTimelineExclusionsAsExceptions(fifteenMinTimeline,
monday9am.getTime().getTime(),
monday9am.getTime().getTime() + FIVE_YEARS);
verifyTranslations(fifteenMinTimeline, monday9am.getTime().getTime());
}
/**
* 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();
}
}
}
//////////////////////////////////////////////////////////////////////////
// test serialization
//////////////////////////////////////////////////////////////////////////
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
verifySerialization(msTimeline);
verifySerialization(ms2Timeline);
verifySerialization(ms2BaseTimeline);
verifySerialization(SegmentedTimeline.newMondayThroughFridayTimeline());
verifySerialization(SegmentedTimeline.newFifteenMinuteTimeline());
}
/**
* Tests serialization of an instance.
* @param a1 The timeline to verify the serialization
*/
private void verifySerialization(SegmentedTimeline a1) {
SegmentedTimeline a2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(a1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
a2 = (SegmentedTimeline) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertEquals(a1, a2);
}
/**
* Adds an array of exceptions to the timeline. The timeline exception list
* is first cleared.
* @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 value
* @throws ParseException If there is any exception parsing each exceptions
* value.
* @return An array of Dates[] containing each exception date.
*/
private long[] verifyFillInExceptions(SegmentedTimeline timeline,
String[] exceptionString,
Format fmt) throws ParseException {
// make sure there are no exceptions
timeline.setExceptionSegments(new java.util.ArrayList());
assertEquals(0, timeline.getExceptionSegments().size());
// add our exceptions and store locally in ArrayList of Longs
ArrayList exceptionList = new ArrayList();
for (int i = 0; i < exceptionString.length; i++) {
long e;
if (fmt instanceof NumberFormat) {
e = ((NumberFormat) fmt).parse(exceptionString).longValue();
}
else {
e = timeline.getTime(((SimpleDateFormat) fmt).parse(exceptionString));
}
// only add an exception if it is currently an included segment
SegmentedTimeline.Segment segment = timeline.getSegment(e);
if (segment.inIncludeSegments()) {
timeline.addException(e);
exceptionList.add(new Long(e));
assertEquals(exceptionList.size(), timeline.getExceptionSegments().size());
assertTrue(segment.inExceptionSegments());
}
}
// make array of exceptions
long[] exception = new long[exceptionList.size()];
int i = 0;
for (Iterator iter = exceptionList.iterator(); iter.hasNext();) {
Long l = (Long) iter.next();
exception[i++] = l.longValue();
}
return (exception);
}
/**
* 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 value
* @throws ParseException If there is any exception parsing each exceptions
* 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).longValue();
}
else {
e = timeline.getTime(((SimpleDateFormat) fmt).parse(exceptionString));
}
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());
}
}
}
}
/**
* 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());
}
}
}
}
}
/**
* Confirm that cloning works.
*/
public void testCloning() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = null;
try {
l2 = (SegmentedTimeline) l1.clone();
}
catch (CloneNotSupportedException e) {
System.err.println("SegmentedTimelineTests2.testCloning: failed to clone.");
}
assertTrue(l1 != l2);
assertTrue(l1.getClass() == l2.getClass());
assertTrue(l1.equals(l2));
}
/**
* Confirm that the equals method can distinguish all the required fields.
*/
public void testEquals() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = new SegmentedTimeline(1000, 5, 2);
assertTrue(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1001, 5, 2);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 4, 2);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 5, 1);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 5, 2);
// start time...
l1.setStartTime(1234L);
assertFalse(l1.equals(l2));
l2.setStartTime(1234L);
assertTrue(l1.equals(l2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization2() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(l1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
l2 = (SegmentedTimeline) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
boolean b = l1.equals(l2);
assertTrue(b);
}
//////////////////////////////////////////////////////////////////////////
// utility methods
//////////////////////////////////////////////////////////////////////////
/**
* Formats a Calendar object avoiding converting into a Date so the local
* TimeZone is not used to convert the output. The Calendar object used as
* parameter is assumed to use the SegmentedTimeline.TIME_ZONE time zone.
*
* @param cal Calendar object to format
* @return
*/
/*
private String calToString(Calendar cal) {
DecimalFormat fmt2 = new DecimalFormat("00");
DecimalFormat fmt4 = new DecimalFormat("0000");
return (getDayOfWeekString(cal)+" "+
fmt2.format(cal.get(Calendar.MONTH)+1)+"/"+
fmt2.format(cal.get(Calendar.DATE))+"/"+
fmt4.format(cal.get(Calendar.YEAR))+" "+
fmt2.format(cal.get(Calendar.HOUR_OF_DAY))+":"+
fmt2.format(cal.get(Calendar.MINUTE))+":"+
fmt2.format(cal.get(Calendar.SECOND))+":"+
fmt4.format(cal.get(Calendar.MILLISECOND)));
}
*/
/**
* Formats time avoiding converting into a Date so the local
* TimeZone is not used to convert the output. The time used as
* parameter is assumed to be relative to SegmentedTimeline.TIME_ZONE time zone.
*
* @param time time to format
* @return
*/
/*
private String calToString(long time) {
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.setTimeInMillis(time);
return (calToString(cal));
}
*/
/**
* Returns a three letter day of the week string for Calendar formatting.
* @param cal Calendar to check
* @return
*/
/*
private String getDayOfWeekString(Calendar cal) {
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY: return ("Sun");
case Calendar.MONDAY: return ("Mon");
case Calendar.TUESDAY: return ("Tue");
case Calendar.WEDNESDAY: return ("Wed");
case Calendar.THURSDAY: return ("Thu");
case Calendar.FRIDAY: return ("Fri");
case Calendar.SATURDAY: return ("Sat");
default: return ("???");
}
}
*/
/**
* Tests a basic segmented timeline.
*/
public void testBasicSegmentedTimeline() {
SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3);
stl.setStartTime(946684800000L); // 1-Jan-2000
assertFalse(stl.containsDomainValue(946684799999L));
assertTrue(stl.containsDomainValue(946684800000L));
assertTrue(stl.containsDomainValue(946684800019L));
assertFalse(stl.containsDomainValue(946684800020L));
assertFalse(stl.containsDomainValue(946684800049L));
assertTrue(stl.containsDomainValue(946684800050L));
assertTrue(stl.containsDomainValue(946684800069L));
assertFalse(stl.containsDomainValue(946684800070L));
assertFalse(stl.containsDomainValue(946684800099L));
assertTrue(stl.containsDomainValue(946684800100L));
assertEquals(0, stl.toTimelineValue(946684800000L));
assertEquals(19, stl.toTimelineValue(946684800019L));
assertEquals(20, stl.toTimelineValue(946684800020L));
assertEquals(20, stl.toTimelineValue(946684800049L));
assertEquals(20, stl.toTimelineValue(946684800050L));
assertEquals(39, stl.toTimelineValue(946684800069L));
assertEquals(40, stl.toTimelineValue(946684800070L));
assertEquals(40, stl.toTimelineValue(946684800099L));
assertEquals(40, stl.toTimelineValue(946684800100L));
assertEquals(946684800000L, stl.toMillisecond(0));
assertEquals(946684800019L, stl.toMillisecond(19));
assertEquals(946684800050L, stl.toMillisecond(20));
assertEquals(946684800069L, stl.toMillisecond(39));
assertEquals(946684800100L, stl.toMillisecond(40));
}
/**
* Tests a basic time line with one exception.
*/
public void testSegmentedTimelineWithException1() {
SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3);
stl.setStartTime(946684800000L); // 1-Jan-2000
stl.addException(946684800050L);
assertFalse(stl.containsDomainValue(946684799999L));
assertTrue(stl.containsDomainValue(946684800000L));
assertTrue(stl.containsDomainValue(946684800019L));
assertFalse(stl.containsDomainValue(946684800020L));
assertFalse(stl.containsDomainValue(946684800049L));
assertFalse(stl.containsDomainValue(946684800050L));
assertFalse(stl.containsDomainValue(946684800059L));
assertTrue(stl.containsDomainValue(946684800060L));
assertTrue(stl.containsDomainValue(946684800069L));
assertFalse(stl.containsDomainValue(946684800070L));
assertFalse(stl.containsDomainValue(946684800099L));
assertTrue(stl.containsDomainValue(946684800100L));
//long v = stl.toTimelineValue(946684800020L);
assertEquals(0, stl.toTimelineValue(946684800000L));
assertEquals(19, stl.toTimelineValue(946684800019L));
assertEquals(20, stl.toTimelineValue(946684800020L));
assertEquals(20, stl.toTimelineValue(946684800049L));
assertEquals(20, stl.toTimelineValue(946684800050L));
assertEquals(29, stl.toTimelineValue(946684800069L));
assertEquals(30, stl.toTimelineValue(946684800070L));
assertEquals(30, stl.toTimelineValue(946684800099L));
assertEquals(30, stl.toTimelineValue(946684800100L));
assertEquals(946684800000L, stl.toMillisecond(0));
assertEquals(946684800019L, stl.toMillisecond(19));
assertEquals(946684800060L, stl.toMillisecond(20));
assertEquals(946684800069L, stl.toMillisecond(29));
assertEquals(946684800100L, stl.toMillisecond(30));
}
//////////////////////////////////////////////////////////////////////////
// main method only for debug
//////////////////////////////////////////////////////////////////////////
/**
* Only use to debug JUnit suite.
*
* @param args ignored.
*
* @throws Exception if there is some problem.
*/
public static void main(String[] args) throws Exception {
SegmentedTimelineTests test = new SegmentedTimelineTests("Test");
test.setUp();
test.testMondayThoughFridayExceptionSegments();
test.tearDown();
}
}
============================================
Thanks
venuGopal
Actually I want that, to solve my problem explained in previouse post.
Check this link,
http://www.jfree.org/phpBB2/viewtopic.php?t=13287
can u suggest any idea Gopal?
Check this link,
http://www.jfree.org/phpBB2/viewtopic.php?t=13287
can u suggest any idea Gopal?
hai sarika
u can check this code...
u can get a solution
=================================================
// setup our test timelines
//
// Legend for comments below:
// <spaces> = Segments included in the final timeline
// EE = Excluded segments via timeline rules
// xx = Exception segments inherited from base timeline exclusions
// 1-ms test timeline using 5 included and 2 excluded segments.
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | | |EE|EE| | | | | |EE|EE| | | | | | |EE|EE| <-- msTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \_________ ________/ \_/
// \/ |
// segment group segment size = 1 ms
//
msTimeline = new SegmentedTimeline(1, 5, 2);
msTimeline.setStartTime(0);
// 4-ms test base timeline for ms2Timeline using 1 included and 1 excluded segments
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \__________ _________/ \____ _____/
// \/ \/
// segment group segment size = 4 ms
//
ms2BaseTimeline = new SegmentedTimeline(4, 1, 1);
ms2BaseTimeline.setStartTime(0);
// 1-ms test timeline (with a baseTimeline) using 2 included and 2 excluded segments
// centered inside each base segment
//
// The ms2Timeline without a base would look like this:
//
// timeline start time = 1
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \____ _____/ \_/
// \/ |
// segment group segment size = 1 ms
//
// With the base timeline some originally included segments are now removed (see "xx"
// below):
//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE|xx|xx|EE|EE| | |EE|EE|xx|xx|EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
//
ms2Timeline = new SegmentedTimeline(1, 2, 2);
ms2Timeline.setStartTime(1);
ms2Timeline.setBaseTimeline(ms2BaseTimeline);
// test monday though friday timeline
mondayFridayTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
// test 9am-4pm Monday through Friday timeline
fifteenMinTimeline = SegmentedTimeline.newFifteenMinuteTimeline();
// find first Monday after 2001-01-01
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.set(2001, 0, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, 1);
}
monday = (Calendar) cal.clone();
// calculate 9am on the first Monday after 2001-01-01
cal.add(Calendar.HOUR, 9);
monday9am = (Calendar) cal.clone();
================================================
Waiting for your reply
Thanks
venu
u can check this code...
u can get a solution
=================================================
// setup our test timelines
//
// Legend for comments below:
// <spaces> = Segments included in the final timeline
// EE = Excluded segments via timeline rules
// xx = Exception segments inherited from base timeline exclusions
// 1-ms test timeline using 5 included and 2 excluded segments.
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | | |EE|EE| | | | | |EE|EE| | | | | | |EE|EE| <-- msTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \_________ ________/ \_/
// \/ |
// segment group segment size = 1 ms
//
msTimeline = new SegmentedTimeline(1, 5, 2);
msTimeline.setStartTime(0);
// 4-ms test base timeline for ms2Timeline using 1 included and 1 excluded segments
//
// timeline start time = 0
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \__________ _________/ \____ _____/
// \/ \/
// segment group segment size = 4 ms
//
ms2BaseTimeline = new SegmentedTimeline(4, 1, 1);
ms2BaseTimeline.setStartTime(0);
// 1-ms test timeline (with a baseTimeline) using 2 included and 2 excluded segments
// centered inside each base segment
//
// The ms2Timeline without a base would look like this:
//
// timeline start time = 1
// |
// v
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// \____ _____/ \_/
// \/ |
// segment group segment size = 1 ms
//
// With the base timeline some originally included segments are now removed (see "xx"
// below):
//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// |EE| | |EE|EE|xx|xx|EE|EE| | |EE|EE|xx|xx|EE|EE| | |EE| <-- ms2Timeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
// | | | | |EE|EE|EE|EE| | | | |EE|EE|EE|EE| | | | | <-- ms2BaseTimeline
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
//
ms2Timeline = new SegmentedTimeline(1, 2, 2);
ms2Timeline.setStartTime(1);
ms2Timeline.setBaseTimeline(ms2BaseTimeline);
// test monday though friday timeline
mondayFridayTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
// test 9am-4pm Monday through Friday timeline
fifteenMinTimeline = SegmentedTimeline.newFifteenMinuteTimeline();
// find first Monday after 2001-01-01
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.set(2001, 0, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, 1);
}
monday = (Calendar) cal.clone();
// calculate 9am on the first Monday after 2001-01-01
cal.add(Calendar.HOUR, 9);
monday9am = (Calendar) cal.clone();
================================================
Waiting for your reply
Thanks
venu
I would like to talk to you VenuGopal . I have many doubts.
If you have yahoo id you can add me in the messenger list.
So that i can talk to u.
my id is : sarika_ture@yahoo.com
bye and have a nice day!
If you have yahoo id you can add me in the messenger list.
So that i can talk to u.
my id is : sarika_ture@yahoo.com
bye and have a nice day!
Forum view
-------------
http://www.jfree.org/phpBB2/viewtopic.p ... 8&start=15
Sorry due to time constraint, I only able to do a simple solution(demo) for Non-Trading Day Exclusion (Holiday, suspend-day, Saturday, Sunday)
Anyone who interested can plug in this two class (JHistoricalCSChart & ConfigSegmentedTimeline) and u guys & girls can find the keyword 'ziwei', theres the place I had edited.
However if anyone using Apache Http Server or Tomcat Web Server and willing to direct plug in in Web-INF > classes. U may request from me, and I email whole package source for you, jar also can.
* Version : jfreechart-1.0.0-rc1.jar
My email address is wzwei28@yahoo.com (any comments and query are welcomed).
hope can contribute little effort in open source application.
ConfigSegmentedTimeline.java
------------------------------------
JHistoricalCSChart------------------------
* Well now I am still solving bugs in overlapping and Exclusion of intraday gap (hour. i.e. 08:45-12:45 , rest, 14:30-17:15) and I want to exclude the gap in the rest session and also market close hour(17:15-08:45 another morning) for historical movement chart).
* If anyone have some solution, please email to me wzwei28@yahoo.com, and I help to publish to everyone. Thanks!
-------------
http://www.jfree.org/phpBB2/viewtopic.p ... 8&start=15
Sorry due to time constraint, I only able to do a simple solution(demo) for Non-Trading Day Exclusion (Holiday, suspend-day, Saturday, Sunday)
Anyone who interested can plug in this two class (JHistoricalCSChart & ConfigSegmentedTimeline) and u guys & girls can find the keyword 'ziwei', theres the place I had edited.
However if anyone using Apache Http Server or Tomcat Web Server and willing to direct plug in in Web-INF > classes. U may request from me, and I email whole package source for you, jar also can.
* Version : jfreechart-1.0.0-rc1.jar
My email address is wzwei28@yahoo.com (any comments and query are welcomed).
hope can contribute little effort in open source application.
ConfigSegmentedTimeline.java
------------------------------------
Code: Select all
/*
* (C) Copyright 2000-2006, by Wong Zi Wei.
*
*
* Original Author : Wong Zi Wei(WZW);
* Contributor(s) : -;
*
* Email : wzwei28@yahoo.com
*
* Version 1.0.1 : 21/07/2006(WZW) Store and calculate the SegmentedTimeline
*
* Error Code : 001
* 002
* 003
*
*
*/
package cfg;
import java.util.Date;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
//---segmentedTimeline---
import java.util.Calendar;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.SegmentedTimeline;
public class ConfigSegmentedTimeline{
private static long m_lFirstMonday = 0;
private static ArrayList m_alHDate = new ArrayList();
private static SegmentedTimeline m_segmentedTimeline = null;
private static JFreeChart m_chart = null;
private static Date m_aTrdDate[] = new Date[1];
private static Date m_dtDateInit = null;
public void configSegmentedTimeline(){
}
public static void run(){
//@@ Segmented Timeline - Exclude Non-Trading Day, Suspended date, Saturday & Sunday
Date dtInit = getDateInit();
JFreeChart chart = getChart();
DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
//SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
SegmentedTimeline segmentedTimeline = new SegmentedTimeline(SegmentedTimeline.DAY_SEGMENT_SIZE, 5, 2);
if(dtInit!=null){
SimpleDateFormat sdfYC = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfMC = new SimpleDateFormat("MM");
SimpleDateFormat sdfDC = new SimpleDateFormat("dd");
int iYC = Integer.parseInt(sdfYC.format(dtInit));
int iMC = Integer.parseInt(sdfMC.format(dtInit));
int iDC = Integer.parseInt(sdfDC.format(dtInit));
//-Select your Timezone-
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+8")); //use standard GMT + 0
//calendar.set(2006, Calendar.MARCH, 25, 12, 0, 0);
Date d1 = calendar.getTime();
//GregorianCalendar calendar = new GregorianCalendar();
ArrayList alHDate = new ArrayList(); //HDate-Hide Date
Date aTrdDate[] = getTrdDate();
Date dtDate = calendar.getTime();
int iTrdDateLast = aTrdDate.length-1;
int iIndex = 0;
if(aTrdDate.length>0){
Date aTrdDateInit = aTrdDate[0];
Date aTrdDateLast = aTrdDate[iTrdDateLast];
calendar.setTime(aTrdDateInit); //set init date
double dDays = (aTrdDateLast.getTime()-aTrdDateInit.getTime())/1000/60/60/24; //60 minutes, 60 seconds, 24 hour.
for(int i=0; i<=dDays; i++){
dtDate = calendar.getTime();
if(dtDate.equals(aTrdDate[iIndex])){
iIndex++;
}else{
alHDate.add(dtDate);
}
calendar.add(Calendar.DATE, 1);//increment 1 day
}
}
int offset = TimeZone.getDefault().getRawOffset();
TimeZone NO_DST_TIME_ZONE = new SimpleTimeZone(offset, "UTC-" + offset);
Calendar cal = new GregorianCalendar(NO_DST_TIME_ZONE);
cal.set(iYC, 0, 1, 0, 0, 0); //can't use iMC and iDC, because the crosshair have bugs in locking data
cal.set(Calendar.MILLISECOND, 0);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, 1);
}
long lFirstMonday = cal.getTime().getTime();
//Store parameter
setFirstMonday(lFirstMonday);
setHDate(alHDate);
setSegmentedTimeline(segmentedTimeline);
}
}
private static void setFirstMonday(long vs_lFirstMonday){ m_lFirstMonday = vs_lFirstMonday; }
private static void setHDate(ArrayList vs_alHDate){ m_alHDate = vs_alHDate; }
private static void setSegmentedTimeline(SegmentedTimeline vs_segmentedTimeline){ m_segmentedTimeline = vs_segmentedTimeline; }
public static void setChart(JFreeChart vs_chart){ m_chart = vs_chart; }
public static void setTrdDate(Date[] vs_aTrdDate){ m_aTrdDate = vs_aTrdDate; }
public static void setDateInit(Date vs_dtDateInit){ m_dtDateInit = vs_dtDateInit; }
public static long getFirstMonday(){ return m_lFirstMonday; }
public static ArrayList getHDate(){ return m_alHDate; }
public static SegmentedTimeline getSegmentedTimeline(){ return m_segmentedTimeline; }
public static JFreeChart getChart(){ return m_chart; }
public static Date[] getTrdDate(){ return m_aTrdDate; }
public static Date getDateInit(){ return m_dtDateInit; }
public static void main(String[] args){
}
}
JHistoricalCSChart------------------------
Code: Select all
package demo;
import java.awt.Dimension;
import java.util.Date;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.date.DateUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.NumberAxis;
//-ziwei import-
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.SegmentedTimeline;
public class JHistoricalCSChart extends ApplicationFrame{
//-ziwei import-
private static cfg.ConfigSegmentedTimeline m_CST;
public JHistoricalCSChart(String s)
{
super(s);
OHLCDataset ohlcdataset = createDataset();
JFreeChart jfreechart = createChart(ohlcdataset);
jfreechart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setRangeZoomable(false);
chartpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartpanel);
}
private static JFreeChart createChart(OHLCDataset ohlcdataset)
{
JFreeChart jfreechart = ChartFactory.createCandlestickChart("Historical Non-Trading Day Exclusion", "Time", "Value", ohlcdataset, true);
jfreechart.setAntiAlias(false);
XYPlot xyplot = jfreechart.getXYPlot();
//@@ edit: solution is to se the autoRangeIncludesZero to false, so that no more return to 0 for candlestick.
NumberAxis numberAxis = (NumberAxis) xyplot.getRangeAxis();
numberAxis.setAutoRangeIncludesZero(false); //exclude 0;
xyplot.setRangeAxis(numberAxis);
//@@ edit: when zooming in & out, u will notice the range had move back to 0,
//@@ edit: set Margin of range(highest & lowest margin)
ValueAxis rangeAxis = (ValueAxis) xyplot.getRangeAxis();
rangeAxis.setAutoRange(false); //no use, because in ValueAxis:In resizeRange(% > 0.0)->setAutoRange(true)->autoAdjustRange() still run it
//rangeAxis.setRangeWithMargins(20,100);
rangeAxis.setRange(20,100);
xyplot.setRangeAxis(rangeAxis);
//-ziwei Edit(Optional)-
DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis();
domainAxis.setVerticalTickLabels(true);
domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
//---Set bar chart to middle---
domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
//==================================================================================
//@@ Segmented Timeline - Exclude Holiday, Non-Trading Day, Suspended date, Saturday & Sunday
m_CST.setChart(jfreechart);
m_CST.run();
SegmentedTimeline segmentedTimeline = m_CST.getSegmentedTimeline();
long lFirstMonday = m_CST.getFirstMonday();
ArrayList alHDate = m_CST.getHDate();
segmentedTimeline.setStartTime(lFirstMonday); //Must remake the calendar segment to avoid overlapping (Monday-Friday)
segmentedTimeline.addExceptions(alHDate); //Exclude non-trading day
DateAxis axis = (DateAxis) jfreechart.getXYPlot().getDomainAxis();
axis.setTimeline(segmentedTimeline);
return jfreechart;
}
public static OHLCDataset createDataset()
{
Date aDate[] = new Date[9]; //(11-2=9),47
double ad[] = new double[9];
double ad1[] = new double[9];
double ad2[] = new double[9];
double ad3[] = new double[9];
double ad4[] = new double[9];
int i = 1;
byte byte0 = 2;
//-------------------------
//ziwei edit
//-1st week(monday-friday)-
aDate[0] = DateUtilities.createDate(2007, 12, 24, 12, 0);
ad[0] = 47D;
ad1[0] = 33D;
ad2[0] = 35D;
ad3[0] = 33D;
ad4[0] = 100D;
//-Holiday Christmas-
/*
aDate[1] = DateUtilities.createDate(2007, 12, 25, 12, 0);
ad[1] = 47D;
ad1[1] = 32D;
ad2[1] = 41D;
ad3[1] = 37D;
ad4[1] = 150D;
*/
aDate[1] = DateUtilities.createDate(2007, 12, 26, 12, 0);
ad[1] = 49D;
ad1[1] = 43D;
ad2[1] = 46D;
ad3[1] = 48D;
ad4[1] = 70D;
aDate[2] = DateUtilities.createDate(2007, 12, 27, 12, 0);
ad[2] = 51D;
ad1[2] = 39D;
ad2[2] = 40D;
ad3[2] = 47D;
ad4[2] = 200D;
aDate[3] = DateUtilities.createDate(2007, 12, 28, 12, 0);
ad[3] = 60D;
ad1[3] = 40D;
ad2[3] = 46D;
ad3[3] = 53D;
ad4[3] = 120D;
//-------------------------
//-2nd week(monday-friday)-
aDate[4] = DateUtilities.createDate(2007, 12, 31, 12, 0);
ad[4] = 62D;
ad1[4] = 55D;
ad2[4] = 57D;
ad3[4] = 61D;
ad4[4] = 110D;
//-New Year Holiday-
/*
aDate[6] = DateUtilities.createDate(2008, 1, 1, 12, 0);
ad[6] = 65D;
ad1[6] = 56D;
ad2[6] = 62D;
ad3[6] = 59D;
ad4[6] = 70D;
*/
aDate[5] = DateUtilities.createDate(2008, 1, 2, 12, 0);
ad[5] = 55D;
ad1[5] = 43D;
ad2[5] = 45D;
ad3[5] = 47D;
ad4[5] = 20D;
aDate[6] = DateUtilities.createDate(2008, 1, 3, 12, 0);
ad[6] = 54D;
ad1[6] = 33D;
ad2[6] = 40D;
ad3[6] = 51D;
ad4[6] = 30D;
aDate[7] = DateUtilities.createDate(2008, 1, 4, 12, 0);
ad[7] = 47D;
ad1[7] = 33D;
ad2[7] = 35D;
ad3[7] = 33D;
ad4[7] = 100D;
//-------------------------
//-3rd week(monday-friday)-
aDate[8] = DateUtilities.createDate(2008, 1, 7, 12, 0);
ad[8] = 54D;
ad1[8] = 38D;
ad2[8] = 43D;
ad3[8] = 52D;
ad4[8] = 50D;
//-------------------------
//---------------------------------------------------
//@@ SegmentedTimeline - Exclude Holiday, Non-Trading Day, Suspended date, Saturday & Sunday
m_CST.setDateInit(aDate[0]);
m_CST.setTrdDate(aDate);
return new DefaultHighLowDataset("Series 1", aDate, ad, ad1, ad2, ad3, ad4);
}
public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String args[])
{
JHistoricalCSChart JHistoricalCSChart = new JHistoricalCSChart("Historical Non-Trading Day Exclusion");
JHistoricalCSChart.pack();
RefineryUtilities.centerFrameOnScreen(JHistoricalCSChart);
JHistoricalCSChart.setVisible(true);
}
}
* Well now I am still solving bugs in overlapping and Exclusion of intraday gap (hour. i.e. 08:45-12:45 , rest, 14:30-17:15) and I want to exclude the gap in the rest session and also market close hour(17:15-08:45 another morning) for historical movement chart).
* If anyone have some solution, please email to me wzwei28@yahoo.com, and I help to publish to everyone. Thanks!