View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
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 }