comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/LineLocation.java @ 21987:b2d1c8ff592a

Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 01 Jul 2015 10:23:36 +0200
parents 9c8c0937da41
children 5573f12b94f8
comparison
equal deleted inserted replaced
21986:67ea94a23074 21987:b2d1c8ff592a
22 * or visit www.oracle.com if you need additional information or have any 22 * or visit www.oracle.com if you need additional information or have any
23 * questions. 23 * questions.
24 */ 24 */
25 package com.oracle.truffle.api.source; 25 package com.oracle.truffle.api.source;
26 26
27 import java.util.*;
28
29 /** 27 /**
30 * A specification for a location in guest language source, expressed as a line number in a specific 28 * A specification for a location in guest language source, expressed as a line number in a specific
31 * instance of {@link Source}, suitable for hash table keys with equality defined in terms of 29 * instance of {@link Source}, suitable for hash table keys with equality defined in terms of
32 * content. 30 * content.
33 */ 31 */
34 public interface LineLocation { 32 public final class LineLocation implements Comparable<LineLocation> {
33 private final Source source;
34 private final int line;
35 35
36 Source getSource(); 36 LineLocation(Source source, int line) {
37 assert source != null;
38 this.source = source;
39 this.line = line;
40 }
41
42 public Source getSource() {
43 return source;
44 }
37 45
38 /** 46 /**
39 * Gets the 1-based number of a line in the source. 47 * Gets the 1-based number of a line in the source.
48 *
49 * @return value from 1 to infinity
40 */ 50 */
41 int getLineNumber(); 51 public int getLineNumber() {
52 return line;
53 }
42 54
43 String getShortDescription(); 55 public String getShortDescription() {
56 return source.getShortName() + ":" + line;
57 }
44 58
45 /** 59 @Override
46 * Default comparator by (1) textual path name, (2) line number. 60 public String toString() {
47 */ 61 return "Line[" + getShortDescription() + "]";
48 Comparator<LineLocation> COMPARATOR = new Comparator<LineLocation>() { 62 }
49 63
50 public int compare(LineLocation l1, LineLocation l2) { 64 @Override
51 final int sourceResult = l1.getSource().getPath().compareTo(l2.getSource().getPath()); 65 public int hashCode() {
52 if (sourceResult != 0) { 66 final int prime = 31;
53 return sourceResult; 67 int result = 1;
54 } 68 result = prime * result + line;
55 return Integer.compare(l1.getLineNumber(), l2.getLineNumber()); 69 result = prime * result + source.getHashKey().hashCode();
70 return result;
71 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (this == obj) {
76 return true;
56 } 77 }
78 if (obj == null) {
79 return false;
80 }
81 if (!(obj instanceof LineLocation)) {
82 return false;
83 }
84 LineLocation other = (LineLocation) obj;
85 if (line != other.line) {
86 return false;
87 }
88 return source.getHashKey().equals(other.source.getHashKey());
89 }
57 90
58 }; 91 @Override
59 92 public int compareTo(LineLocation o) {
93 final int sourceResult = this.getSource().getPath().compareTo(o.getSource().getPath());
94 if (sourceResult != 0) {
95 return sourceResult;
96 }
97 return Integer.compare(this.getLineNumber(), o.getLineNumber());
98 }
60 } 99 }