001    package core;
002    
003    public enum Color
004    {
005            WHITE, BLACK, NONE;
006    
007            public static final Color[] colors = { WHITE, BLACK };
008    
009            /**
010             * @returns the color as a lowercase string
011             */
012            public String toString()
013            {
014                    switch (this)
015                    {
016                    case WHITE:
017                            return "white";
018                    case BLACK:
019                            return "black";
020                    case NONE:
021                            return "neutral";
022                    default:
023                            throw new RuntimeException("impossible");
024                    }
025            }
026    
027            public static Color fromString(String s)
028            {
029                    if (s.equals("white"))
030                            return WHITE;
031                    if (s.equals("black"))
032                            return BLACK;
033                    if (s.equals("neutral"))
034                            return NONE;
035                    throw new RuntimeException("impossible");
036            }
037    
038            /**
039             * @returns the other color if BLACK or WHITE, and throws an exception
040             *          otherwise.
041             */
042            public Color otherColor()
043            {
044                    switch (this)
045                    {
046                    case WHITE:
047                            return BLACK;
048                    case BLACK:
049                            return WHITE;
050                    default:
051                            throw new RuntimeException("trying to get otherColor of NONE");
052                    }// testagain
053            }
054    }