Search code examples
androidreusability

Reuse same bunch of statements several times


I 'm sorry if this is too dumb and simple but I am out of every option I can think of. It is not even real programming. But I have several lines in a switch statement which I 've to use several times. I can't think of any way in which I can store all this code (these lines) and reuse whenever i want.

        case 0:
            bcolor=Color.rgb(240, 255, 255);//Azure
            break;
        case 1:
            bcolor=Color.BLACK;
            break;
        case 2:
            bcolor=Color.BLUE;
            break;
        case 3:
            bcolor=Color.rgb(165, 42, 42);//Brown
            break;
        case 4:
            bcolor=Color.rgb(255, 127, 50);//Coral
            break;
        case 5:
            bcolor=Color.rgb(00, 255, 255);//cyan
            break;
        case 6:
            bcolor=Color.rgb(255, 00, 255);//Fuchsia
            break;
        case 7:
            bcolor=Color.rgb(255, 215, 00);//Gold
            break;
        case 8:
            bcolor=Color.GRAY;
            break;
        case 9:
            bcolor=Color.GREEN;
            break;
        case 10:
            bcolor=Color.rgb(173, 255, 47);//Green Yellow
            break;
        case 11:
            bcolor=Color.rgb(00, 255, 00);//Lime
            break;
        case 12:
            bcolor=Color.MAGENTA;
            break;
        case 13:
            bcolor=Color.rgb(80, 00, 00);//Maroon
            break;
        case 14:
            bcolor=Color.rgb(80, 80, 00);//Olive
            break;
        case 15:
            bcolor=Color.rgb(255, 165, 00);//Orange
            break;
        case 16:
            bcolor=Color.rgb(255, 192, 203);//pink
            break;
        case 17:
            bcolor=Color.rgb(80, 00, 80);//purple
            break;
        case 18:
            bcolor=Color.RED;
            break;
        case 19:
            bcolor=Color.rgb(244, 196, 30);//Saffron
            break;
        case 20:
            bcolor=Color.rgb(192, 192, 192);//Silver
            break;
        case 21:
            bcolor=Color.rgb(238, 82, 238);//Violet
            break;
        case 22:
            bcolor=Color.WHITE;
            break;
        case 23:
            bcolor=Color.YELLOW;
            break;

I have to use all these switch cases in several switch conditions. Isn't is possible to bind them in some kind of label or something. and just call them when I want to use...


Solution

  • One more optimal solution that I have seen and used is to store all of these values in a map (constant time complexity for lookups):

    public static Map<Integer, Integer> colorMap = new HashMap<Integer, Integer>();
    
    static
    {
        colorMap.put(0, Color.rgb(240, 255, 255));
        colorMap.put(1, Color.BLACK);
        colorMap.put(2, Color.BLUE);
        colorMap.put(3, Color.rgb(165, 42, 42));
        colorMap.put(4, Color.rgb(255, 127, 50));
        colorMap.put(5, Color.rgb(00, 255, 255));
        colorMap.put(6, Color.rgb(255, 00, 255));
        colorMap.put(7, Color.rgb(255, 215, 00));
        colorMap.put(8, Color.GRAY);
        colorMap.put(9, Color.GREEN);
        colorMap.put(10, Color.rgb(173, 255, 47));
        colorMap.put(11, Color.rgb(00, 255, 00));
        colorMap.put(12, Color.MAGENTA);
        colorMap.put(13, Color.rgb(80, 00, 00));
        colorMap.put(14, Color.rgb(80, 80, 00));
        colorMap.put(15, Color.rgb(255, 165, 00));
        colorMap.put(16, Color.rgb(255, 192, 203));
        colorMap.put(17, Color.rgb(80, 00, 80));
        colorMap.put(18, Color.RED);
        colorMap.put(19, Color.rgb(244, 196, 30));
        colorMap.put(20, Color.rgb(192, 192, 192));
        colorMap.put(21, Color.rgb(238, 82, 238));
        colorMap.put(22, Color.WHITE);
        colorMap.put(23, Color.YELLOW);
    }
    
    
    public static int getColor(int lookup)
    {
        int color = colorMap.get(lookup);
        return color == null ? 0 : color;
    }