| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableSet; |
| 20 |
import com.google.common.collect.Maps; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
|
| 23 |
import java.io.Serializable; |
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class DiagnosticGroup implements Serializable { |
| 34 |
private static final long serialVersionUID = 1; |
| 35 |
|
| 36 |
|
| 37 |
private final Set<DiagnosticType> types; |
| 38 |
|
| 39 |
|
| 40 |
private final String name; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
DiagnosticGroup(String name, DiagnosticType ...types) { |
| 46 |
this.name = name; |
| 47 |
this.types = ImmutableSet.copyOf(Arrays.asList(types)); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public DiagnosticGroup(DiagnosticType ...types) { |
| 54 |
this(null, types); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private DiagnosticGroup(DiagnosticType type) { |
| 61 |
this.name = null; |
| 62 |
this.types = ImmutableSet.of(type); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
private static final Map<DiagnosticType, DiagnosticGroup> singletons = |
| 67 |
Maps.newHashMap(); |
| 68 |
|
| 69 |
|
| 70 |
public static DiagnosticGroup forType(DiagnosticType type) { |
| 71 |
if (!singletons.containsKey(type)) { |
| 72 |
singletons.put(type, new DiagnosticGroup(type)); |
| 73 |
} |
| 74 |
return singletons.get(type); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public DiagnosticGroup(DiagnosticGroup ...groups) { |
| 81 |
this(null, groups); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public DiagnosticGroup(String name, DiagnosticGroup ...groups) { |
| 88 |
Set<DiagnosticType> set = Sets.newHashSet(); |
| 89 |
|
| 90 |
for (DiagnosticGroup group : groups) { |
| 91 |
set.addAll(group.types); |
| 92 |
} |
| 93 |
|
| 94 |
this.name = name; |
| 95 |
this.types = ImmutableSet.copyOf(set); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public boolean matches(JSError error) { |
| 103 |
return matches(error.getType()); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public boolean matches(DiagnosticType type) { |
| 110 |
return types.contains(type); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
boolean isSubGroup(DiagnosticGroup group) { |
| 117 |
for (DiagnosticType type : group.types) { |
| 118 |
if (!matches(type)) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
} |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public Iterable<DiagnosticType> getTypes() { |
| 129 |
return types; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public String toString() { |
| 134 |
return name == null ? super.toString() : "DiagnosticGroup<" + name + ">"; |
| 135 |
} |
| 136 |
} |