📄 segmentedtimeline.java
字号:
* @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() < segmentSize) {
throw new IllegalArgumentException("baseTimeline.getSegmentSize() is smaller "
+ "than segmentSize");
}
else if (baseTimeline.getStartTime() > startTime) {
throw new IllegalArgumentException("baseTimeline.getStartTime() is after than "
+ "startTime");
}
else if ((baseTimeline.getSegmentSize() % segmentSize) != 0) {
throw new IllegalArgumentException(
"baseTimeline.getSegmentSize() is not multiple of segmentSize");
}
else if (((startTime - baseTimeline.getStartTime()) % 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 - startTime;
long groupMilliseconds = rawMilliseconds % segmentsGroupSize;
long groupIndex = rawMilliseconds / segmentsGroupSize;
if (groupMilliseconds >= segmentsIncludedSize) {
result = toTimelineValue(startTime + segmentsGroupSize * (groupIndex + 1));
}
else {
Segment segment = getSegment(millisecond);
if (segment.inExceptionSegments()) {
result = toTimelineValue(segment.getSegmentEnd() + 1);
}
else {
long shiftedSegmentedValue = millisecond - startTime;
long x = shiftedSegmentedValue % segmentsGroupSize;
long y = shiftedSegmentedValue / segmentsGroupSize;
long wholeExceptionsBeforeDomainValue =
getExceptionSegmentCount(startTime, millisecond - 1);
// long partialTimeInException = 0;
// Segment ss = getSegment(millisecond);
// if (ss.inExceptionSegments()) {
// partialTimeInException = millisecond - ss.getSegmentStart();
// }
if (x < segmentsIncludedSize) {
result = segmentsIncludedSize * y + x - wholeExceptionsBeforeDomainValue * segmentSize;// - partialTimeInException;;
}
else {
result = segmentsIncludedSize * (y + 1)
- wholeExceptionsBeforeDomainValue * 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.
*/
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.
*
* @return The domain value.
*/
public long toMillisecond(long timelineValue) {
// calculate the result as if no exceptions
Segment result = new Segment(startTime + timelineValue
+ (timelineValue / segmentsIncludedSize) * segmentsExcludedSize);
long lastIndex = 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 - 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 += segmentSize;
}
lastIndex++;
}
return result.millisecond;
}
/**
* Returns <code>true</code> if a value is contained in the timeline.
*
* @param domainValue 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 domainValue 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);
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
* 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 baseTimeline segment.
*
* @param domainValue domain value to teat as a baseTimeline exception.
*/
public void addBaseTimelineException(long domainValue) {
Segment baseSegment = baseTimeline.getSegment(domainValue);
if (baseSegment.inIncludeSegments()) {
// cycle through all the segments contained in the BaseTimeline exception segment
Segment segment = getSegment(baseSegment.getSegmentStart());
while (segment.getSegmentStart() <= baseSegment.getSegmentEnd()) {
if (segment.inIncludeSegments()) {
// find all consecutive included segments
long fromDomainValue = segment.getSegmentStart();
long toDomainValue;
do {
toDomainValue = segment.getSegmentEnd();
segment.inc();
}
while (segment.inIncludeSegments());
// add the interval as an exception
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -