Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ubermq/chord/Interval.java


1   package com.ubermq.chord;
2   
3   public class Interval
4   {
5       private Interval()
6       {
7       }
8   
9       /**
10       * Tests whether x is contained in an arbitrary interval between
11       * a and b. The open parameters indicate if the interval endpoint
12       * is opened or closed. <P>
13       *
14       * An open interval is notated as (a, b). A closed interval is [a,b].
15       * Open intervals include the endpoint as specified (i.e. inclusive).
16       * Closed intervals do not.<P>
17       *
18       * @param x value to test
19       * @param a lower bound
20       * @param b upper bound
21       * @param openA whether the a endpoint is open - (
22       * @param openB whether the b endpoint is open - )
23       */
24      public static boolean elementOf(ChordIdentifier x,
25                                      ChordIdentifier a,
26                                      ChordIdentifier b,
27                                      boolean openA,
28                                      boolean openB)
29      {
30          // if a > b, this is still a valid case
31          // because our identifiers are laid out in a circle.
32          // in this case, we normalize
33          if (a.compareTo(b) > 0) {
34              return !elementOf(x, b, a, !openB, !openA);
35          } else {
36              int r1 = x.compareTo(a), r2 = x.compareTo(b);
37  
38              return (a.equals(b) && (openA || openB)) ||
39                  ( (openA && r1 == 0) ||
40                       r1 > 0) &&
41                  ( (openB && r2 == 0) ||
42                       r2 < 0);
43          }
44      }
45  }