The Video Age

I made a quick YouTube video of my graphics test program. Whee, it’s like the space age.

The neat thing (at least to me) is that the cutter beam from the game is actually a texture that is stretched and rotated to the location of the mouse. That solution seems to work really well.

Java Enumerations, or Hey, You Aren’t Just an En-number Anymore

Java has some cool features, but one thing I really like are enumerations. For my project I need a damage type, so I made a base class for it:

public abstract class Damage
{
    public enum Type
    {
        NORMAL("NORMAL"),
        FIRE("FIRE"),
        COLD("COLD"),
        AIR("AIR"),
        EARTH("EARTH"),
        POISON("POISON"),
        CORROSION("CORROSION"),
        ELECTRICAL("ELECTRICAL"),
        BLUNT("BLUNT"),
        PIERCING("PIERCING");

        private final String desc;
        private final String user;

        /**
         * Construct the enumeration with the description given
         * @param desc The description of the type
         */
        private Type(String desc)
        {
            // save the description in all uppercase
            this.desc=desc.toUpperCase();

            // the user first character is upper case
            // make the characters after the first lowercase for the user description
            this.user=Character.toString((Character.toUpperCase(this.desc.charAt(0))))+(this.desc.substring(1).toLowerCase());
        }

        /**
         * Get the description of the type
         * @return A string describing the type
         */
        public String getDesc()
        {
            return desc;
        }

        /**
         * Get the user-readable description of the type
         * @return A string for users describing the type
         */
        public String getUser()
        {
            return user;
        }

        @Override
        public String toString()
        {
            return user;
        }

    }

    /**
     * Get the damage for the damage type
     * @return The damage caused.
     */
    public abstract int getDamage();

    /**
     * Get the damage type for the damage
     * @return The damage type
     */
    public abstract Type getType();

    /**
     * Get the type of the damage given a string describing the type
     * @param desc The description of the damage type
     * @return The type of the damage
     * @throws IllegalArgumentException if the description has no match to a damage type
     */
    public Type getType(String desc) throws IllegalArgumentException
    {
        // if the description is empty, then make this normal
        if(desc.isEmpty())
        {
            return Type.NORMAL;
        }

        // loop through the values in the damage type enumeration
        // using the enhanced for loop after changing the enumeration to an array
        for(Type damageType : Type.values())
        {
            if(desc.equalsIgnoreCase(damageType.getDesc()))
            {
                // matched the damage type, return this type
                return damageType;
            }

        }        

        // no match
        throw new IllegalArgumentException("Cannot match damage type to description "+ desc);

    }

}

This base class is really just two things:  an interface to get the damage amount and type, and a container for the enumeration I use for the damage types. The neat thing I find is how I can match the damage type easily to a string by using Java’s enhanced for loop form and converting the enumeration to an array with the values() call.

That enhanced for loop may be old hat to Java programmers, but coming from a C++ world it is very cool. I am sure it boils down to the same-old-thing I would write in C once it goes through the byte code, but it is much more readable. Why not be able to iterate through the values in an enumeration? It is just intuitive and very usable.

But the really neat thing is how enumerations aren’t just for numbers anymore. I like being able to attach multiple data to the values in the enumeration. So it makes the enum type name a misnomer, but it is really useful. Just about every beginner Java book starts with an example of this a few sections in, and I believe that this is so they can show off this powerful capability to any C programmers delving into the language.

Well, I’m convinced. Programmer jealousy is a wonderful thing…maybe this will show up in C++ sometime? Strongly typed enumerations are not enough!