safe cast for Short Project: http://git-wip-us.apache.org/repos/asf/incubator-hivemall/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-hivemall/commit/deea39a2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-hivemall/tree/deea39a2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-hivemall/diff/deea39a2
Branch: refs/heads/HIVEMALL-24-2 Commit: deea39a25653d0d85993ca50236003a0db13c202 Parents: 82fa581 Author: Makoto Yui <[email protected]> Authored: Mon Jul 24 15:22:16 2017 +0900 Committer: Makoto Yui <[email protected]> Committed: Mon Jul 24 15:22:16 2017 +0900 ---------------------------------------------------------------------- core/src/main/java/hivemall/fm/Feature.java | 9 +++++---- .../src/main/java/hivemall/utils/lang/Primitives.java | 14 +++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-hivemall/blob/deea39a2/core/src/main/java/hivemall/fm/Feature.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/hivemall/fm/Feature.java b/core/src/main/java/hivemall/fm/Feature.java index 2966a02..f77d3fd 100644 --- a/core/src/main/java/hivemall/fm/Feature.java +++ b/core/src/main/java/hivemall/fm/Feature.java @@ -18,6 +18,7 @@ */ package hivemall.fm; +import static hivemall.utils.lang.Primitives.castToShort; import hivemall.utils.hashing.MurmurHash3; import hivemall.utils.lang.NumberUtils; @@ -219,7 +220,7 @@ public abstract class Feature { } else { index = MurmurHash3.murmurhash3(lead, numFields); } - short field = (short) index; + short field = castToShort(index); double value = parseFeatureValue(rest); return new IntFeature(index, field, value); } @@ -237,7 +238,7 @@ public abstract class Feature { } else { // +NUM_FIELD to avoid conflict to quantitative features index = MurmurHash3.murmurhash3(indexStr, numFeatures) + numFields; - field = (short) MurmurHash3.murmurhash3(lead, numFields); + field = castToShort(MurmurHash3.murmurhash3(lead, numFields)); } String valueStr = rest.substring(pos2 + 1); double value = parseFeatureValue(valueStr); @@ -296,7 +297,7 @@ public abstract class Feature { } else { index = MurmurHash3.murmurhash3(lead, numFields); } - short field = (short) index; + short field = castToShort(index); probe.setField(field); probe.setFeatureIndex(index); probe.value = parseFeatureValue(rest); @@ -316,7 +317,7 @@ public abstract class Feature { } else { // +NUM_FIELD to avoid conflict to quantitative features index = MurmurHash3.murmurhash3(indexStr, numFeatures) + numFields; - field = (short) MurmurHash3.murmurhash3(lead, numFields); + field = castToShort(MurmurHash3.murmurhash3(lead, numFields)); } probe.setField(field); probe.setFeatureIndex(index); http://git-wip-us.apache.org/repos/asf/incubator-hivemall/blob/deea39a2/core/src/main/java/hivemall/utils/lang/Primitives.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/hivemall/utils/lang/Primitives.java b/core/src/main/java/hivemall/utils/lang/Primitives.java index 2ec012c..a6e945f 100644 --- a/core/src/main/java/hivemall/utils/lang/Primitives.java +++ b/core/src/main/java/hivemall/utils/lang/Primitives.java @@ -92,16 +92,16 @@ public final class Primitives { b[off] = (byte) (val >>> 8); } - public static int toIntExact(final long longValue) { - final int casted = (int) longValue; - if (casted != longValue) { - throw new ArithmeticException("integer overflow: " + longValue); + public static int castToInt(final long value) { + final int result = (int) value; + if (result != value) { + throw new IllegalArgumentException("Out of range: " + value); } - return casted; + return result; } - public static int castToInt(final long value) { - final int result = (int) value; + public static short castToShort(final int value) { + final short result = (short) value; if (result != value) { throw new IllegalArgumentException("Out of range: " + value); }
