Hopefully this is fairly basic. I've never used enumerated types in Java. I'm sort of trying to take my C concept of them and make it work in Java. Basically use them as constant integer values. That might be putting the square C block into the round Java hole. I'm not sure. Here's the code. It's giving me errors in lines 42 and 47 (inconvertible types). If the constructor gets passed 3, I want this.code to be assigned to be INVALID_BYTE_VALUE.

package PacketException;


public class PacketException
{
    public static enum ExceptionCode
    {
        UNSPECIFIED_PACKET_EXCEPTION,
        INVALID_HEX_DIGIT,
        INVALID_BINARY_DIGIT,
        INVALID_BYTE_VALUE,
        INVALID_HEX_BYTE_TEXT_REP,
        INVALID_BINARY_BYTE_TEXT_REP,
        INVALID_PACKET_TEXT_REP,
        INVALID_EVAL_BOARD_SNIFFER_PARSE,
        INVALID_ENCODED_BYTE,
        INVALID_DECODED_BYTE,
        INVALID_CRC,
        INVALID_PACKET_LENGTH,
        INVALID_ENCRYPTION_METHOD,
        INVALID_PTYP,
        INVALID_PREAMBLE,
        INVALID_SOF,
        INVALID_HOPS,
        INVALID_NACK_REASON,
        INVALID_DATA_RATE
    }

    public String message;
    public ExceptionCode code;


    
    public PacketException ()
    {
        this.code = ExceptionCode.UNSPECIFIED_PACKET_EXCEPTION;
        this.message = "";
    }

    public PacketException (int code)
    {
        this.code = (ExceptionCode) code;
    }

    public PacketException (String message, int code)
    {
        this.code = (ExceptionCode) code;
        this.message = message;
    }
}

Edited 6 Years Ago by VernonDozier: Screwed up the code tags.

You can use:

ExceptionCode.values()[code];

to access the values array by declaration index. (I can't put that in code tags though because it contains half a code tag :) )

Edited 3 Years Ago by pritaeas: Fixed formatting

You can use:
ExceptionCode.values();
to access the values array by declaration index. (I can't put that in code tags though because it contains half a code tag :) )

Thanks. Yeah, I guess it does contain half a code tag. Hadn't thought of that, but it doesn't now, so we're OK.;)

Below seems to work, but is leaving me not very happy. If there's another way to convert between types, please tell me or if I am completely missing the entire point of enumerated types, please tell me. There's going to be some encryption and encoding and packet parsing going on, so I need to be able to convert back and forth so I can do arithmetic. The toInt () below seems extremely inefficient, particularly for large enumerated types.

package PacketException;


public class PacketException
{
    public static enum ExceptionCode
    {
        UNSPECIFIED_PACKET_EXCEPTION,
        INVALID_HEX_DIGIT,
        INVALID_BINARY_DIGIT,
        INVALID_BYTE_VALUE,
        INVALID_HEX_BYTE_TEXT_REP,
        INVALID_BINARY_BYTE_TEXT_REP,
        INVALID_PACKET_TEXT_REP,
        INVALID_EVAL_BOARD_SNIFFER_PARSE,
        INVALID_ENCODED_BYTE,
        INVALID_DECODED_BYTE,
        INVALID_CRC,
        INVALID_PACKET_LENGTH,
        INVALID_ENCRYPTION_METHOD,
        INVALID_PTYP,
        INVALID_PREAMBLE,
        INVALID_SOF,
        INVALID_HOPS,
        INVALID_NACK_REASON,
        INVALID_DATA_RATE
    }

    public String message;
    public ExceptionCode theCode;



    public PacketException ()
    {
        this.theCode = ExceptionCode.UNSPECIFIED_PACKET_EXCEPTION;
        this.message = "";
    }

    public PacketException (int theCode)
    {
        this.theCode = toExceptionCode (theCode);
        this.message = "";
    }

    public PacketException (String message, int theCode)
    {
        this.theCode = toExceptionCode (theCode);
        this.message = message;
    }

    public static ExceptionCode toExceptionCode (int anInt)
    {
        ExceptionCode values[] = ExceptionCode.values();
        if (anInt < 0 || anInt >= values.length)
            return null;

        return values[anInt];
    }

    public static int toInt (ExceptionCode exCode)
    {
        if (exCode == null)
            return -1;

        ExceptionCode values[] = ExceptionCode.values();
        for (int i = 0; i < values.length; i++)
        {
            if (exCode == values[i])
                return i;
        }

        return -1; // I assume it's impossible to get here?
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.