geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r707092 - in /geronimo/gshell/trunk/gshell-support/gshell-ansi/src: main/java/org/apache/geronimo/gshell/ansi/ test/java/org/apache/geronimo/gshell/ansi/
Date Wed, 22 Oct 2008 14:43:53 GMT
Author: jdillon
Date: Wed Oct 22 07:43:52 2008
New Revision: 707092

URL: http://svn.apache.org/viewvc?rev=707092&view=rev
Log:
Make AnsiCode an enum

Modified:
    geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiBuffer.java
    geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiCode.java
    geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiRenderer.java
    geronimo/gshell/trunk/gshell-support/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java

Modified: geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiBuffer.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiBuffer.java?rev=707092&r1=707091&r2=707092&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiBuffer.java
(original)
+++ geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiBuffer.java
Wed Oct 22 07:43:52 2008
@@ -77,7 +77,7 @@
         return append(String.valueOf(obj));
     }
 
-    public AnsiBuffer attrib(final int code) {
+    public AnsiBuffer attrib(final AnsiCode code) {
         if (isAnsiEnabled()) {
             buff.append(AnsiCode.attrib(code));
         }
@@ -85,7 +85,7 @@
         return this;
     }
 
-    public AnsiBuffer attrib(final String text, final int code) {
+    public AnsiBuffer attrib(final String text, final AnsiCode code) {
         assert text != null;
 
         if (isAnsiEnabled()) {
@@ -99,6 +99,6 @@
     }
 
     public AnsiBuffer attrib(final String text, final String codeName) {
-        return attrib(text, AnsiCode.forName(codeName));
+        return attrib(text, AnsiCode.valueOf(codeName.toUpperCase()));
     }
 }

Modified: geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiCode.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiCode.java?rev=707092&r1=707091&r2=707092&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiCode.java
(original)
+++ geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiCode.java
Wed Oct 22 07:43:52 2008
@@ -19,128 +19,65 @@
 
 package org.apache.geronimo.gshell.ansi;
 
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.HashMap;
-import java.util.Map;
-
 /**
- * Container for ANSI color codes.
+ * <tt>ANSI</tt> color codes.
  *
  * @version $Rev$ $Date$
  */
-public class AnsiCode
+public enum AnsiCode
 {
-    //
-    // TODO: Consider converting this to an enum
-    //
-    
-    public static final int OFF = 0;
-    public static final int BOLD = 1;
-    public static final int UNDERSCORE = 4;
-    public static final int BLINK = 5;
-    public static final int REVERSE = 7;
-    public static final int CONCEALED = 8;
-
-    public static final int FG_BLACK = 30;
-    public static final int FG_RED = 31;
-    public static final int FG_GREEN = 32;
-    public static final int FG_YELLOW = 33;
-    public static final int FG_BLUE = 34;
-    public static final int FG_MAGENTA = 35;
-    public static final int FG_CYAN = 36;
-    public static final int FG_WHITE = 37;
-
-    public static final int BLACK = FG_BLACK;
-    public static final int RED = FG_RED;
-    public static final int GREEN = FG_GREEN;
-    public static final int YELLOW = FG_YELLOW;
-    public static final int BLUE = FG_BLUE;
-    public static final int MAGENTA = FG_MAGENTA;
-    public static final int CYAN = FG_CYAN;
-    public static final int WHITE = FG_WHITE;
-
-    public static final int BG_BLACK = 40;
-    public static final int BG_RED = 41;
-    public static final int BG_GREEN = 42;
-    public static final int BG_YELLOW = 43;
-    public static final int BG_BLUE = 44;
-    public static final int BG_MAGENTA = 45;
-    public static final int BG_CYAN = 46;
-    public static final int BG_WHITE = 47;
+    OFF(0),
+    BOLD(1),
+    UNDERSCORE(4),
+    BLINK(5),
+    REVERSE(7),
+    CONCEALED(8),
+
+    FG_BLACK(30),
+    FG_RED(31),
+    FG_GREEN(32),
+    FG_YELLOW(33),
+    FG_BLUE(34),
+    FG_MAGENTA(35),
+    FG_CYAN(36),
+    FG_WHITE(37),
+
+    BLACK(FG_BLACK),
+    RED(FG_RED),
+    GREEN(FG_GREEN),
+    YELLOW(FG_YELLOW),
+    BLUE(FG_BLUE),
+    MAGENTA(FG_MAGENTA),
+    CYAN(FG_CYAN),
+    WHITE(FG_WHITE),
+
+    BG_BLACK(40),
+    BG_RED(41),
+    BG_GREEN(42),
+    BG_YELLOW(43),
+    BG_BLUE(44),
+    BG_MAGENTA(45),
+    BG_CYAN(46),
+    BG_WHITE(47);
 
     /** The ANSI escape char which is used to start sequences. */
     private static final char ESC = 27;
 
-    /** A map of code names to values. */
-    private static final Map<String,Integer> NAMES_TO_CODES;
-
-    /** A map of codes to name. */
-    private static final Map<Integer,String> CODES_TO_NAMES;
+    private final int code;
 
-    static {
-        Field[] fields = AnsiCode.class.getDeclaredFields();
-        Map<String,Integer> names = new HashMap<String,Integer>(fields.length);
-        Map<Integer,String> codes = new HashMap<Integer,String>(fields.length);
-
-        try {
-            for (Field field : fields) {
-                // Skip anything non-public, all public fields are codes
-                int mods = field.getModifiers();
-                if (!Modifier.isPublic(mods)) {
-                    continue;
-                }
-
-                String name = field.getName();
-                Integer code = (Integer) field.get(AnsiCode.class);
-
-                names.put(name, code);
-                codes.put(code, name);
-            }
-        }
-        catch (IllegalAccessException e) {
-            // This should never happen
-            throw new Error(e);
-        }
-
-        NAMES_TO_CODES = names;
-        CODES_TO_NAMES = codes;
+    private AnsiCode(final int code) {
+        this.code = code;
     }
 
-    /**
-     * Returns the ANSI code for the given symbolic name.  Supported symbolic names are all
defined as
-     * fields in {@link AnsiCode} where the case is not significant.
-     */
-    public static int forName(final String name) throws IllegalArgumentException {
-        assert name != null;
-
-        // All names in the map are upper-case
-        String tmp = name.toUpperCase();
-        Integer code = NAMES_TO_CODES.get(tmp);
-
-        if (code == null) {
-            throw new IllegalArgumentException("Invalid ANSI code name: " + name);
-        }
-
-        return code;
-    }
-
-    /**
-     * Returns the symbolic name for the given ANSI code.
-     */
-    public static String name(final int code) throws IllegalArgumentException {
-        assert code >= 0;
-        
-        String name = CODES_TO_NAMES.get(code);
-
-        if (name == null) {
-            throw new IllegalArgumentException("Invalid ANSI code: " + code);
-        }
-
-        return name;
+    private AnsiCode(final AnsiCode code) {
+        this(code.code);
     }
+    
+    public static String attrib(final AnsiCode code) {
+        assert code != null;
 
-    public static String attrib(final int attr) {
-        return ESC + "[" + attr + "m";
+        StringBuilder buff = new StringBuilder();
+        buff.append(ESC).append("[").append(code.code).append("m");
+        return buff.toString();
     }
 }

Modified: geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiRenderer.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiRenderer.java?rev=707092&r1=707091&r2=707092&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiRenderer.java
(original)
+++ geronimo/gshell/trunk/gshell-support/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/AnsiRenderer.java
Wed Oct 22 07:43:52 2008
@@ -89,7 +89,7 @@
         String text = input.substring(i + 1, input.length());
 
         for (String name : codes) {
-            int code = AnsiCode.forName(name);
+            AnsiCode code = AnsiCode.valueOf(name.toUpperCase());
             buff.attrib(code);
         }
 
@@ -118,9 +118,9 @@
         return text != null && text.indexOf(BEGIN_TOKEN) >= 0;
     }
 
-    public static String encode(final Object text, final int code) {
+    public static String encode(final Object text, final AnsiCode code) {
         return new StringBuilder(BEGIN_TOKEN).
-                append(AnsiCode.name(code)).
+                append(code.name()).
                 append(CODE_TEXT_SEPARATOR).
                 append(text).
                 append(END_TOKEN).

Modified: geronimo/gshell/trunk/gshell-support/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java?rev=707092&r1=707091&r2=707092&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java
(original)
+++ geronimo/gshell/trunk/gshell-support/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java
Wed Oct 22 07:43:52 2008
@@ -30,11 +30,10 @@
     extends TestCase
 {
     public void testForName() throws Exception {
-        assertEquals(AnsiCode.OFF, AnsiCode.forName("OFF"));
-        assertEquals(AnsiCode.OFF, AnsiCode.forName("off"));
+        assertEquals(AnsiCode.OFF, AnsiCode.valueOf("OFF"));
     }
 
     public void testName() throws Exception {
-        assertEquals("OFF", AnsiCode.name(AnsiCode.OFF));
+        assertEquals("OFF", AnsiCode.OFF.name());
     }
 }
\ No newline at end of file



Mime
View raw message