View Javadoc

1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
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  	 * {@inheritDoc}
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 	 * @return the end
102 	 */
103 	public int getEnd() {
104 		return myEnd;
105 	}
106 
107 	/**
108 	 * @return the start
109 	 */
110 	public int getStart() {
111 		return myStart;
112 	}
113 
114 	/**
115 	 * {@inheritDoc}
116 	 */
117 	@Override
118 	public int hashCode() {
119 		return myStart * myEnd;
120 	}
121 
122 	/**
123 	 * {@inheritDoc}
124 	 */
125 	@Override
126 	public String toString() {
127 		return "Range[" + myStart + ", " + myEnd + "]";
128 	}
129 
130 	public Range overlay(Range theRange) {
131 		return new Range(Math.min(myStart, theRange.getStart()), Math.max(myEnd, theRange.getEnd()));
132 	}
133 
134 }