Changed the classes Scope and SeverityLevel to be based on the Java 5 Enum class. Used patch #2004776 from Travis Schneeberger.

This commit is contained in:
Oliver Burn 2008-07-02 01:22:16 +00:00
parent 3e05297d53
commit ecc5239c78
6 changed files with 151 additions and 257 deletions

View File

@ -18,121 +18,39 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.Map;
/**
* Represents a Java visibility scope.
*
* @author <a href="mailto:lkuehne@users.sourceforge.net">Lars Kühne</a>
* @author Lars Kühne
* @author Travis Schneeberger
*/
public final class Scope implements Comparable<Scope>, Serializable
public enum Scope
{
// Note that although this class might seem to be an
// implementation detail, this class has to be public because it
// is used as a parameter in GlobalProperties.setJavadocScope()
/** poor man's enum for nothing scope */
private static final int SCOPECODE_NOTHING = 0;
/** poor man's enum for public scope */
private static final int SCOPECODE_PUBLIC = 1;
/** poor man's enum for protected scope */
private static final int SCOPECODE_PROTECTED = 2;
/** poor man's enum for package scope */
private static final int SCOPECODE_PACKAGE = 3;
/** poor man's enum for private scope */
private static final int SCOPECODE_PRIVATE = 4;
/** poor man's enum for anonymous inner class scope */
private static final int SCOPECODE_ANONINNER = 5;
/** none scopename */
private static final String SCOPENAME_NOTHING = "nothing";
/** public scopename */
private static final String SCOPENAME_PUBLIC = "public";
/** protected scopename */
private static final String SCOPENAME_PROTECTED = "protected";
/** package scopename */
private static final String SCOPENAME_PACKAGE = "package";
/** private scopename */
private static final String SCOPENAME_PRIVATE = "private";
/** anon inner scopename */
private static final String SCOPENAME_ANONINNER = "anoninner";
/** nothing scope. */
public static final Scope NOTHING =
new Scope(SCOPECODE_NOTHING, SCOPENAME_NOTHING);
/** public scope. */
public static final Scope PUBLIC =
new Scope(SCOPECODE_PUBLIC, SCOPENAME_PUBLIC);
NOTHING,
/** protected scope. */
public static final Scope PROTECTED =
new Scope(SCOPECODE_PROTECTED, SCOPENAME_PROTECTED);
/** package scope. */
public static final Scope PACKAGE =
new Scope(SCOPECODE_PACKAGE, SCOPENAME_PACKAGE);
PUBLIC,
/** protected scope. */
PROTECTED,
/** package or default scope. */
PACKAGE,
/** private scope. */
public static final Scope PRIVATE =
new Scope(SCOPECODE_PRIVATE, SCOPENAME_PRIVATE);
/** anon inner scope. */
public static final Scope ANONINNER =
new Scope(SCOPECODE_ANONINNER, SCOPENAME_ANONINNER);
/** map from scope names to the respective Scope */
private static final Map<String, Scope> NAME_TO_SCOPE = Maps.newHashMap();
static {
NAME_TO_SCOPE.put(SCOPENAME_NOTHING, NOTHING);
NAME_TO_SCOPE.put(SCOPENAME_PUBLIC, PUBLIC);
NAME_TO_SCOPE.put(SCOPENAME_PROTECTED, PROTECTED);
NAME_TO_SCOPE.put(SCOPENAME_PACKAGE, PACKAGE);
NAME_TO_SCOPE.put(SCOPENAME_PRIVATE, PRIVATE);
NAME_TO_SCOPE.put(SCOPENAME_ANONINNER, ANONINNER);
}
/** the SCOPECODE_XYZ value of this scope. */
private final int mCode;
/** the name of this scope. */
private final String mName;
PRIVATE,
/** anonymous inner scope. */
ANONINNER;
@Override
public String toString()
{
return "Scope[" + mCode + " (" + mName + ")]";
return getName();
}
/**
* @return the name of this scope.
* @return the name of this severity level.
*/
public String getName()
{
return mName;
}
/**
* {@inheritDoc}
*/
public int compareTo(Scope aObject)
{
return this.mCode - aObject.mCode;
}
@Override
public boolean equals(Object aOther)
{
// Since this is an Enum class, can do a simple implementation.
return (this == aOther);
}
@Override
public int hashCode()
{
return mCode;
return name().toLowerCase();
}
/**
@ -147,18 +65,6 @@ public final class Scope implements Comparable<Scope>, Serializable
return (compareTo(aScope) <= 0);
}
/**
* Creates a new <code>Scope</code> instance.
*
* @param aCode one of the SCOPECODE_XYZ values.
* @param aName one of the SCOPENAME_XYZ values.
*/
private Scope(int aCode, String aName)
{
mCode = aCode;
mName = aName;
}
/**
* Scope factory method.
*
@ -167,26 +73,6 @@ public final class Scope implements Comparable<Scope>, Serializable
*/
public static Scope getInstance(String aScopeName)
{
// TODO: change scope....
// canonicalize argument
final String scopeName = aScopeName.trim().toLowerCase();
final Scope retVal = NAME_TO_SCOPE.get(scopeName);
if (retVal == null) {
throw new IllegalArgumentException(scopeName);
}
return retVal;
}
/**
* Ensures that we don't get multiple instances of one Scope
* during deserialization. See Section 3.6 of the Java Object
* Serialization Specification for details.
*
* @return the serialization replacement object
*/
private Object readResolve()
{
return getInstance(mName);
return valueOf(Scope.class, aScopeName.trim().toUpperCase());
}
}

View File

@ -18,76 +18,33 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.Map;
/**
* <p>
* Severity level for a check violation.
* </p>
* <p>
* Each violation of an audit check is assigned one of the severity levels
* defined here.
* </p>
*
* @author David Schneider
* @author Travis Schneeberger
*/
public final class SeverityLevel implements Comparable<SeverityLevel>,
Serializable
public enum SeverityLevel
{
/** Numeric value for severity level IGNORE */
private static final int SEVERITYCODE_IGNORE = 10;
/** Numeric value for severity level INFO */
private static final int SEVERITYCODE_INFO = 20;
/** Numeric value for severity level WARNING */
private static final int SEVERITYCODE_WARNING = 30;
/** Numeric value for severity level ERROR */
private static final int SEVERITYCODE_ERROR = 40;
/** Name for severity level IGNORE */
private static final String SEVERITYNAME_IGNORE = "ignore";
/** Name for severity level INFO */
private static final String SEVERITYNAME_INFO = "info";
/** Name for severity level WARNING */
private static final String SEVERITYNAME_WARNING = "warning";
/** Name for severity level ERROR */
private static final String SEVERITYNAME_ERROR = "error";
/** Severity level: ignore. This is the lowest severity level. */
public static final SeverityLevel IGNORE =
new SeverityLevel(SEVERITYCODE_IGNORE, SEVERITYNAME_IGNORE);
/** Severity level: informational. */
public static final SeverityLevel INFO =
new SeverityLevel(SEVERITYCODE_INFO, SEVERITYNAME_INFO);
/** Severity level: warning. */
public static final SeverityLevel WARNING =
new SeverityLevel(SEVERITYCODE_WARNING, SEVERITYNAME_WARNING);
/** Severity level: error. This is the highest severity level. */
public static final SeverityLevel ERROR =
new SeverityLevel(SEVERITYCODE_ERROR, SEVERITYNAME_ERROR);
/** map from level names to the respective level */
private static final Map<String, SeverityLevel> NAME_TO_LEVEL =
Maps.newHashMap();
static {
NAME_TO_LEVEL.put(SEVERITYNAME_IGNORE, IGNORE);
NAME_TO_LEVEL.put(SEVERITYNAME_INFO, INFO);
NAME_TO_LEVEL.put(SEVERITYNAME_WARNING, WARNING);
NAME_TO_LEVEL.put(SEVERITYNAME_ERROR, ERROR);
}
/** the SEVERITYCODE_XYZ value of this severity level. */
private final int mCode;
/** the name of this severity level. */
private final String mName;
/** security level ignore. */
IGNORE,
/** security level info. */
INFO,
/** security level warning. */
WARNING,
/** security level error. */
ERROR;
@Override
public String toString()
{
return "Severity[" + mCode + " (" + mName + ")]";
return getName();
}
/**
@ -95,78 +52,19 @@ public final class SeverityLevel implements Comparable<SeverityLevel>,
*/
public String getName()
{
return mName;
}
/**
* {@inheritDoc}
*/
public int compareTo(SeverityLevel aObject)
{
return this.mCode - aObject.mCode;
}
@Override
public boolean equals(Object aObj)
{
boolean result = false;
if ((aObj instanceof SeverityLevel)
&& (((SeverityLevel) aObj).mCode == this.mCode))
{
result = true;
}
return result;
}
@Override
public int hashCode()
{
return mCode;
}
/**
* Creates a new <code>SeverityLevel</code> instance.
*
* @param aCode one of the SEVERITYCODE_XYZ values.
* @param aName one of the SEVERITYNAME_XYZ values.
*/
private SeverityLevel(int aCode, String aName)
{
mCode = aCode;
mName = aName;
return name().toLowerCase();
}
/**
* SeverityLevel factory method.
*
* @param aSeverityName severity name, such as "ignore", "info", etc.
* @return the <code>SeverityLevel</code> associated with
* <code>aSeverityName</code>
* @param aSecurityLevelName level name, such as "ignore", "info", etc.
* @return the <code>SeverityLevel</code>
* associated with <code>aSecurityLevelName</code>
*/
public static SeverityLevel getInstance(String aSeverityName)
public static SeverityLevel getInstance(String aSecurityLevelName)
{
// canonicalize argument
final String severityName = aSeverityName.trim().toLowerCase();
final SeverityLevel retVal =
NAME_TO_LEVEL.get(severityName);
if (retVal == null) {
throw new IllegalArgumentException(severityName);
}
return retVal;
}
/**
* Ensures that we don't get multiple instances of one SeverityLevel
* during deserialization. See Section 3.6 of the Java Object
* Serialization Specification for details.
*
* @return the serialization replacement object
*/
private Object readResolve()
{
return getInstance(mName);
return valueOf(SeverityLevel.class, aSecurityLevelName.trim()
.toUpperCase());
}
}

View File

@ -6,7 +6,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses( {AbstractViolationReporterTest.class,
AutomaticBeanTest.class, DetailASTTest.class, ScopeTest.class,
TokenTypesTest.class, FastStackTest.class})
SeverityLevelTest.class, TokenTypesTest.class, FastStackTest.class})
public class AllApiTests
{
}

View File

@ -1,7 +1,6 @@
package com.puppycrawl.tools.checkstyle.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import org.junit.Test;
@ -12,9 +11,86 @@ public class ScopeTest
{
final Scope o = Scope.getInstance("public");
assertNotNull(o);
assertEquals("Scope[1 (public)]", o.toString());
assertEquals("public", o.toString());
assertEquals("public", o.getName());
Scope.getInstance("unknown"); // will fail
}
@Test
public void testMixedCaseSpaces()
{
Scope.getInstance("NothinG ");
Scope.getInstance(" PuBlic");
Scope.getInstance(" ProteCted");
Scope.getInstance(" PackAge ");
Scope.getInstance("privaTe ");
Scope.getInstance("AnonInner");
}
@Test
public void testIsInAnonInner()
{
assertTrue(Scope.NOTHING.isIn(Scope.ANONINNER));
assertTrue(Scope.PUBLIC.isIn(Scope.ANONINNER));
assertTrue(Scope.PROTECTED.isIn(Scope.ANONINNER));
assertTrue(Scope.PACKAGE.isIn(Scope.ANONINNER));
assertTrue(Scope.PRIVATE.isIn(Scope.ANONINNER));
assertTrue(Scope.ANONINNER.isIn(Scope.ANONINNER));
}
@Test
public void testIsInPrivate()
{
assertTrue(Scope.NOTHING.isIn(Scope.PRIVATE));
assertTrue(Scope.PUBLIC.isIn(Scope.PRIVATE));
assertTrue(Scope.PROTECTED.isIn(Scope.PRIVATE));
assertTrue(Scope.PACKAGE.isIn(Scope.PRIVATE));
assertTrue(Scope.PRIVATE.isIn(Scope.PRIVATE));
assertTrue(!Scope.ANONINNER.isIn(Scope.PRIVATE));
}
@Test
public void testIsInPackage()
{
assertTrue(Scope.NOTHING.isIn(Scope.PACKAGE));
assertTrue(Scope.PUBLIC.isIn(Scope.PACKAGE));
assertTrue(Scope.PROTECTED.isIn(Scope.PACKAGE));
assertTrue(Scope.PACKAGE.isIn(Scope.PACKAGE));
assertTrue(!Scope.PRIVATE.isIn(Scope.PACKAGE));
assertTrue(!Scope.ANONINNER.isIn(Scope.PACKAGE));
}
@Test
public void testIsInProtected()
{
assertTrue(Scope.NOTHING.isIn(Scope.PROTECTED));
assertTrue(Scope.PUBLIC.isIn(Scope.PROTECTED));
assertTrue(Scope.PROTECTED.isIn(Scope.PROTECTED));
assertTrue(!Scope.PACKAGE.isIn(Scope.PROTECTED));
assertTrue(!Scope.PRIVATE.isIn(Scope.PROTECTED));
assertTrue(!Scope.ANONINNER.isIn(Scope.PROTECTED));
}
@Test
public void testIsInPublic()
{
assertTrue(Scope.NOTHING.isIn(Scope.PUBLIC));
assertTrue(Scope.PUBLIC.isIn(Scope.PUBLIC));
assertTrue(!Scope.PROTECTED.isIn(Scope.PUBLIC));
assertTrue(!Scope.PACKAGE.isIn(Scope.PUBLIC));
assertTrue(!Scope.PRIVATE.isIn(Scope.PUBLIC));
assertTrue(!Scope.ANONINNER.isIn(Scope.PUBLIC));
}
@Test
public void testIsInNothing()
{
assertTrue(Scope.NOTHING.isIn(Scope.NOTHING));
assertTrue(!Scope.PUBLIC.isIn(Scope.NOTHING));
assertTrue(!Scope.PROTECTED.isIn(Scope.NOTHING));
assertTrue(!Scope.PACKAGE.isIn(Scope.NOTHING));
assertTrue(!Scope.PRIVATE.isIn(Scope.NOTHING));
assertTrue(!Scope.ANONINNER.isIn(Scope.NOTHING));
}
}

View File

@ -0,0 +1,28 @@
package com.puppycrawl.tools.checkstyle.api;
import static org.junit.Assert.*;
import org.junit.Test;
public class SeverityLevelTest
{
@Test(expected = IllegalArgumentException.class)
public void testMisc()
{
final SeverityLevel o = SeverityLevel.getInstance("info");
assertNotNull(o);
assertEquals("info", o.toString());
assertEquals("info", o.getName());
SeverityLevel.getInstance("unknown"); // will fail
}
@Test
public void testMixedCaseSpaces()
{
SeverityLevel.getInstance("IgnoRe ");
SeverityLevel.getInstance(" iNfo");
SeverityLevel.getInstance(" WarniNg");
SeverityLevel.getInstance(" ERROR ");
}
}

View File

@ -121,6 +121,12 @@
<i>Updated dependencies</i>: Removed commons-collections; added google collections
(google-collect-snapshot-20080321.jar); upgraded commons-cli to version 1.1.
</li>
<li>
Changed the classes <span class="code">Scope</span> and
<span class="code">SeverityLevel</span> to be based on the Java 5
<span class="code">Enum</span> class. Used patch #2004776 from Travis
Schneeberger.
</li>
</ul>
</section>