/** * Create a decimal writable with no current value (i.e. isSet() will return false). */ @HiveDecimalWritableVersionV1 public HiveDecimalWritable() { super(); isSet = false; internalScratchLongs = null; internalScratchBuffer = null; }
@HiveDecimalWritableVersionV1 public void set(byte[] bytes, int scale) { this.internalStorage = bytes; this.scale = scale; }
@HiveDecimalWritableVersionV1 public int getScale() { return scale; }
@HiveDecimalWritableVersionV1 public byte[] getInternalStorage() { return internalStorage; }
@HiveDecimalWritableVersionV1 public HiveDecimalWritableV1() { }
/** * Create a decimal writable with an initial value from BigInteger bytes and a specified scale. * <p> * If the conversion to decimal is successful, isSet() will return true. * */ @HiveDecimalWritableVersionV1 public HiveDecimalWritable(byte[] bigIntegerBytes, int scale) { super(); setFromBigIntegerBytesAndScale(bigIntegerBytes, scale); }
/** * Set a decimal writable's value from BigInteger bytes and a specified scale. * <p> * If the conversion to decimal is successful, isSet() will return true. * */ @HiveDecimalWritableVersionV1 public void set(byte[] bigIntegerBytes, int scale) { setFromBigIntegerBytesAndScale(bigIntegerBytes, scale); }
/** * Create a decimal writable with an initial value from a long with scale 0. * <p> * Afterwards, the isSet() method will return true. * */ @HiveDecimalWritableVersionV1 public HiveDecimalWritable(long longValue) { super(); setFromLong(longValue); }
/** * Create a decimal writable with an initial value from another decimal writable. * <p> * If the supplied writable has a value, isSet() will return true. * */ @HiveDecimalWritableVersionV1 public HiveDecimalWritable(HiveDecimalWritable writable) { super(); set(writable); }
/** * Create a decimal writable with an initial value from a HiveDecimal. * <p> * Afterwards, the isSet() method will return true, unless value is null. * */ @HiveDecimalWritableVersionV1 public HiveDecimalWritable(HiveDecimal value) { super(); set(value); }
@HiveDecimalWritableVersionV1 public HiveDecimalWritableV1(HiveDecimalV1 value) { set(value); }
@HiveDecimalWritableVersionV1 public HiveDecimalV1 getHiveDecimal() { return HiveDecimalV1.create(new BigInteger(internalStorage), scale); }
@HiveDecimalWritableVersionV1 public HiveDecimalWritableV1(byte[] bytes, int scale) { set(bytes, scale); }
/** * Returns a HiveDecimal for the writable's current value. * <p> * Returns null if the writable isn't set. * */ @HiveDecimalWritableVersionV1 public HiveDecimal getHiveDecimal() { if (!isSet) { return null; } HiveDecimal result = HiveDecimal.createFromFast(this); return result; }
@HiveDecimalWritableVersionV1 @Override public void readFields(DataInput in) throws IOException { scale = WritableUtils.readVInt(in); int byteArrayLen = WritableUtils.readVInt(in); if (internalStorage.length != byteArrayLen) { internalStorage = new byte[byteArrayLen]; } in.readFully(internalStorage); }
@HiveDecimalWritableVersionV1 @Override public int hashCode() { if (!isSet()) { throw new RuntimeException("no value set"); } return fastHashCode(); }
@HiveDecimalWritableVersionV1 public int getScale() { if (!isSet()) { throw new RuntimeException("no value set"); } return fastScale(); }
@HiveDecimalWritableVersionV1 @Override public String toString() { if (!isSet()) { throw new RuntimeException("no value set"); } return fastToString(); }
@HiveDecimalWritableVersionV1 @Override public void write(DataOutput out) throws IOException { WritableUtils.writeVInt(out, scale); WritableUtils.writeVInt(out, internalStorage.length); out.write(internalStorage); }
/** * Get a OldHiveDecimal instance from the writable and constraint it with maximum precision/scale. * * @param maxPrecision maximum precision * @param maxScale maximum scale * @return OldHiveDecimal instance */ @HiveDecimalWritableVersionV1 public HiveDecimalV1 getHiveDecimal(int maxPrecision, int maxScale) { return HiveDecimalV1.enforcePrecisionScale(HiveDecimalV1. create(new BigInteger(internalStorage), scale), maxPrecision, maxScale); }