001 /*--------------------------------------------------------------------------+
002 $Id: Region.java 26268 2010-02-18 10:44:30Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.commons.region;
019
020 /**
021 * Regions represent intervals. Both the start and the end position are
022 * considered to be part of the region. Regions can i.e. be used to represent
023 * fragments of files.
024 * <p>
025 * This class is immutable.
026 *
027 * @author Elmar Juergens
028 * @author $Author: juergens $
029 *
030 * @version $Revision: 26268 $
031 * @levd.rating GREEN Hash: 7B5A6EE152CFF787B1C23A03B25BD6AE
032 */
033 public final class Region implements Comparable<Region> {
034
035 /** Name that is used if region is created without name */
036 public static final String UNKNOWN_ORIGIN = "Unkwnon origin";
037
038 /**
039 * Origin of the region. Can be used to store information about who created
040 * the region.
041 */
042 private final String origin;
043
044 /** Region start position */
045 private final int start;
046
047 /** Region end position. */
048 private final int end;
049
050 /**
051 * Creates a region with an origin
052 *
053 * @param start
054 * Start position of the region
055 * @param end
056 * End position of the region
057 * @param origin
058 * Region origin. (i.e. region producer)
059 */
060 public Region(int start, int end, String origin) {
061 this.start = start;
062 this.end = end;
063 this.origin = origin;
064 }
065
066 /**
067 * Creates a region with an unknown origin
068 *
069 * @param start
070 * Start position of the region
071 * @param end
072 * End position of the region
073 */
074 public Region(int start, int end) {
075 this(start, end, UNKNOWN_ORIGIN);
076 }
077
078 /** Checks if the region contains a position */
079 public boolean containsPosition(int position) {
080 return (start <= position && end >= position);
081 }
082
083 /** Checks if two regions are overlapping */
084 public boolean overlaps(Region r) {
085 // Region with smaller start value performs overlap check
086 if (r.start < start) {
087 return r.overlaps(this);
088 }
089
090 return (start <= r.start && end >= r.start);
091 }
092
093 /** Checks if two regions are adjacent */
094 public boolean adjacent(Region r) {
095 // Region with smaller start value performs adjacency check
096 if (r.start < start) {
097 return r.adjacent(this);
098 }
099
100 return (end + 1 == r.start);
101 }
102
103 /** Get origin. */
104 public String getOrigin() {
105 return origin;
106 }
107
108 /** Gets the end position of the region */
109 public int getEnd() {
110 return end;
111 }
112
113 /** Gets the start position of the region */
114 public int getStart() {
115 return start;
116 }
117
118 /** Gets the length of the region */
119 public int getLength() {
120 return end - start + 1;
121 }
122
123 /** {@inheritDoc} */
124 @Override
125 public String toString() {
126 return "[" + start + "-" + end + "]";
127 }
128
129 /** Compares regions by their start position */
130 public int compareTo(Region compareTo) {
131 return new Integer(start).compareTo(new Integer(compareTo.start));
132 }
133
134 }