ErrorCollector.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package de.jplag;
  2. import static de.jplag.options.Verbosity.LONG;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import de.jplag.options.JPlagOptions;
  6. import de.jplag.options.Verbosity;
  7. /**
  8. * Error collector class that collects errors but also allows printing the collected errors.
  9. * @author Timur Saglam
  10. */
  11. public class ErrorCollector implements ErrorConsumer { // TODO TS should be eventually replaced with a true logger/logging manager
  12. private final List<String> collectedErrors; // List of errors that occurred during the execution of the errorConsumer.
  13. private final JPlagOptions options;
  14. private String currentSubmissionName;
  15. public ErrorCollector(JPlagOptions options) {
  16. this.options = options;
  17. collectedErrors = new ArrayList<>();
  18. currentSubmissionName = "<Unknown submission>";
  19. }
  20. @Override
  21. public void addError(String errorMessage) {
  22. collectedErrors.add("[" + currentSubmissionName + "] " + errorMessage);
  23. print(null, "\t" + errorMessage);
  24. }
  25. @Override
  26. public void print(String message, String longMessage) {
  27. if (message == null && longMessage == null) {
  28. throw new IllegalArgumentException("At least one message parameter needs to be non-null!");
  29. }
  30. Verbosity verbosity = options.getVerbosity();
  31. if (message != null) {
  32. System.out.println(message);
  33. }
  34. if (longMessage != null && verbosity == LONG) {
  35. System.out.println(longMessage);
  36. }
  37. }
  38. /**
  39. * Print all collected errors messages in a list-like fashion.
  40. */
  41. public void printCollectedErrors() {
  42. StringBuilder errorReport = new StringBuilder();
  43. System.out.println("The following errors occured: ");
  44. for (String message : collectedErrors) {
  45. errorReport.append(message);
  46. errorReport.append('\n');
  47. }
  48. System.out.println(errorReport);
  49. }
  50. /**
  51. * Updates the name of the currently processed submission.
  52. * @param currentSubmissionName is the name.
  53. */
  54. public void setCurrentSubmissionName(String currentSubmissionName) {
  55. this.currentSubmissionName = currentSubmissionName;
  56. }
  57. /**
  58. * @return true if there is at least one error.
  59. */
  60. public boolean hasErrors() {
  61. return !collectedErrors.isEmpty();
  62. }
  63. }