Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2009 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core; |
12 |
|
13 |
|
14 |
/** |
15 |
* A source range defines an element's source coordinates relative to |
16 |
* its source buffer. |
17 |
* @see ISourceRange |
18 |
* @since 3.6 |
19 |
*/ |
20 |
public class SourceRange implements ISourceRange { |
21 |
|
22 |
private int offset, length; |
23 |
|
24 |
public SourceRange(int offset, int length) { |
25 |
this.offset = offset; |
26 |
this.length = length; |
27 |
} |
28 |
/* |
29 |
* @see Object#equals(Object) |
30 |
*/ |
31 |
public boolean equals(Object obj) { |
32 |
if (!(obj instanceof ISourceRange)) |
33 |
return false; |
34 |
ISourceRange sourceRange = (ISourceRange) obj; |
35 |
return sourceRange.getOffset() == this.offset && sourceRange.getLength() == this.length; |
36 |
} |
37 |
/** |
38 |
* @see ISourceRange |
39 |
*/ |
40 |
public int getLength() { |
41 |
return this.length; |
42 |
} |
43 |
/** |
44 |
* @see ISourceRange |
45 |
*/ |
46 |
public int getOffset() { |
47 |
return this.offset; |
48 |
} |
49 |
/* |
50 |
* @see Object#hashCode() |
51 |
*/ |
52 |
public int hashCode() { |
53 |
return this.length ^ this.offset; |
54 |
} |
55 |
|
56 |
/** |
57 |
* Helper method that answers whether a valid source range is available |
58 |
* in the given ISourceRange. When an element has no associated source |
59 |
* code, Java Model APIs may return either <code>null</code> or a range of |
60 |
* [-1, 0] to indicate an invalid range. This utility method can be used |
61 |
* to detect that case. |
62 |
* |
63 |
* @param range a source range, can be <code>null</code> |
64 |
* @return <code>true</code> iff range is not null and range.getOffset() is not -1 |
65 |
*/ |
66 |
public static boolean isAvailable(ISourceRange range) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=130161 |
67 |
return range != null && range.getOffset() != -1; |
68 |
} |
69 |
|
70 |
public String toString() { |
71 |
StringBuffer buffer = new StringBuffer(); |
72 |
buffer.append("[offset="); //$NON-NLS-1$ |
73 |
buffer.append(this.offset); |
74 |
buffer.append(", length="); //$NON-NLS-1$ |
75 |
buffer.append(this.length); |
76 |
buffer.append("]"); //$NON-NLS-1$ |
77 |
return buffer.toString(); |
78 |
} |
79 |
} |