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