1
2
3
4
5
6
7
8
9
10
11
12
13 package sk.uniba.euromath.editor.textEditor;
14
15 /***
16 * Range of this interval is positive (not zero)
17 *
18 * @author Tomáš Studva 7.2.2006
19 * Martin Kollar
20 */
21 public class Interval {
22 /***
23 * Start of the interval.
24 */
25 private int start;
26
27 /***
28 * End of the interval.
29 */
30 private int end;
31
32 /***
33 * Contructor.
34 *
35 * @param start
36 * of interval, must be non-negative
37 * @param end
38 * of interval
39 */
40 public Interval(int start, int end) {
41 assert (start >= 0);
42 assert ( (end - start)>0 );
43 this.start = start;
44 this.end = end;
45 }
46
47 /***
48 * @return starting point
49 */
50 public int getStart(){
51 return start;
52 }
53
54 /***
55 * @return ending point
56 */
57 public int getEnd(){
58 return end;
59 }
60
61 /***
62 *
63 * @param value
64 * @return <code>true</code> if value is inside of this interval
65 */
66 public boolean contains(int value){
67 return ((start <= value) && (value <= end));
68 }
69
70 /***
71 *
72 * @param interval Interval to check
73 * @return <code> true </code> if <code> interval </code> is inside this interval
74 */
75 public boolean contains(Interval interval){
76 return ( contains(interval.getStart()) && contains(interval.getEnd()) );
77 }
78
79 /***
80 *
81 * @param interval Interval to check fror inersection
82 * @return <code> true </code> if their intersection is not empty or they are adjacent
83 */
84 public boolean intersects(Interval interval){
85 return ( contains(interval.getStart()) || contains(interval.getEnd()) || interval.contains(this) );
86 }
87
88 /***
89 * Intervals have to intersect
90 *
91 * @param interval1
92 * @param interval2
93 *
94 * @return logical OR of this intersecting intervals
95 */
96 public static Interval ORIntervals(Interval interval1, Interval interval2){
97 if (interval1.intersects(interval2)) {
98 int start;
99 if (interval1.getStart() <= interval2.getStart())
100 start = interval1.getStart();
101 else
102 start = interval2.getStart();
103
104 int end;
105 if (interval1.getEnd() >= interval2.getEnd())
106 end = interval1.getEnd();
107 else
108 end = interval2.getEnd();
109
110 return new Interval(start,end);
111 }
112 throw new IllegalArgumentException("Intervals do not intersect!");
113 }
114 }