@Override public int hashCode() { final int prime = 31; if (isNull()) { return 1; } int result = 1; result = prime * result + Float.floatToIntBits(result); return result; }
@Override public String toString() { if (isNull()) { return String.valueOf((Object) null); } else { return String.valueOf(value); } }
/** * Returns whether both this object and the specified value represents an equivalent value or not. * @param other the target value * @return {@code true} if this object has the specified value, otherwise {@code false} */ public boolean has(float other) { if (isNull()) { return false; } return Float.floatToIntBits(value) == Float.floatToIntBits(other); }
@Override public void write(DataOutput out) throws IOException { if (isNull()) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeInt(encode(value)); } }
@Override public Object getPrimitiveJavaObject(Object o) { FloatOption object = (FloatOption) o; if (object == null || object.isNull()) { return null; } return object.get(); }
@Override public Object getPrimitiveWritableObject(Object o) { FloatOption object = (FloatOption) o; if (object == null || object.isNull()) { return null; } return new FloatWritable(object.get()); }
@Override public void emit(FloatOption option) throws IOException { addCellDelimiter(); if (option.isNull() == false) { if (isEscapeTarget()) { lineBuffer.append(ESCAPE).append(option.get()).append(ESCAPE); } else { lineBuffer.append(option.get()); } } }
/** * test for initial state. */ @Test public void init() { FloatOption option = new FloatOption(); assertThat(option.isNull(), is(true)); }
/** * test for or w/ absent value. */ @Test public void or() { FloatOption option = new FloatOption(); assertThat(option.or(30), is(30.f)); assertThat(option.isNull(), is(true)); }
/** * test for Writable w/ null. */ @Test public void writeNull() { FloatOption option = new FloatOption(); FloatOption restored = restore(option); assertThat(restored.isNull(), is(true)); } }
/** * test for get. */ @Test public void get() { FloatOption option = new FloatOption(); option.modify(100); assertThat(option.get(), is(100.f)); assertThat(option.isNull(), is(false)); }
/** * test for copyFrom w/ null. */ @Test public void copyNull() { FloatOption option = new FloatOption(); option.modify(100); FloatOption other = new FloatOption(); option.copyFrom(other); assertThat(option.isNull(), is(true)); option.modify(100); option.copyFrom(null); assertThat(option.isNull(), is(true)); }
/** * test for parsing {@code float} value. * @throws Exception if failed */ @Test public void fillFloat() throws Exception { FloatOption value = new FloatOption(); create("float"); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.get(), is(+0.f)); parser.fill(value); assertThat(value.get(), is(-0.f)); parser.fill(value); assertThat(value.get(), is(+10.5f)); parser.fill(value); assertThat(value.get(), is(-10.5f)); parser.endRecord(); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.isNull(), is(true)); parser.fill(value); assertThat(value.get(), is(Float.POSITIVE_INFINITY)); parser.fill(value); assertThat(value.get(), is(Float.NEGATIVE_INFINITY)); parser.fill(value); assertThat(value.get(), is(Float.NaN)); parser.endRecord(); assertThat(parser.next(), is(false)); }
assertThat(option.isNull(), is(true));
assertThat(value.isNull(), is(true)); parser.fill(value); assertThat(value.get(), is(Float.POSITIVE_INFINITY));