📄 segmentedtimeline.java
字号:
* @return The size in milliseconds. */ public long getSegmentsExcludedSize() { return this.segmentsExcludedSize; } /** * Returns the number of segments in a segment group. This will be equal to * segments included plus segments excluded. * * @return The number of segments. */ public int getGroupSegmentCount() { return this.groupSegmentCount; } /** * Returns the size in milliseconds of a segment group. This will be equal to * size of the segments included plus the size of the segments excluded. * * @return The segment group size in milliseconds. */ public long getSegmentsGroupSize() { return this.segmentsGroupSize; } /** * Returns the number of segments included per segment group. * * @return The number of segments. */ public int getSegmentsIncluded() { return this.segmentsIncluded; } /** * Returns the size in ms of the segments included per segment group. * * @return The segment size in milliseconds. */ public long getSegmentsIncludedSize() { return this.segmentsIncludedSize; } /** * Returns the size of one segment in ms. * * @return The segment size in milliseconds. */ public long getSegmentSize() { return this.segmentSize; } /** * Returns a list of all the exception segments. This list is not modifiable. * * @return The exception segments. */ public List getExceptionSegments() { return Collections.unmodifiableList(this.exceptionSegments); } /** * Sets the exception segments list. * * @param exceptionSegments the exception segments. */ public void setExceptionSegments(List exceptionSegments) { this.exceptionSegments = exceptionSegments; } /** * Returns our baseTimeline, or <code>null</code> if none. * * @return The base timeline. */ public SegmentedTimeline getBaseTimeline() { return this.baseTimeline; } /** * Sets the base timeline. * * @param baseTimeline the timeline. */ public void setBaseTimeline(SegmentedTimeline baseTimeline) { // verify that baseTimeline is compatible with us if (baseTimeline != null) { if (baseTimeline.getSegmentSize() < this.segmentSize) { throw new IllegalArgumentException("baseTimeline.getSegmentSize() is smaller " + "than segmentSize"); } else if (baseTimeline.getStartTime() > this.startTime) { throw new IllegalArgumentException("baseTimeline.getStartTime() is after than " + "startTime"); } else if ((baseTimeline.getSegmentSize() % this.segmentSize) != 0) { throw new IllegalArgumentException( "baseTimeline.getSegmentSize() is not multiple of segmentSize"); } else if (((this.startTime - baseTimeline.getStartTime()) % this.segmentSize) != 0) { throw new IllegalArgumentException( "baseTimeline is not aligned"); } } this.baseTimeline = baseTimeline; } /** * Translates a value relative to the domain value (all Dates) into a value * relative to the segmented timeline. The values relative to the segmented timeline * are all consecutives starting at zero at the startTime. * * @param millisecond the millisecond (as encoded by java.util.Date). * * @return The timeline value. */ public long toTimelineValue(long millisecond) { long result; long rawMilliseconds = millisecond - this.startTime; long groupMilliseconds = rawMilliseconds % this.segmentsGroupSize; long groupIndex = rawMilliseconds / this.segmentsGroupSize; if (groupMilliseconds >= this.segmentsIncludedSize) { result = toTimelineValue(this.startTime + this.segmentsGroupSize * (groupIndex + 1)); } else { Segment segment = getSegment(millisecond); if (segment.inExceptionSegments()) { result = toTimelineValue(segment.getSegmentEnd() + 1); } else { long shiftedSegmentedValue = millisecond - this.startTime; long x = shiftedSegmentedValue % this.segmentsGroupSize; long y = shiftedSegmentedValue / this.segmentsGroupSize; long wholeExceptionsBeforeDomainValue = getExceptionSegmentCount(this.startTime, millisecond - 1);// long partialTimeInException = 0;// Segment ss = getSegment(millisecond);// if (ss.inExceptionSegments()) {// partialTimeInException = millisecond - ss.getSegmentStart();// } if (x < this.segmentsIncludedSize) { result = this.segmentsIncludedSize * y + x - wholeExceptionsBeforeDomainValue * this.segmentSize; // - partialTimeInException;; } else { result = this.segmentsIncludedSize * (y + 1) - wholeExceptionsBeforeDomainValue * this.segmentSize; // - partialTimeInException; } } } return result; } /** * Translates a date into a value relative to the segmented timeline. The values relative * to the segmented timeline are all consecutives starting at zero at the startTime. * * @param date date relative to the domain. * * @return The timeline value (in milliseconds). */ public long toTimelineValue(Date date) { return toTimelineValue(getTime(date)); //return toTimelineValue(dateDomainValue.getTime()); } /** * Translates a value relative to the timeline into a millisecond. * * @param timelineValue the timeline value (in milliseconds). * * @return The domain value (in milliseconds). */ public long toMillisecond(long timelineValue) { // calculate the result as if no exceptions Segment result = new Segment(this.startTime + timelineValue + (timelineValue / this.segmentsIncludedSize) * this.segmentsExcludedSize); long lastIndex = this.startTime; // adjust result for any exceptions in the result calculated while (lastIndex <= result.segmentStart) { // skip all whole exception segments in the range long exceptionSegmentCount; while ( (exceptionSegmentCount = getExceptionSegmentCount( lastIndex, (result.millisecond / this.segmentSize) * this.segmentSize - 1) ) > 0) { lastIndex = result.segmentStart; // move forward exceptionSegmentCount segments skipping excluded segments for (int i = 0; i < exceptionSegmentCount; i++) { do { result.inc(); } while (result.inExcludeSegments()); } } lastIndex = result.segmentStart; // skip exception or excluded segments we may fall on while (result.inExceptionSegments() || result.inExcludeSegments()) { result.inc(); lastIndex += this.segmentSize; } lastIndex++; } return getTimeFromLong(result.millisecond); } /** * Converts a date/time value to take account of daylight savings time. * * @param date the milliseconds. * * @return The milliseconds. */ public long getTimeFromLong(long date) { long result = date; if (this.adjustForDaylightSaving) { this.workingCalendarNoDST.setTime(new Date(date)); this.workingCalendar.set( this.workingCalendarNoDST.get(Calendar.YEAR), this.workingCalendarNoDST.get(Calendar.MONTH), this.workingCalendarNoDST.get(Calendar.DATE), this.workingCalendarNoDST.get(Calendar.HOUR_OF_DAY), this.workingCalendarNoDST.get(Calendar.MINUTE), this.workingCalendarNoDST.get(Calendar.SECOND) ); this.workingCalendar.set( Calendar.MILLISECOND, this.workingCalendarNoDST.get(Calendar.MILLISECOND) ); result = this.workingCalendar.getTime().getTime(); } return result; } /** * Returns <code>true</code> if a value is contained in the timeline. * * @param millisecond the value to verify. * * @return <code>true</code> if value is contained in the timeline. */ public boolean containsDomainValue(long millisecond) { Segment segment = getSegment(millisecond); return segment.inIncludeSegments(); } /** * Returns true if a value is contained in the timeline. * @param date date to verify * @return true if value is contained in the timeline */ public boolean containsDomainValue(Date date) { return containsDomainValue(getTime(date)); } /** * Returns true if a range of values are contained in the timeline. This is * implemented verifying that all segments are in the range. * * @param domainValueStart start of the range to verify * @param domainValueEnd end of the range to verify * @return true if the range is contained in the timeline */ public boolean containsDomainRange(long domainValueStart, long domainValueEnd) { if (domainValueEnd < domainValueStart) { throw new IllegalArgumentException("domainValueEnd (" + domainValueEnd + ") < domainValueStart (" + domainValueStart + ")"); } Segment segment = getSegment(domainValueStart); boolean contains = true; do { contains = (segment.inIncludeSegments()); if (segment.contains(domainValueEnd)) { break; } else { segment.inc(); } } while (contains); return (contains); } /** * Returns true if a range of values are contained in the timeline. This is * implemented verifying that all segments are in the range. * * @param dateDomainValueStart start of the range to verify * @param dateDomainValueEnd end of the range to verify * @return true if the range is contained in the timeline */ public boolean containsDomainRange(Date dateDomainValueStart, Date dateDomainValueEnd) { return containsDomainRange(getTime(dateDomainValueStart), getTime(dateDomainValueEnd)); } /** * Adds a segment as an exception. An exception segment is defined as a segment * to exclude from what would otherwise be considered a valid segment of the timeline. * An exception segment can not be contained inside an already excluded segment. * If so, no action will occur (the proposed exception segment will be discarded). * <p> * The segment is identified by a domainValue into any part of the segment. * Therefore the segmentStart <= domainValue <= segmentEnd. * * @param millisecond domain value to treat as an exception */ public void addException(long millisecond) { addException(new Segment(millisecond)); } /** * Adds a segment range as an exception. An exception segment is defined as a segment * to exclude from what would otherwise be considered a valid segment of the timeline. * An exception segment can not be contained inside an already excluded segment. * If so, no action will occur (the proposed exception segment will be discarded). * <p> * The segment range is identified by a domainValue that begins a valid segment and ends * with a domainValue that ends a valid segment. Therefore the range will contain all * segments whose segmentStart <= domainValue and segmentEnd <= toDomainValue. * * @param fromDomainValue start of domain range to treat as an exception * @param toDomainValue end of domain range to treat as an exception */ public void addException(long fromDomainValue, long toDomainValue) { addException(new SegmentRange(fromDomainValue, toDomainValue)); } /** * Adds a segment as an exception. An exception segment is defined as a segment * to exclude from what would otherwise be considered a valid segment of the timeline. * An exception segment can not be contained inside an already excluded segment. * If so, no action will occur (the proposed exception segment will be discarded). * <p> * The segment is identified by a Date into any part of the segment. * * @param exceptionDate Date into the segment to exclude. */ public void addException(Date exceptionDate) { addException(getTime(exceptionDate)); //addException(exceptionDate.getTime()); } /** * Adds a list of dates as segment exceptions. Each exception segment is defined as a segment * to exclude from what would otherwise be considered a valid segment of the timeline. * An exception segment can not be contained inside an already excluded segment. * If so, no action will occur (the proposed exception segment will be discarded). * <p> * The segment is identified by a Date into any part of the segment. * * @param exceptionList List of Date objects that identify the segments to exclude. */ public void addExceptions(List exceptionList) { for (Iterator iter = exceptionList.iterator(); iter.hasNext();) { addException((Date) iter.next()); } } /** * Adds a segment as an exception. An exception segment is defined as a segment * to exclude from what would otherwise be considered a valid segment of the timeline. * An exception segment can not be contained inside an already excluded segment. * This is verified inside this method, and if so, no action will occur (the proposed * exception segment will be discarded). * * @param segment the segment to exclude. */ private void addException(Segment segment) { if (segment.inIncludeSegments()) { int p = binarySearchExceptionSegments(segment); this.exceptionSegments.add(-(p + 1), segment); } } /** * Adds a segment relative to the baseTimeline as an exception. Because a base segment is * normally larger than our segments, this may add one or more segment ranges to the * exception list. * <p> * An exception segment is defined as a segment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -