mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stuart Scott <stu...@vuetec.com>
Subject IoBuffer getEnum/putEnum using signed values
Date Tue, 19 Feb 2008 10:33:02 GMT
I noticed that the various getEnum/putEnum methods in IoBuffer use the
signed values of the various data types. As Enums cannot have negative
ordinals would it not be more useful to use the unsigned version of the
data type?

This would be useless for getEnumInt/putEnumInt as an Enum can't be that
large but it might be useful for getEnumShort/putEnumShort and
getEnum/putEnum.

For example:

@Override
public <E extends Enum<E>> E getEnum(Class<E> enumClass) {
    return toEnum(enumClass, getUnsigned());
}

@Override
public <E extends Enum<E>> E getEnum(int index, Class<E> enumClass) {
    return toEnum(enumClass, getUnsigned(index));
}

@Override
public <E extends Enum<E>> E getEnumShort(Class<E> enumClass) {
    return toEnum(enumClass, getUnsignedShort());
}

@Override
public <E extends Enum<E>> E getEnumShort(int index, Class<E> enumClass)
{
    return toEnum(enumClass, getUnsignedShort(index));
}

@Override
public IoBuffer putEnum(Enum<?> e) {
    if (e.ordinal() > 0xff) {
        throw new IllegalArgumentException(enumConversionErrorMessage(e,
                "byte"));
    }
    return put((byte) e.ordinal());
}

@Override
public IoBuffer putEnum(int index, Enum<?> e) {
    if (e.ordinal() > 0xff) {
        throw new IllegalArgumentException(enumConversionErrorMessage(e,
                "byte"));
    }
    return put(index, (byte) e.ordinal());
}

@Override
public IoBuffer putEnumShort(Enum<?> e) {
    if (e.ordinal() > 0xffff) {
        throw new IllegalArgumentException(enumConversionErrorMessage(e,
                "short"));
    }
    return putShort((short) e.ordinal());
}

@Override
public IoBuffer putEnumShort(int index, Enum<?> e) {
    if (e.ordinal() > 0xffff) {
        throw new IllegalArgumentException(enumConversionErrorMessage(e,
                "short"));
    }
    return putShort(index, (short) e.ordinal());
}

Stuart

Mime
View raw message