📄 segmentedtimeline.java
字号:
/**
* Returns the number of segments excluded per segment group.
*
* @return The number of segments excluded.
*/
public int getSegmentsExcluded() {
return (segmentsExcluded);
}
/**
* Returns the size in ms of the segments excluded per segment group.
*
* @return The size in milliseconds.
*/
public long getSegmentsExcludedSize() {
return (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 getSegmentsGroup() {
return (segmentsGroup);
}
/**
* Returns the size in ms 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 (segmentsGroupSize);
}
/**
* Returns the number of segments incluced per segment group.
*
* @return The number of segments.
*/
public int getSegmentsIncluded() {
return (segmentsIncluded);
}
/**
* Returns the size in ms of the segments included per segment group.
*
* @return The segment size in milliseconds.
*/
public long getSegmentsIncludedSize() {
return (segmentsIncludedSize);
}
/**
* Returns the size of one segment in ms.
*
* @return The segment size in milliseconds.
*/
public long getSegmentSize() {
return (segmentSize);
}
/**
* Returns a list of all the exception segments. This list should not be
* modified directly.
*
* @return The exception segments.
*/
public ArrayList getExceptionSegments() {
return (exceptionSegments);
}
/**
* Sets the exception segments list.
*
* @param exceptionSegments the exception segments.
*/
public void setExceptionSegments(ArrayList exceptionSegments) {
this.exceptionSegments = exceptionSegments;
}
/**
* Returns our baseTimeline, or null if none.
*
* @return The base timeline.
*/
public SegmentedTimeline getBaseTimeline() {
return (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 domainValue Value relative to the domain.
*
* @return The timeline value.
*/
public long toTimelineValue(long domainValue) {
long shiftedSegmentedValue = domainValue - startTime;
long x = shiftedSegmentedValue % segmentsGroupSize;
long y = shiftedSegmentedValue / segmentsGroupSize;
long wholeExceptionsBeforeDomainValue =
getExceptionSegmentCount(startTime, domainValue - 1);
long partialTimeInException = 0;
Segment segment = getSegment(domainValue);
if (segment.inExceptionSegments()) {
partialTimeInException = domainValue - segment.getSegmentStart();
}
long value = (x < segmentsIncludedSize ? segmentsIncludedSize * y + x
: segmentsIncludedSize * (y + 1))
- wholeExceptionsBeforeDomainValue * segmentSize - partialTimeInException;
return (value);
}
/**
* 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 dateDomainValue date relative to the domain.
*
* @return The timeline value.
*/
public long toTimelineValue(Date dateDomainValue) {
return (toTimelineValue(getTime(dateDomainValue)));
}
/**
* Translates a value relative to the timeline into a domain value.
*
* @param timelineValue the timeline value.
*
* @return The domain value.
*/
public long toDomainValue(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.index - 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.index);
}
/**
* Returns true if a value is contained in the timeline.
* @param domainValue value to verify
* @return true if value is contained in the timeline
*/
public boolean containsDomainValue(long domainValue) {
Segment segment = getSegment(domainValue);
return (segment.inIncludeSegments());
}
/**
* Returns true if a value is contained in the timeline.
* @param dateDomainValue date to verify
* @return true if value is contained in the timeline
*/
public boolean containsDomainValue(Date dateDomainValue) {
return (containsDomainValue(getTime(dateDomainValue)));
}
/**
* 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 occure (the proposed exception segment will be discarted).
* <p>
* The segment is identified by a domainValue into any part of the segment.
* Therefore the segmentStart <= domainValue <= segmentEnd.
*
* @param domainValue domain value to teat as an exception
*/
public void addException(long domainValue) {
addException(new Segment(domainValue));
}
/**
* 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 occure (the proposed exception segment will be discarted).
* <p>
* The segment range is identified by the domainValue that begins a valid segment and ends
* with the 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 teat as an exception
* @param toDomainValue end of domain range to teat 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 occure (the proposed exception segment will be discarted).<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));
}
/**
* Adds a list of dates as segment exceptions. Each exception segment is definied 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 occure (the proposed exception segment will be discarted).<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 occure (the proposed
* exception segment will be discarted).
*
* @param segment 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 occure (the proposed exception segment will be discarted).
* <p>
* The segment is identified by a domainValue into any part of the baseTimeline segment.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -