PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   26-07-2021

ENUM

p157 Before Enum, group constants were used.
 
class App {

    public static void main(String[] args) {

        System.out.println(
            Utils.Color.ANSI_RED + "This text is red!" + 
            Utils.Color.ANSI_RESET
        );
    }
}

class Utils {

    public static class Color {

        public static final String ANSI_RESET = "\u001B[0m";
        public static final String ANSI_BLACK = "\u001B[30m";
        public static final String ANSI_RED = "\u001B[31m";
        public static final String ANSI_GREEN = "\u001B[32m";
        public static final String ANSI_YELLOW = "\u001B[33m";
        public static final String ANSI_BLUE = "\u001B[34m";
        public static final String ANSI_PURPLE = "\u001B[35m";
        public static final String ANSI_CYAN = "\u001B[36m";
        public static final String ANSI_WHITE = "\u001B[37m";
    }
}
... 15 lines
 
p158 ! Enum types are classes that export one instance for each enumaration constant.
 
class App {

    public static void main(String[] args) {

        System.out.println(
            Utils.Planet.MERCURY // MERCURY
        );
            
    }
}

class Utils {

    public enum Planet { 
        MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE 
    }
}
... 6 lines
 
p158 An enum type can start as a simple collection of enum and evolve over time.
 
class App {

    public static void main(String[] args) {

        System.out.println(
            Color.PURPLE + "This text is purple!" + Color.RESET
        );
    }
}

enum Color {

    RESET, BLACK, RED, GREEN, YELLOW, BLUE, PURPLE, CYAN, WHITE;

    public String toString() { 
        switch(this) {
            case RESET:  return "\u001B[0m";
            case BLACK:  return "\u001B[30m";
            case RED:    return "\u001B[31m"; 
            case GREEN:  return "\u001B[32m";  
            case YELLOW: return "\u001B[33m"; 
            case BLUE:   return "\u001B[34m";
            case PURPLE: return "\u001B[35m"; 
            case CYAN:   return "\u001B[36m";  
            case WHITE:  return "\u001B[37m";
            default:     return null;
        }
    }
}
... 18 lines
 
 
class App {

    public static void main(String[] args) {

        System.out.println(
            Color.PURPLE + "This text is purple!" + Color.RESET
        );
    }
}

enum Color {
    
    RESET("\u001B[0m"),   BLACK("\u001B[30m"),  RED("\u001B[31m"), 
    GREEN("\u001B[32m"),  YELLOW("\u001B[33m"), BLUE("\u001B[34m"),
    PURPLE("\u001B[35m"), CYAN("\u001B[36m"),   WHITE("\u001B[37m");

    private final String value;
    private Color(String c) { value = c;  }
    public String toString() { return value; }
}
... 9 lines
 
 
class App {

    public static void main(String[] args) {

        System.out.println(
            Color.GREEN + "This text is green!" + Color.RESET
        );
    }
}

enum Color {

    RESET  { public String toString() { return "\u001B[0m";  } },
    BLACK  { public String toString() { return "\u001B[30m"; } },
    RED    { public String toString() { return "\u001B[31m"; } },
    GREEN  { public String toString() { return "\u001B[32m"; } },
    YELLOW { public String toString() { return "\u001B[33m"; } },
    BLUE   { public String toString() { return "\u001B[34m"; } },
    PURPLE { public String toString() { return "\u001B[35m"; } },
    CYAN   { public String toString() { return "\u001B[36m"; } },
    WHITE  { public String toString() { return "\u001B[37m"; } };
}
... 12 lines
 
! The primary advantage of using enum vs constants is type safety. With enum only the applicable values can be used.
 
class App {

    public static void main(String[] args) {

        Tools.print("This text is purple!", Color.PURPLE);
        Tools.print("This text is purple underline!", "\033[4;35m");

        Tools.print2("This text is purple!", ColorEnum.PURPLE);
        //Tools.print2("This text is purple!", "\033[4;35m"); // Look Here
    }
}

class Tools {
    
    public static void print(String message, String color) {
        System.out.println(color + message + Color.RESET);
    }

    public static void print2(String message, ColorEnum color) { // Look Here
        System.out.println(color + message + Color.RESET);
    }
}

class Color {
    public static final String RESET  = "\u001B[0m";
    public static final String BLACK  = "\u001B[30m";
    public static final String RED    = "\u001B[31m";
    public static final String GREEN  = "\u001B[32m";
    public static final String YELLOW = "\u001B[33m";
    public static final String BLUE   = "\u001B[34m";
    public static final String PURPLE = "\u001B[35m";
    public static final String CYAN   = "\u001B[36m";
    public static final String WHITE  = "\u001B[37m";
}

enum ColorEnum {

    RESET  { public String toString() { return "\u001B[0m";  } },
    BLACK  { public String toString() { return "\u001B[30m"; } },
    RED    { public String toString() { return "\u001B[31m"; } },
    GREEN  { public String toString() { return "\u001B[32m"; } },
    YELLOW { public String toString() { return "\u001B[33m"; } },
    BLUE   { public String toString() { return "\u001B[34m"; } },
    PURPLE { public String toString() { return "\u001B[35m"; } },
    CYAN   { public String toString() { return "\u001B[36m"; } },
    WHITE  { public String toString() { return "\u001B[37m"; } };
}
... 32 lines
 

Questions    
Lambdas

        A B C D E F
🔔
1/2