comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/LineLocationToProbeCollectionMap.java @ 16961:a1427e40deaf

Truffle/Instrumentation: some Javadoc revistions; minor code cleanups; remove one redundant operation; add tracing to the LineLocation maps.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 26 Aug 2014 13:54:53 -0700
parents f0e3b50c29c8
children 7b2e6171f455
comparison
equal deleted inserted replaced
16955:7ef0a2355540 16961:a1427e40deaf
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.instrument.impl; 25 package com.oracle.truffle.api.instrument.impl;
26 26
27 import java.io.*;
27 import java.util.*; 28 import java.util.*;
28 29
29 import com.oracle.truffle.api.instrument.*; 30 import com.oracle.truffle.api.instrument.*;
30 import com.oracle.truffle.api.source.*; 31 import com.oracle.truffle.api.source.*;
31 32
33 * A mapping from {@link LineLocation} (a line number in a specific piece of {@link Source} code) to 34 * A mapping from {@link LineLocation} (a line number in a specific piece of {@link Source} code) to
34 * a collection of {@link Probe}s whose associated {@link SourceSection} starts on that line. 35 * a collection of {@link Probe}s whose associated {@link SourceSection} starts on that line.
35 */ 36 */
36 public class LineLocationToProbeCollectionMap implements ProbeListener { 37 public class LineLocationToProbeCollectionMap implements ProbeListener {
37 38
39 private static final boolean TRACE = false;
40 private static final PrintStream OUT = System.out;
41
38 /** 42 /**
39 * Map: Source line ==> probes associated with source sections starting on the line. 43 * Map: Source line ==> probes associated with source sections starting on the line.
40 */ 44 */
41 private final Map<LineLocation, Collection<Probe>> lineToProbesMap = new HashMap<>(); 45 private final Map<LineLocation, Collection<Probe>> lineToProbesMap = new HashMap<>();
42 46
43 public LineLocationToProbeCollectionMap() { 47 public LineLocationToProbeCollectionMap() {
44 } 48 }
45 49
46 public void newProbeInserted(SourceSection source, Probe probe) { 50 public void newProbeInserted(SourceSection source, Probe probe) {
47 if (source != null && !(source instanceof NullSourceSection)) 51 if (source != null && !(source instanceof NullSourceSection)) {
48 this.addProbeToLine(source.getLineLocation(), probe); 52 final LineLocation lineLocation = source.getLineLocation();
53 if (TRACE) {
54 OUT.println("LineLocationToProbeCollectionMap: adding " + lineLocation + " Probe=" + probe);
55 }
56 this.addProbeToLine(lineLocation, probe);
57 }
49 } 58 }
50 59
51 public void probeTaggedAs(Probe probe, SyntaxTag tag) { 60 public void probeTaggedAs(Probe probe, SyntaxTag tag) {
52 // This map ignores tags 61 // This map ignores tags
53 } 62 }
96 existingProbeList.add(probe); 105 existingProbeList.add(probe);
97 } 106 }
98 } 107 }
99 108
100 /** 109 /**
101 * 110 *
102 * Returns a collection of {@link Probe}s whose associated source begins at the given 111 * Returns a collection of {@link Probe}s whose associated source begins at the given
103 * {@link LineLocation}. If there are no probes at that line, an empty list is returned. 112 * {@link LineLocation}, an empty list if none.
104 * 113 *
105 * @param line The line to check. 114 * @param line The line to check.
106 * @return A collection of probes at the given line. 115 * @return A collection of probes at the given line.
107 */ 116 */
108 public Collection<Probe> getProbesAtLine(LineLocation line) { 117 public Collection<Probe> getProbesAtLine(LineLocation line) {
109 Collection<Probe> probeList = lineToProbesMap.get(line); 118 Collection<Probe> probeList = lineToProbesMap.get(line);
110 119
111 if (probeList == null) 120 if (probeList == null) {
112 probeList = new ArrayList<>(1); 121 return Collections.emptyList();
113 122 }
114 return probeList; 123 return probeList;
115 } 124 }
116 125
117 /** 126 /**
118 * Convenience method to get probes according to a int line number. Returns a collection of 127 * Convenience method to get probes according to a int line number. Returns a collection of
119 * {@link Probe}s at the given line number. If there are no probes at that line, a new empty 128 * {@link Probe}s at the given line number, an empty list if none.
120 * list is returned.
121 * 129 *
122 * @param lineNumber The line number to check. 130 * @param lineNumber The line number to check.
123 * @return A iterable collection of probes at the given line. 131 * @return A collection of probes at the given line.
124 */ 132 */
125 public Collection<Probe> getProbesAtLineNumber(int lineNumber) { 133 public Collection<Probe> getProbesAtLineNumber(int lineNumber) {
134
135 final Set<LineLocation> keySet = lineToProbesMap.keySet();
136 if (keySet.size() == 0) {
137 return Collections.emptyList();
138 }
139
126 ArrayList<Probe> probes = new ArrayList<>(); 140 ArrayList<Probe> probes = new ArrayList<>();
127 141 for (LineLocation line : keySet) {
128 for (LineLocation line : lineToProbesMap.keySet()) {
129 if (line.getLineNumber() == lineNumber) 142 if (line.getLineNumber() == lineNumber)
130 probes.addAll(lineToProbesMap.get(line)); 143 probes.addAll(lineToProbesMap.get(line));
131 } 144 }
132 145
133 return probes; 146 return probes;