From ecc5239c78ca71db775de187b6b70481d8b08284 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Wed, 2 Jul 2008 01:22:16 +0000 Subject: [PATCH] Changed the classes Scope and SeverityLevel to be based on the Java 5 Enum class. Used patch #2004776 from Travis Schneeberger. --- .../tools/checkstyle/api/Scope.java | 146 ++---------------- .../tools/checkstyle/api/SeverityLevel.java | 144 +++-------------- .../tools/checkstyle/api/AllApiTests.java | 2 +- .../tools/checkstyle/api/ScopeTest.java | 82 +++++++++- .../checkstyle/api/SeverityLevelTest.java | 28 ++++ src/xdocs/releasenotes.xml | 6 + 6 files changed, 151 insertions(+), 257 deletions(-) create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/api/SeverityLevelTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Scope.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Scope.java index 07f08cb1d..6a8c289a8 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Scope.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Scope.java @@ -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 Lars Kühne + * @author Lars Kühne + * @author Travis Schneeberger */ -public final class Scope implements Comparable, 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 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, Serializable return (compareTo(aScope) <= 0); } - /** - * Creates a new Scope 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, 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()); } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/SeverityLevel.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/SeverityLevel.java index be18a8bc0..41f5412c6 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/SeverityLevel.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/SeverityLevel.java @@ -18,76 +18,33 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.api; -import com.google.common.collect.Maps; -import java.io.Serializable; -import java.util.Map; - /** + *

* Severity level for a check violation. + *

*

* Each violation of an audit check is assigned one of the severity levels * defined here. + *

* * @author David Schneider + * @author Travis Schneeberger */ -public final class SeverityLevel implements Comparable, - 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 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, */ 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 SeverityLevel 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 SeverityLevel associated with - * aSeverityName + * @param aSecurityLevelName level name, such as "ignore", "info", etc. + * @return the SeverityLevel + * associated with aSecurityLevelName */ - 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()); } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java b/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java index 90b1d5f72..cbfa44912 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java @@ -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 { } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java index 970dbef04..682b27ca3 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java @@ -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)); + } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/SeverityLevelTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/SeverityLevelTest.java new file mode 100644 index 000000000..aa7a2ba66 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/SeverityLevelTest.java @@ -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 "); + } +} diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index c43a794db..d9d71bcf4 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -121,6 +121,12 @@ Updated dependencies: Removed commons-collections; added google collections (google-collect-snapshot-20080321.jar); upgraded commons-cli to version 1.1. +
  • + Changed the classes Scope and + SeverityLevel to be based on the Java 5 + Enum class. Used patch #2004776 from Travis + Schneeberger. +