Source code: com/puppycrawl/tools/checkstyle/api/SeverityLevel.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19 package com.puppycrawl.tools.checkstyle.api;
20
21 import java.io.Serializable;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 /**
26 * Severity level for a check violation.
27 * <p>
28 * Each violation of an audit check is assigned one of the severity levels
29 * defined here.
30 *
31 * @author David Schneider
32 */
33 public final class SeverityLevel implements Comparable, Serializable
34 {
35 /** Numeric value for severity level IGNORE */
36 private static final int SEVERITYCODE_IGNORE = 10;
37 /** Numeric value for severity level INFO */
38 private static final int SEVERITYCODE_INFO = 20;
39 /** Numeric value for severity level WARNING */
40 private static final int SEVERITYCODE_WARNING = 30;
41 /** Numeric value for severity level ERROR */
42 private static final int SEVERITYCODE_ERROR = 40;
43
44
45 /** Name for severity level IGNORE */
46 private static final String SEVERITYNAME_IGNORE = "ignore";
47 /** Name for severity level INFO */
48 private static final String SEVERITYNAME_INFO = "info";
49 /** Name for severity level WARNING */
50 private static final String SEVERITYNAME_WARNING = "warning";
51 /** Name for severity level ERROR */
52 private static final String SEVERITYNAME_ERROR = "error";
53
54 /** Severity level: ignore. This is the lowest severity level. */
55 public static final SeverityLevel IGNORE =
56 new SeverityLevel(SEVERITYCODE_IGNORE, SEVERITYNAME_IGNORE);
57
58 /** Severity level: informational. */
59 public static final SeverityLevel INFO =
60 new SeverityLevel(SEVERITYCODE_INFO, SEVERITYNAME_INFO);
61
62 /** Severity level: warning. */
63 public static final SeverityLevel WARNING =
64 new SeverityLevel(SEVERITYCODE_WARNING, SEVERITYNAME_WARNING);
65
66 /** Severity level: error. This is the highest severity level. */
67 public static final SeverityLevel ERROR =
68 new SeverityLevel(SEVERITYCODE_ERROR, SEVERITYNAME_ERROR);
69
70 /** map from level names to the respective level */
71 private static final Map NAME_TO_LEVEL = new HashMap();
72 static {
73 NAME_TO_LEVEL.put(SEVERITYNAME_IGNORE, IGNORE);
74 NAME_TO_LEVEL.put(SEVERITYNAME_INFO, INFO);
75 NAME_TO_LEVEL.put(SEVERITYNAME_WARNING, WARNING);
76 NAME_TO_LEVEL.put(SEVERITYNAME_ERROR, ERROR);
77 }
78
79 /** the SEVERITYCODE_XYZ value of this severity level. */
80 private final int mCode;
81
82 /** the name of this severity level. */
83 private final String mName;
84
85 /**
86 * @see Object
87 */
88 public String toString()
89 {
90 return "Severity[" + mCode + " (" + mName + ")]";
91 }
92
93 /**
94 * @return the name of this severity level.
95 */
96 public String getName()
97 {
98 return mName;
99 }
100
101 /**
102 * @see Comparable
103 */
104 public int compareTo(Object aObject)
105 {
106 SeverityLevel severity = (SeverityLevel) aObject;
107 return this.mCode - severity.mCode;
108 }
109
110 /**
111 * The equals method.
112 *
113 * @param aObj Object to compare to.
114 *
115 * @return <code>true</code> means equal, <code>false</code> means
116 * not equal.
117 */
118 public boolean equals(Object aObj)
119 {
120 boolean result = false;
121
122 if ((aObj instanceof SeverityLevel)
123 && (((SeverityLevel) aObj).mCode == this.mCode))
124 {
125 result = true;
126 }
127
128 return result;
129 }
130
131 /**
132 * The hashCode method.
133 *
134 * @return hash code for the object.
135 */
136 public int hashCode()
137 {
138 return mCode;
139 }
140
141 /**
142 * Creates a new <code>SeverityLevel</code> instance.
143 *
144 * @param aCode one of the SEVERITYCODE_XYZ values.
145 * @param aName one of the SEVERITYNAME_XYZ values.
146 */
147 private SeverityLevel(int aCode, String aName)
148 {
149 mCode = aCode;
150 mName = aName;
151 }
152
153 /**
154 * SeverityLevel factory method.
155 *
156 * @param aSeverityName severity name, such as "ignore", "info", etc.
157 * @return the <code>SeverityLevel</code> associated with
158 * <code>aSeverityName</code>
159 */
160 public static SeverityLevel getInstance(String aSeverityName)
161 {
162 // canonicalize argument
163 final String severityName = aSeverityName.trim().toLowerCase();
164
165 final SeverityLevel retVal =
166 (SeverityLevel) NAME_TO_LEVEL.get(severityName);
167 if (retVal == null) {
168 throw new IllegalArgumentException(severityName);
169 }
170 return retVal;
171 }
172
173 /**
174 * Ensures that we don't get multiple instances of one SeverityLevel
175 * during deserialization. See Section 3.6 of the Java Object
176 * Serialization Specification for details.
177 *
178 * @return the serialization replacement object
179 */
180 private Object readResolve()
181 {
182 return getInstance(mName);
183 }
184 }