/** * 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(); }
@SuppressWarnings("deprecation") @Override protected void set(BigDecimal value, DecimalOption property) { property.modify(value); }
@Override protected ValueOption<?> newObject() { return new DecimalOption(); }
/** * 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 Writable. */ @Test public void write() { DecimalOption option = new DecimalOption(); option.modify(decimal("3.14")); DecimalOption restored = restore(option); assertThat(restored.get(), is(option.get())); }
/** * 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)); }
/** * test for compare w/ null. */ @Test public void compareNull() { DecimalOption a = new DecimalOption(); DecimalOption b = new DecimalOption(); DecimalOption c = new DecimalOption(); a.modify(decimal(Long.MIN_VALUE)); assertThat(compare(a, b), greaterThan(0)); assertThat(compare(b, a), lessThan(0)); assertThat(compare(b, c), is(0)); }
/** * test for copyFrom w/ null. */ @Test public void copyNull() { DecimalOption option = new DecimalOption(); option.modify(decimal(100)); DecimalOption other = new DecimalOption(); option.copyFrom(other); assertThat(option.isNull(), is(true)); option.modify(decimal(100)); option.copyFrom(null); assertThat(option.isNull(), is(true)); }
@SuppressWarnings("deprecation") @Override public void readFields(DataInput in) throws IOException { int head = in.readByte() & 0xff; if ((head & MASK_PRESENT) == 0) { setNull(); return; } boolean plus = (head & MASK_PLUS) != 0; int scale = WritableUtils.readVInt(in); int length = WritableUtils.readVInt(in); DecimalBuffer buffer = BUFFER_MAIN.get(); byte[] target = buffer.setMeta(plus, scale, length); in.readFully(target, target.length - length, length); modify(buffer.toBigDecimal()); }
/** * test for or w/ present value. */ @Test public void orNotNull() { DecimalOption option = new DecimalOption(); option.modify(decimal(100)); assertThat(option.or(decimal(30)), is(decimal(100))); }
/** * test for initial state. */ @Test public void init() { DecimalOption option = new DecimalOption(); assertThat(option.isNull(), is(true)); }
/** * test for or w/ null. */ @Test public void or() { DecimalOption option = new DecimalOption(); assertThat(option.or(decimal(30)), is(decimal(30))); assertThat(option.isNull(), is(true)); }
/** * heavy compare bytes. * @throws Exception if failed */ @Test public void stress_compareBytes_diff_scale() throws Exception { int count = 10000000; byte[] a = toBytes(new DecimalOption(new BigDecimal("3.14"))); byte[] b = toBytes(new DecimalOption(new BigDecimal("1.414"))); for (int i = 0; i < count; i++) { DecimalOption.compareBytes(a, 0, a.length, b, 0, b.length); } }
/** * heavy restore. * @throws Exception if failed */ @Test public void stress_restore_large() throws Exception { int count = 10000000; DecimalOption value = new DecimalOption(new BigDecimal(Long.MAX_VALUE).multiply(BigDecimal.TEN)); byte[] bytes = toBytes(value); DecimalOption buf = new DecimalOption(); for (int i = 0; i < count; i++) { buf.restore(bytes, 0, bytes.length); if (i == 0) { assertThat(buf, is(value)); } } }
/** * heavy read. * @throws Exception if failed */ @Test public void stress_read_large() throws Exception { int count = 10000000; DecimalOption value = new DecimalOption(new BigDecimal(Long.MAX_VALUE).multiply(BigDecimal.TEN)); byte[] bytes = toBytes(value); DecimalOption buf = new DecimalOption(); DataInputBuffer in = new DataInputBuffer(); for (int i = 0; i < count; i++) { in.reset(bytes, bytes.length); buf.readFields(in); if (i == 0) { assertThat(buf, is(value)); } } }
/** * heavy write. * @throws Exception if failed */ @Test public void stress_write() throws Exception { int count = 10000000; DecimalOption option = new DecimalOption(new BigDecimal("3.14")); DataOutput out = new DataOutputStream(new NullOutputStream()); for (int i = 0; i < count; i++) { option.write(out); } }
@Override public int hashCode() { final int prime = 31; if (isNull()) { return 1; } int result = 1; result = prime * result + entity.hashCode(); return result; }
@Override public int compareInBytes(byte[] b1, int o1, byte[] b2, int o2) throws IOException { return compareBytes(b1, o1, b1.length - o1, b2, o2, b2.length - o2); }