]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/CompilationError.java
(refs #7386) Minor SCL tools improvements
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / errors / CompilationError.java
1 package org.simantics.scl.compiler.errors;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
6 public class CompilationError implements Comparable<CompilationError> {
7     
8     public static final CompilationError[] EMPTY_ARRAY = new CompilationError[0];
9     
10     public final long location;
11     public final String description;
12     public final ErrorSeverity severity;
13     
14     public CompilationError(long location, String description, ErrorSeverity severity) {
15         if(description == null)
16             throw new NullPointerException();
17         this.location = location;
18         this.description = description;
19         this.severity = severity;
20     }
21     
22     public CompilationError(long location, String description) {
23         this(location, description, ErrorSeverity.ERROR);
24     }           
25     
26     public CompilationError(long location, Exception exception) {
27         this(location, exceptionToString(exception));
28     }
29     
30     public CompilationError(String description) {
31         this(Locations.NO_LOCATION, description);
32     }
33     
34     public CompilationError(Exception exception) {
35         this(Locations.NO_LOCATION, exception);
36     }
37     
38     private static String exceptionToString(Exception e) {
39         StringWriter w = new StringWriter();
40         e.printStackTrace(new PrintWriter(w));
41         return w.toString();
42     }
43    
44     @Override
45     public int hashCode() {
46         final int prime = 31;
47         int result = 1;
48         result = prime * result
49                 + ((description == null) ? 0 : description.hashCode());
50         result = prime * result + (int) (location ^ (location >>> 32));
51         return result;
52     }
53
54     @Override
55     public boolean equals(Object obj) {
56         if (this == obj)
57             return true;
58         if (obj == null)
59             return false;
60         if (getClass() != obj.getClass())
61             return false;
62         CompilationError other = (CompilationError) obj;
63         if (description == null) {
64             if (other.description != null)
65                 return false;
66         } else if (!description.equals(other.description))
67             return false;
68         if (location != other.location)
69             return false;
70         return true;
71     }
72
73     @Override
74     public int compareTo(CompilationError o) {
75         if(location < o.location)
76             return -1;
77         if(location > o.location)
78             return 1;
79         return description.compareTo(o.description);
80     }
81     
82     @Override
83     public String toString() {
84         return new StringBuilder().append("CompilationError: \"").append(description).append("\" at location ").append(location).toString();
85     }
86 }