View Javadoc
1   /*
2    * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   *
23   */
24  
25  package sun.jvm.hotspot.utilities;
26  
27  /** An interval is an immutable data structure defined by its two
28      endpoints. */
29  
30  import java.util.Comparator;
31  
32  public class Interval {
33    private Object lowEndpoint;
34    private Object highEndpoint;
35  
36    /** It is required that the low endpoint be less than or equal to
37        the high endpoint according to the Comparator which will be
38        passed into the overlaps() routines. */
39    public Interval(Object lowEndpoint, Object highEndpoint) {
40      this.lowEndpoint = lowEndpoint;
41      this.highEndpoint = highEndpoint;
42    }
43  
44    public Object getLowEndpoint() {
45      return lowEndpoint;
46    }
47  
48    public Object getHighEndpoint() {
49      return highEndpoint;
50    }
51  
52    /** This takes the Interval to compare against as well as a
53        Comparator which will be applied to the low and high endpoints
54        of the given intervals. */
55    public boolean overlaps(Interval arg, Comparator endpointComparator) {
56      return overlaps(arg.getLowEndpoint(), arg.getHighEndpoint(), endpointComparator);
57    }
58  
59    /** Routine which can be used instead of the one taking an interval,
60        for the situation where the endpoints are being retrieved from
61        different data structures */
62    public boolean overlaps(Object otherLowEndpoint,
63                            Object otherHighEndpoint,
64                            Comparator endpointComparator) {
65      if (endpointComparator.compare(highEndpoint, otherLowEndpoint) <= 0) {
66        return false;
67      }
68      if (endpointComparator.compare(lowEndpoint, otherHighEndpoint) >= 0) {
69        return false;
70      }
71      return true;
72    }
73  
74    public String toString() {
75      return "[ " + getLowEndpoint().toString() + ", " + getHighEndpoint().toString() + ")";
76    }
77  }