Splitting PrintingMessageCollector into Grouping and Printing collectors

This commit is contained in:
Andrey Breslav
2013-02-13 15:02:05 +04:00
parent 1f8d42ab49
commit 33fa065162
7 changed files with 135 additions and 36 deletions
@@ -51,4 +51,26 @@ public class CompilerMessageLocation {
public int getColumn() {
return column;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompilerMessageLocation location = (CompilerMessageLocation) o;
if (column != location.column) return false;
if (line != location.line) return false;
if (path != null ? !path.equals(location.path) : location.path != null) return false;
return true;
}
@Override
public int hashCode() {
int result = path != null ? path.hashCode() : 0;
result = 31 * result + line;
result = 31 * result + column;
return result;
}
}
@@ -27,4 +27,5 @@ public enum CompilerMessageSeverity {
OUTPUT;
public static final EnumSet<CompilerMessageSeverity> ERRORS = EnumSet.of(ERROR, EXCEPTION);
public static final EnumSet<CompilerMessageSeverity> VERBOSE = EnumSet.of(OUTPUT, LOGGING);
}
@@ -0,0 +1,93 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cli.common.messages;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public class GroupingMessageCollector implements MessageCollector {
private final MessageCollector delegate;
// File path (nullable) -> message
private final Multimap<String, Message> groupedMessages = LinkedHashMultimap.create();
public GroupingMessageCollector(@NotNull MessageCollector delegate) {
this.delegate = delegate;
}
@Override
public void report(
@NotNull CompilerMessageSeverity severity,
@NotNull String message,
@NotNull CompilerMessageLocation location
) {
if (CompilerMessageSeverity.VERBOSE.contains(severity)) {
delegate.report(severity, message, location);
}
else {
groupedMessages.put(location.getPath(), new Message(severity, message, location));
}
}
public void flush() {
for (String path : groupedMessages.keySet()) {
Collection<Message> messages = groupedMessages.get(path);
for (Message message : messages) {
delegate.report(message.severity, message.message, message.location);
}
}
groupedMessages.clear();
}
private static class Message {
private final CompilerMessageSeverity severity;
private final String message;
private final CompilerMessageLocation location;
private Message(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
this.severity = severity;
this.message = message;
this.location = location;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Message message1 = (Message) o;
if (!location.equals(message1.location)) return false;
if (!message.equals(message1.message)) return false;
if (severity != message1.severity) return false;
return true;
}
@Override
public int hashCode() {
int result = severity.hashCode();
result = 31 * result + message.hashCode();
result = 31 * result + location.hashCode();
return result;
}
}
}
@@ -16,12 +16,12 @@
package org.jetbrains.jet.cli.common.messages;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.jetbrains.annotations.NotNull;
import java.io.PrintStream;
import java.util.Collection;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.LOGGING;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.OUTPUT;
public class PrintingMessageCollector implements MessageCollector {
@@ -29,9 +29,6 @@ public class PrintingMessageCollector implements MessageCollector {
private final PrintStream errStream;
private final MessageRenderer messageRenderer;
// File path (nullable) -> error message
private final Multimap<String, String> groupedMessages = LinkedHashMultimap.create();
public PrintingMessageCollector(
@NotNull PrintStream errStream,
@NotNull MessageRenderer messageRenderer,
@@ -48,27 +45,9 @@ public class PrintingMessageCollector implements MessageCollector {
@NotNull String message,
@NotNull CompilerMessageLocation location
) {
boolean verboseMessage = severity == LOGGING || severity == OUTPUT;
if (!verbose && verboseMessage) return;
String text = messageRenderer.render(severity, message, location);
if (severity == CompilerMessageSeverity.LOGGING || severity == CompilerMessageSeverity.OUTPUT) {
if (!verbose) {
return;
}
errStream.println(text);
}
else {
groupedMessages.put(location.getPath(), text);
}
}
public void printToErrStream() {
if (!groupedMessages.isEmpty()) {
for (String path : groupedMessages.keySet()) {
Collection<String> messageTexts = groupedMessages.get(path);
for (String text : messageTexts) {
errStream.println(text);
}
}
}
errStream.println(messageRenderer.render(severity, message, location));
}
}