/** * Returns the value which this object represents. * @param alternate the alternative value for {@code null} * @return the value which this object represents, or the alternative one if this object represents {@code null} */ public BigDecimal or(BigDecimal alternate) { if (nullValue) { return alternate; } return get(); }
@Override Object extract(DecimalOption holder) { return holder.get(); } });
@Override protected Number get(DecimalOption property) { return property.get(); }
@Override protected String toString(Object value) { DecimalOption option = (DecimalOption) value; return option.get().toPlainString(); } }
@Override protected String toString(ValueOption<?> object) { return ((DecimalOption) object).get().toPlainString(); } }
@Override protected void doEmitDefault(DecimalOption property, StringBuilder output) { switch (outputStyle) { case PLAIN: output.append(property.get().toPlainString()); break; case ENGINEERING: output.append(property.get().toEngineeringString()); break; case SCIENTIFIC: output.append(property.get().toString()); break; default: throw new AssertionError(output); } }
@Override public String toString() { if (isNull()) { return String.valueOf((Object) null); } else { return get().toString(); } }
@Override public final void write(Object value, RecordConsumer consumer) { BigDecimal decimal = ((DecimalOption) value).get(); BigInteger unscaled = decimal .setScale(scale, RoundingMode.FLOOR) .unscaledValue(); writeAsBinary(consumer, decimal, unscaled); }
@Override protected void doWrite(DecimalOption property, ValueWriter writer) throws IOException { writer.writeDecimal(property.get()); }
@Override public void emit(DecimalOption option) throws IOException { startCell(); if (emitNull(option)) { return; } lineBuffer.append(option.get()); }
@Override public HiveDecimal getPrimitiveJavaObject(Object o) { DecimalOption object = (DecimalOption) o; if (object == null || object.isNull()) { return null; } return convert(object.get()); }
/** * test for get. */ @Test public void get() { DecimalOption option = new DecimalOption(); option.modify(decimal(100)); assertThat(option.get(), is(decimal(100))); assertThat(option.isNull(), is(false)); }
/** * test for copyFrom. */ @Test public void copy() { DecimalOption option = new DecimalOption(); DecimalOption other = new DecimalOption(); other.modify(decimal(50)); option.copyFrom(other); assertThat(option.get(), is(decimal(50))); option.modify(decimal(0)); assertThat(other.get(), is(decimal(50))); }
/** * test for parsing {@code decimal} value. * @throws Exception if failed */ @Test public void fillDecimal() throws Exception { DecimalOption value = new DecimalOption(); create("decimal"); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.get(), is(decimal("0"))); parser.fill(value); assertThat(value.get(), is(decimal("10"))); parser.fill(value); assertThat(value.get(), is(decimal("-10"))); parser.endRecord(); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.isNull(), is(true)); parser.fill(value); assertThat(value.get(), is(decimal("0.9999999999999999999999999999999999999999999999999"))); parser.fill(value); assertThat(value.get(), is(decimal("9223372036854775809"))); parser.endRecord(); assertThat(parser.next(), is(false)); }
assertThat(option.get(), is(decimal("0"))); assertThat(option.get(), is(decimal("1"))); assertThat(option.get(), is(decimal("50"))); assertThat(option.get(), is(decimal("-1"))); assertThat(option.get(), is(decimal("-50"))); assertThat(option.get(), is(decimal("0.5"))); assertThat(option.get(), is(decimal("-0.5"))); assertThat(option.get(), is(decimal("3.1415"))); assertThat(option.get(), is(decimal("-3.1415"))); assertThat(option.get(), is(decimal("0"))); assertThat(option.get(), is(decimal("0")));
assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.get(), is(decimal("0"))); parser.fill(value); assertThat(value.get(), is(decimal("10"))); parser.fill(value); assertThat(value.get(), is(decimal("-10"))); assertThat(value.isNull(), is(true)); parser.fill(value); assertThat(value.get(), is(decimal("0.9999999999999999999999999999999999999999999999999"))); parser.fill(value); assertThat(value.get(), is(decimal("9223372036854775809")));