I'm accessing a MongoDB instance from Java that's written to from a Rails App. I'm retrieving integer values that should be stored in a Long, because they can exceed 32 bits.
This code will compile:
this.profile_uid = (Long)this.profile.get("uid");
However, I'm getting a type conversion run-time error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
This is presumably because the field is returned by Mongo as Integer, but I know that some ID's can come as Longs and for various reasons I can't change the type that written to the DB (from another app); it may be 32-bit in some cases and 64-bit in others.
The Java app needs to handle either, and I don't want to run into some sort of truncation or overflow issue. I want to read it as a Long on the Java side.
I've tried the workaround below and it seems to run, but I don't know if I'm safe from truncation or overflow issues this way. I'm not sure what the Number class in Java does.
this.profile_uid = ((Number)this.profile.get("uid")).longValue();
Is this legit? What side effects does it have? Is there another/better way?