Debian Codegraph @

src/com/google/javascript/jscomp/DiagnosticGroup.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Group a set of related diagnostic types together, so that they can
30 * be toggled on and off as one unit.
31 * @author [email protected] (Nick Santos)
32 */
33 public class DiagnosticGroup implements Serializable {
34 private static final long serialVersionUID = 1;
35
36 // The set of types represented by this group, hashed by key.
37 private final Set<DiagnosticType> types;
38
39 // A human-readable name for the group.
40 private final String name;
41
42 /**
43 * Create a group that matches all errors of the given types.
44 */
45 DiagnosticGroup(String name, DiagnosticType ...types) {
46 this.name = name;
47 this.types = ImmutableSet.copyOf(Arrays.asList(types));
48 }
49
50 /**
51 * Create a group that matches all errors of the given types.
52 */
53 public DiagnosticGroup(DiagnosticType ...types) {
54 this(null, types);
55 }
56
57 /**
58 * Create a diagnostic group with no name that only matches the given type.
59 */
60 private DiagnosticGroup(DiagnosticType type) {
61 this.name = null;
62 this.types = ImmutableSet.of(type);
63 }
64
65 // DiagnosticGroups with only a single DiagnosticType.
66 private static final Map<DiagnosticType, DiagnosticGroup> singletons =
67 Maps.newHashMap();
68
69 /** Create a diagnostic group that matches only the given type. */
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 * Create a composite group.
79 */
80 public DiagnosticGroup(DiagnosticGroup ...groups) {
81 this(null, groups);
82 }
83
84 /**
85 * Create a composite group.
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 * Returns whether the given error's type matches a type
100 * in this group.
101 */
102 public boolean matches(JSError error) {
103 return matches(error.getType());
104 }
105
106 /**
107 * Returns whether the given type matches a type in this group.
108 */
109 public boolean matches(DiagnosticType type) {
110 return types.contains(type);
111 }
112
113 /**
114 * Returns whether all of the types in the given group are in this group.
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 * Returns an iterable over all the types in this group.
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 }