Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2006 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.model; |
12 |
|
13 |
import org.eclipse.jdt.core.ISourceRange; |
14 |
|
15 |
/** |
16 |
* @see ISourceRange |
17 |
* @since 3.6 |
18 |
*/ |
19 |
public class SourceRange implements ISourceRange { |
20 |
|
21 |
protected int offset, length; |
22 |
|
23 |
public SourceRange(int offset, int length) { |
24 |
this.offset = offset; |
25 |
this.length = length; |
26 |
} |
27 |
/* |
28 |
* @see Object#equals(Object) |
29 |
*/ |
30 |
public boolean equals(Object obj) { |
31 |
if (!(obj instanceof ISourceRange)) |
32 |
return false; |
33 |
ISourceRange sourceRange = (ISourceRange) obj; |
34 |
return sourceRange.getOffset() == this.offset && sourceRange.getLength() == this.length; |
35 |
} |
36 |
/** |
37 |
* @see ISourceRange |
38 |
*/ |
39 |
public int getLength() { |
40 |
return this.length; |
41 |
} |
42 |
/** |
43 |
* @see ISourceRange |
44 |
*/ |
45 |
public int getOffset() { |
46 |
return this.offset; |
47 |
} |
48 |
/* |
49 |
* @see Object#hashCode() |
50 |
*/ |
51 |
public int hashCode() { |
52 |
return this.length ^ this.offset; |
53 |
} |
54 |
|
55 |
/** |
56 |
* Helper method that answers whether a valid source range is available |
57 |
* in the given ISourceRange. When an element has no associated source |
58 |
* code, Java Model APIs may return either <code>null</code> or a range of |
59 |
* [-1, 0] to indicate an invalid range. This utility method can be used |
60 |
* to detect that case. |
61 |
* |
62 |
* @param range a source range, can be <code>null</code> |
63 |
* @return <code>true</code> iff range is not null and range.getOffset() is not -1 |
64 |
*/ |
65 |
public static boolean isAvailable(ISourceRange range) { |
66 |
return range != null && range.getOffset() != -1; |
67 |
} |
68 |
|
69 |
public String toString() { |
70 |
StringBuffer buffer = new StringBuffer(); |
71 |
buffer.append("[offset="); //$NON-NLS-1$ |
72 |
buffer.append(this.offset); |
73 |
buffer.append(", length="); //$NON-NLS-1$ |
74 |
buffer.append(this.length); |
75 |
buffer.append("]"); //$NON-NLS-1$ |
76 |
return buffer.toString(); |
77 |
} |
78 |
} |