View Javadoc

1   /*
2    * File:    Interval.java
3    * Created: 13.10.2006 8:52:33 
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  package net.sf.webmancer.util.integerset;
22  
23  public class Interval {
24      public enum RelativePosition {
25          LOWER, LEFT_OUTSIDE, INSIDE, EQUAL, RIGHT_OUTSIDE, GREATER, OVER
26      }
27  
28      private int from;
29  
30      private int to;
31  
32      public Interval(final int from, final int to) {
33          this.from = from;
34          this.to = to;
35      }
36  
37      /**
38       * @return the from
39       */
40      public int getFrom() {
41          return this.from;
42      }
43  
44      /**
45       * @return the to
46       */
47      public int getTo() {
48          return this.to;
49      }
50  
51      /**
52       * @param integer
53       * @return
54       */
55      public boolean contains(final int integer) {
56          return (this.from <= integer) && (integer <= this.to);
57      }
58  
59      /**
60       * @param interval
61       * @return
62       */
63      public RelativePosition getRelativePositionTo(final Interval interval) {
64          if (this.to < interval.getFrom()) {
65              return RelativePosition.LOWER;
66  
67          } else if (this.from > interval.getTo()) {
68              return RelativePosition.GREATER;
69  
70          } else if (this.from == interval.getFrom() && this.to == interval.getTo()) {
71              return RelativePosition.EQUAL;
72  
73          } else if (this.from < interval.getFrom()) {
74              // LEFT_OUTSIDE or OVER
75              if (this.to <= interval.getTo()) {
76                  return RelativePosition.LEFT_OUTSIDE;
77              } else {
78                  return RelativePosition.OVER;
79              }
80  
81          } else {
82              // INSIDE or RIGHT_OUTSIDE
83              if (this.to <= interval.getTo()) {
84                  return RelativePosition.INSIDE;
85              } else {
86                  return RelativePosition.RIGHT_OUTSIDE;
87              }
88          }
89      }
90  
91  }