📄 segmentedtimeline.java
字号:
}
}
/**
* Returns <code>true</code> if this segment is wholly before another
* segment.
*
* @param other the other segment.
*
* @return A boolean.
*/
public boolean before(Segment other) {
return (this.segmentEnd < other.getSegmentStart());
}
/**
* Returns <code>true</code> if this segment is wholly after another
* segment.
*
* @param other the other segment.
*
* @return A boolean.
*/
public boolean after(Segment other) {
return (this.segmentStart > other.getSegmentEnd());
}
/**
* Tests an object (usually another <code>Segment</code>) for equality
* with this segment.
*
* @param object The other segment to compare with us
*
* @return <code>true</code> if we are the same segment
*/
public boolean equals(Object object) {
if (object instanceof Segment) {
Segment other = (Segment) object;
return (this.segmentNumber == other.getSegmentNumber()
&& this.segmentStart == other.getSegmentStart()
&& this.segmentEnd == other.getSegmentEnd()
&& this.millisecond == other.getMillisecond());
}
else {
return false;
}
}
/**
* Returns a copy of ourselves or <code>null</code> if there was an
* exception during cloning.
*
* @return A copy of this segment.
*/
public Segment copy() {
try {
return (Segment) this.clone();
}
catch (CloneNotSupportedException e) {
return null;
}
}
/**
* Will compare this Segment with another Segment (from Comparable
* interface).
*
* @param object The other Segment to compare with
*
* @return -1: this < object, 0: this.equal(object) and
* +1: this > object
*/
public int compareTo(Object object) {
Segment other = (Segment) object;
if (this.before(other)) {
return -1;
}
else if (this.after(other)) {
return +1;
}
else {
return 0;
}
}
/**
* Returns true if we are an included segment and we are not an
* exception.
*
* @return <code>true</code> or <code>false</code>.
*/
public boolean inIncludeSegments() {
if (getSegmentNumberRelativeToGroup()
< SegmentedTimeline.this.segmentsIncluded) {
return !inExceptionSegments();
}
else {
return false;
}
}
/**
* Returns true if we are an excluded segment.
*
* @return <code>true</code> or <code>false</code>.
*/
public boolean inExcludeSegments() {
return getSegmentNumberRelativeToGroup()
>= SegmentedTimeline.this.segmentsIncluded;
}
/**
* Calculate the segment number relative to the segment group. This
* will be a number between 0 and segmentsGroup-1. This value is
* calculated from the segmentNumber. Special care is taken for
* negative segmentNumbers.
*
* @return The segment number.
*/
private long getSegmentNumberRelativeToGroup() {
long p = (this.segmentNumber
% SegmentedTimeline.this.groupSegmentCount);
if (p < 0) {
p += SegmentedTimeline.this.groupSegmentCount;
}
return p;
}
/**
* Returns true if we are an exception segment. This is implemented via
* a binary search on the exceptionSegments sorted list.
*
* If the segment is not listed as an exception in our list and we have
* a baseTimeline, a check is performed to see if the segment is inside
* an excluded segment from our base. If so, it is also considered an
* exception.
*
* @return <code>true</code> if we are an exception segment.
*/
public boolean inExceptionSegments() {
return binarySearchExceptionSegments(this) >= 0;
}
/**
* Increments the internal attributes of this segment by a number of
* segments.
*
* @param n Number of segments to increment.
*/
public void inc(long n) {
this.segmentNumber += n;
long m = n * SegmentedTimeline.this.segmentSize;
this.segmentStart += m;
this.segmentEnd += m;
this.millisecond += m;
}
/**
* Increments the internal attributes of this segment by one segment.
* The exact time incremented is segmentSize.
*/
public void inc() {
inc(1);
}
/**
* Decrements the internal attributes of this segment by a number of
* segments.
*
* @param n Number of segments to decrement.
*/
public void dec(long n) {
this.segmentNumber -= n;
long m = n * SegmentedTimeline.this.segmentSize;
this.segmentStart -= m;
this.segmentEnd -= m;
this.millisecond -= m;
}
/**
* Decrements the internal attributes of this segment by one segment.
* The exact time decremented is segmentSize.
*/
public void dec() {
dec(1);
}
/**
* Moves the index of this segment to the beginning if the segment.
*/
public void moveIndexToStart() {
this.millisecond = this.segmentStart;
}
/**
* Moves the index of this segment to the end of the segment.
*/
public void moveIndexToEnd() {
this.millisecond = this.segmentEnd;
}
}
/**
* Private internal class to represent a range of segments. This class is
* mainly used to store in one object a range of exception segments. This
* optimizes certain timelines that use a small segment size (like an
* intraday timeline) allowing them to express a day exception as one
* SegmentRange instead of multi Segments.
*/
protected class SegmentRange extends Segment {
/** The number of segments in the range. */
private long segmentCount;
/**
* Creates a SegmentRange between a start and end domain values.
*
* @param fromMillisecond start of the range
* @param toMillisecond end of the range
*/
public SegmentRange(long fromMillisecond, long toMillisecond) {
Segment start = getSegment(fromMillisecond);
Segment end = getSegment(toMillisecond);
// if (start.getSegmentStart() != fromMillisecond
// || end.getSegmentEnd() != toMillisecond) {
// throw new IllegalArgumentException("Invalid Segment Range ["
// + fromMillisecond + "," + toMillisecond + "]");
// }
this.millisecond = fromMillisecond;
this.segmentNumber = calculateSegmentNumber(fromMillisecond);
this.segmentStart = start.segmentStart;
this.segmentEnd = end.segmentEnd;
this.segmentCount
= (end.getSegmentNumber() - start.getSegmentNumber() + 1);
}
/**
* Returns the number of segments contained in this range.
*
* @return The segment count.
*/
public long getSegmentCount() {
return this.segmentCount;
}
/**
* Returns a segment that is the intersection of this segment and the
* interval.
*
* @param from the start of the interval.
* @param to the end of the interval.
*
* @return The intersection.
*/
public Segment intersect(long from, long to) {
// Segment fromSegment = getSegment(from);
// fromSegment.inc();
// Segment toSegment = getSegment(to);
// toSegment.dec();
long start = Math.max(from, this.segmentStart);
long end = Math.min(to, this.segmentEnd);
// long start = Math.max(
// fromSegment.getSegmentStart(), this.segmentStart
// );
// long end = Math.min(toSegment.getSegmentEnd(), this.segmentEnd);
if (start <= end) {
return new SegmentRange(start, end);
}
else {
return null;
}
}
/**
* Returns true if all Segments of this SegmentRenge are an included
* segment and are not an exception.
*
* @return <code>true</code> or </code>false</code>.
*/
public boolean inIncludeSegments() {
for (Segment segment = getSegment(this.segmentStart);
segment.getSegmentStart() < this.segmentEnd;
segment.inc()) {
if (!segment.inIncludeSegments()) {
return (false);
}
}
return true;
}
/**
* Returns true if we are an excluded segment.
*
* @return <code>true</code> or </code>false</code>.
*/
public boolean inExcludeSegments() {
for (Segment segment = getSegment(this.segmentStart);
segment.getSegmentStart() < this.segmentEnd;
segment.inc()) {
if (!segment.inExceptionSegments()) {
return (false);
}
}
return true;
}
/**
* Not implemented for SegmentRange. Always throws
* IllegalArgumentException.
*
* @param n Number of segments to increment.
*/
public void inc(long n) {
throw new IllegalArgumentException(
"Not implemented in SegmentRange"
);
}
}
/**
* Special <code>SegmentRange</code> that came from the BaseTimeline.
*/
protected class BaseTimelineSegmentRange extends SegmentRange {
/**
* Constructor.
*
* @param fromDomainValue the start value.
* @param toDomainValue the end value.
*/
public BaseTimelineSegmentRange(long fromDomainValue,
long toDomainValue) {
super(fromDomainValue, toDomainValue);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -