You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by David Li <da...@gmail.com> on 2017/04/24 07:57:48 UTC
Weird behaviour about enum with methods
Hi all,
I encountered something weird about enum value in the object stored in
ignite cache.
for example:
class definition:
public class Application {
private long id;
private String name;
private ApplicationType type;
}
enum definition:
public enum ApplicationType {
TX {
@Override
public boolean isSupported() {
return true;
}
},
API {
@Override
public boolean isSupported() {
return true;
}
};
public abstract boolean isSupported();
}
then have a cache like:
IgniteCache<Long, Application> cache
When get back the Application instance from cache, the value of the type
property will not equal to the enum value in the enum class, i found their
hash code is different, even though they have same class name.
for example,
Application app = new Application(1L, "app 1", ApplicationType.API);
app.getType() == ApplicationType.API is true
app.getType().getClass().getName() is ApplicationType$2, hash code is 251520863
Application appFound = cache.get(app.getId());
appFound.getType() == Application.API is false
appFound.getType().getClass().getName() is ApplicationType$2, hash
code is 331596257
ApplicationType.API.hashCode() is 251520863
I tried an enum without any method definitions, then no issue. Any
idea about this?
BRs,
David
Re: Weird behaviour about enum with methods
Posted by Andrey Mashenkov <an...@gmail.com>.
Hi David,
Looks like a bug. As a workaround, you can either use optimized marshaller
or compare enum ordinal.
I've created an issue IGNITE-5087 [1].
[1] https://issues.apache.org/jira/browse/IGNITE-5087
On Mon, Apr 24, 2017 at 10:57 AM, David Li <da...@gmail.com> wrote:
> Hi all,
>
> I encountered something weird about enum value in the object stored in
> ignite cache.
>
> for example:
>
> class definition:
>
> public class Application {
> private long id;
> private String name;
> private ApplicationType type;
> }
>
> enum definition:
>
> public enum ApplicationType {
> TX {
> @Override
> public boolean isSupported() {
> return true;
> }
> },
> API {
> @Override
> public boolean isSupported() {
> return true;
> }
> };
>
> public abstract boolean isSupported();
> }
>
> then have a cache like:
>
> IgniteCache<Long, Application> cache
>
> When get back the Application instance from cache, the value of the type
>
> property will not equal to the enum value in the enum class, i found their
>
> hash code is different, even though they have same class name.
>
> for example,
>
> Application app = new Application(1L, "app 1", ApplicationType.API);
>
> app.getType() == ApplicationType.API is true
>
> app.getType().getClass().getName() is ApplicationType$2, hash code is 251520863
>
>
> Application appFound = cache.get(app.getId());
>
> appFound.getType() == Application.API is false
>
> appFound.getType().getClass().getName() is ApplicationType$2, hash code is 331596257
>
>
> ApplicationType.API.hashCode() is 251520863
>
>
> I tried an enum without any method definitions, then no issue. Any idea about this?
>
>
> BRs,
>
> David
>
>
>
--
Best regards,
Andrey V. Mashenkov