001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.php.editor.completion;
043:
044: /**
045: * Container to store a pair of offsets related to a document.
046: * A pair of offsets is able to describe a part of the text into the document,
047: * e.g. selected text, token location and so on.
048: *
049: * @author Victor G. Vasilyev
050: */
051: public class OffsetPair {
052:
053: private int start;
054: private int end;
055:
056: /**
057: * Creates <code>OffsetPair</code> instance and sort the specified
058: * offsets so that getStartOffset() <= getEndOffset().
059: *
060: * @param startOffset the offset from the beginning of the document
061: * @param endOffset the offset from the beginning of the document
062: */
063: public OffsetPair(int startOffset, int endOffset) {
064: if (endOffset > startOffset) {
065: this .start = startOffset;
066: this .end = endOffset;
067: } else {
068: this .start = endOffset;
069: this .end = startOffset;
070: }
071: }
072:
073: /**
074: * Fetches the offset from the beginning of the document that this
075: * selection begins at.
076: *
077: * @return the starting offset >= 0 and <= getEndOffset()
078: */
079: public int getStartOffset() {
080: return start;
081: }
082:
083: /**
084: * Fetches the offset from the beginning of the document that this
085: * selection ends at.
086: *
087: * @return the ending offset <= getStartOffset()
088: */
089: public int getEndOffset() {
090: return end;
091: }
092:
093: /**
094: * Returns <code>true</code> if both offsets are valid, otherwise
095: * <code>false</code>.
096: *
097: * @return <code>true</code> if both getStartOffset() and getEndOffset()
098: * > 0, otherwise <code>false</code>.
099: */
100: public boolean isValid() {
101: return start >= 0 && end >= 0;
102: }
103:
104: /**
105: * Returns length of the entity (e.g. text) that is specified by the
106: * underlying <code>OffsetPair</code>.
107: *
108: * @return difference between <code>getEndOffset()</code> and
109: * <code>getStartOffset()</code>.
110: */
111: public int length() {
112: return end - start;
113: }
114:
115: @Override
116: public String toString() {
117: StringBuilder sb = new StringBuilder();
118: sb.append(isValid() ? "Valid" : "Invalid");
119: sb.append(" ");
120: sb.append(OffsetPair.class.getName());
121: sb.append(" [");
122: sb.append("StartOffset=");
123: sb.append(getStartOffset());
124: sb.append(";");
125: sb.append("EndOffset=");
126: sb.append(getEndOffset());
127: sb.append(";]");
128: return sb.toString();
129: }
130:
131: }
|