You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Emmanuel Lecharny <el...@gmail.com> on 2010/11/11 11:28:58 UTC
A small hint when using Enum in the codec
Hi guys,
yesterday, I was stuck for a couple of hours (it was late, and my brain
was half sleeping. The only part working was my right half, the one in
charge of emotion and slang ...), trying to understand why the kerberos
codec was not accepting my correct hand crafted PDU. At the end, I
realized that instead of getting the value I affected to a constant, it
took the ordinal.
Such an enum declaration:
public enum MyEnum {
VA(10),
VB(20),
VB(30);
private int value;
private MyEnum( int value ) (
this.value = value;
}
public int getValue() {
return value;
}
}
will return 20 when doing VB.getValue() but *1* when doing
VB.getOrdinal()...
Don't be as idiot as I was : use getValue instead of getOrdinal, which
is based on the field order. Also always declare a 'value' field, not an
'ordinal' field, as it was done in many case...
I should have knew it, but anyway. I hope it can spare you some time...
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com
Re: A small hint when using Enum in the codec
Posted by Emmanuel Lecharny <el...@apache.org>.
Absolutely. It's just that when you add a value to the fields, then avoid
using the getOrdinal() method ;)
On Thu, Nov 11, 2010 at 12:08 PM, Felix Knecht <fe...@apache.org> wrote:
> Don't be as idiot as I was : use getValue instead of getOrdinal, which
>> is based on the field order. Also always declare a 'value' field, not an
>> 'ordinal' field, as it was done in many case...
>>
>
> Well, it depends always on the use case. Some time ago when I fixed
> DIRSHARED-64 (Transform IStates and inherited interfaces/implementing
> classes to enums) I finally realized, that it's not really necessary to have
> a custom value. Sometimes the use of the ordinal can make sense as well (
> http://svn.apache.org/viewvc?rev=1028638&view=rev).
>
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com
Re: A small hint when using Enum in the codec
Posted by Felix Knecht <fe...@apache.org>.
> Don't be as idiot as I was : use getValue instead of getOrdinal, which
> is based on the field order. Also always declare a 'value' field, not an
> 'ordinal' field, as it was done in many case...
Well, it depends always on the use case. Some time ago when I fixed
DIRSHARED-64 (Transform IStates and inherited interfaces/implementing
classes to enums) I finally realized, that it's not really necessary to
have a custom value. Sometimes the use of the ordinal can make sense as
well (http://svn.apache.org/viewvc?rev=1028638&view=rev).