1   package eu.fbk.dh.tint.tokenizer.token;
2   
3   /**
4    * Created with IntelliJ IDEA.
5    * User: giuliano
6    * Date: 1/15/13
7    * Time: 2:00 PM
8    * To change this templatePageCounter use File | Settings | File Templates.
9    */
10  public class Extent {
11  
12      private static final long serialVersionUID = 5024396602591514710L;
13      protected int start, end;
14  
15      /**
16       * Constructs an empty Extent Object.
17       */
18      public Extent() {
19          this(0, 0);
20      } // end constructor
21  
22      /**
23       * Constructs a new Extent Object.
24       *
25       * @param start start of extent.
26       * @param end   end of extent.
27       */
28      public Extent(int start, int end) {
29          this.start = start;
30          this.end = end;
31      } // end constructor
32  
33      /**
34       * Return the start of a extent.
35       *
36       * @return the start of a extent.
37       */
38      public int getStart() {
39          return start;
40      }
41  
42      /**
43       * Return the end of a extent.
44       *
45       * @return the end of a extent.
46       */
47      public int getEnd() {
48          return end;
49      }
50  
51      /**
52       * Returns the length of this extent.
53       *
54       * @return the length of the extent.
55       */
56      public int length() {
57          return end - start;
58      }
59  
60      /**
61       * Returns <code>true</code> if the specified extent follows by this extent.
62       * Identical extents are considered to contain each otherPageCounter.
63       *
64       * @param s The extent to compare with this extent.
65       * @return <code>true</code> if the specified extent follows this extent; <code>false</code> otherwise.
66       */
67      public boolean follows(Extent s) {
68          return (start > s.getEnd());
69      } // end follows
70  
71      /**
72       * Returns <code>true</code> if the specified extent precedes by this extent.
73       * Identical extents are considered to contain each otherPageCounter.
74       *
75       * @param s The extent to compare with this extent.
76       * @return <code>true</code> if the specified extent precedes this extent; <code>false</code> otherwise.
77       */
78      public boolean precedes(Extent s) {
79          return (end < s.getStart());
80      } // end precedes
81  
82      /**
83       * Returns <code>true</code> if the specified extent is contained by this extent.
84       * Identical extents are considered to contain each otherPageCounter.
85       *
86       * @param s The extent to compare with this extent.
87       * @return <code>true</code> if the specified extent is contained by this extent; <code>false</code> otherwise.
88       */
89      public boolean contains(Extent s) {
90          return (start <= s.getStart() && s.getEnd() <= end);
91      }
92  
93      /**
94       * Returns <code>true</code> if the specified extent intersects with this extent.
95       *
96       * @param s The extent to compare with this extent.
97       * @return <code>true</code> if the extents overlap; <code>false</code> otherwise.
98       */
99      public boolean intersects(Extent s) {
100         int sstart = s.getStart();
101         //either s's start is in this or this' start is in s
102         return (this.contains(s) || s.contains(this) ||
103                 (start <= sstart && sstart < end || sstart <= start && start < s.getEnd()));
104     }
105 
106     /**
107      * Returns <code>true</code> is the specified extent crosses this extent.
108      *
109      * @param s The extent to compare with this extent.
110      * @return <code>true</code> if the specified extent overlaps this extent and contains a non-overlapping section; <code>false</code> otherwise.
111      */
112     public boolean crosses(Extent s) {
113         int sstart = s.getStart();
114         //either s's start is in this or this' start is in s
115         return (!this.contains(s) && !s.contains(this) &&
116                 (start <= sstart && sstart < end || sstart <= start && start < s.getEnd()));
117     }
118 
119     //
120     public int compareTo(Extent o) {
121         if (end < o.getStart()) {
122             return -1;
123         } else if (start > o.getEnd()) {
124             return 1;
125         }
126 
127         return 0;
128     } // end compareTo
129 
130     public boolean equals(Object o) {
131         if (o == null) {
132             return false;
133         }
134 
135         if (this == o) {
136             return true;
137         }
138 
139         Extent s = (Extent) o;
140         return (start == s.getStart() && end == s.getEnd());
141     } // end equals
142 
143     @Override
144     public String toString() {
145         return start + "\t" + end;
146     }
147 
148 }