1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package ca.uhn.hl7v2.testpanel.util;
27
28 import org.apache.commons.lang.Validate;
29
30 public class Range {
31
32 private final int myEnd;
33 private final int myStart;
34
35 public Range(int theStartAndEnd) {
36 this(theStartAndEnd, theStartAndEnd);
37 }
38
39 public Range(int theStart, int theEnd) {
40 Validate.isTrue(theStart >= 0, "theStart: " + theStart);
41 Validate.isTrue(theEnd >= 0, "theEnd: " + theStart);
42
43 if (theStart > theEnd) {
44 myEnd = theStart;
45 myStart = theEnd;
46 } else {
47 myStart = theStart;
48 myEnd = theEnd;
49 }
50 }
51
52 public Range add(int theAmount) {
53 return new Range(myStart + theAmount, myEnd + theAmount);
54 }
55
56 public Range addToBoth(int theDelta) {
57 return new Range(myStart + theDelta, myEnd + theDelta);
58 }
59
60 public Range addToEnd(int theDelta) {
61 return new Range(myStart, myEnd + theDelta);
62 }
63
64 public String applyTo(String theMessage) {
65 int end = myEnd;
66 if (theMessage.length() < myEnd) {
67 end = theMessage.length();
68 }
69 return theMessage.substring(myStart, end);
70 }
71
72 public boolean contains(int theDot) {
73 return myStart <= theDot && myEnd >= theDot;
74 }
75
76
77
78
79 @Override
80 public boolean equals(Object theObj) {
81 if (theObj == null || (!(theObj instanceof Range))) {
82 return false;
83 }
84
85 Range o = (Range) theObj;
86 if (myStart != o.myStart) {
87 return false;
88 }
89 if (myEnd != o.myEnd) {
90 return false;
91 }
92
93 return true;
94 }
95
96 public int getDelta() {
97 return myEnd - myStart;
98 }
99
100
101
102
103 public int getEnd() {
104 return myEnd;
105 }
106
107
108
109
110 public int getStart() {
111 return myStart;
112 }
113
114
115
116
117 @Override
118 public int hashCode() {
119 return myStart * myEnd;
120 }
121
122
123
124
125 @Override
126 public String toString() {
127 return "Range[" + myStart + ", " + myEnd + "]";
128 }
129
130 public Rangef="../../../../../ca/uhn/hl7v2/testpanel/util/Range.html#Range">Range overlay(Range theRange) {
131 return new Range(Math.min(myStart, theRange.getStart()), Math.max(myEnd, theRange.getEnd()));
132 }
133
134 }