@Override public int hashCode() { final int prime = 31; if (isNull()) { return 1; } int result = 1; result = prime * result + (value ? TRUE_HASHCODE : FALSE_HASHCODE); return result; }
/** * 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(boolean other) { if (isNull()) { return false; } return value == other; }
@Override public String toString() { if (isNull()) { return String.valueOf((Object) null); } else { return String.valueOf(value); } }
@Override public void write(DataOutput out) throws IOException { if (isNull()) { out.writeByte(SERIALIZE_NULL); } else { out.writeByte(value ? SERIALIZE_TRUE : SERIALIZE_FALSE); } }
@Override public Object getPrimitiveJavaObject(Object o) { BooleanOption object = (BooleanOption) o; if (object == null || object.isNull()) { return null; } return object.get(); }
@Override public void emit(BooleanOption option) throws IOException { addCellDelimiter(); if (option.isNull() == false) { boolean value = option.get(); String str = value ? trueFormat : falseFormat; boolean raw = value ? escapeTrue == false : escapeFalse == false; if (raw && isEscapeTarget()) { lineBuffer.append(ESCAPE).append(str).append(ESCAPE); } else { lineBuffer.append(str); } } }
@Override public Object getPrimitiveWritableObject(Object o) { BooleanOption object = (BooleanOption) o; if (object == null || object.isNull()) { return null; } return new BooleanWritable(object.get()); }
/** * test for initial state. */ @Test public void init() { BooleanOption option = new BooleanOption(); assertThat(option.isNull(), is(true)); }
/** * test for Writable w/ null. */ @Test public void writeNull() { BooleanOption option = new BooleanOption(); BooleanOption restored = restore(option); assertThat(restored.isNull(), is(true)); } }
/** * test for or w/ null. */ @Test public void or() { BooleanOption option = new BooleanOption(); assertThat(option.or(true), is(true)); assertThat(option.isNull(), is(true)); }
/** * test for copyFrom w/ null. */ @Test public void copyNull() { BooleanOption option = new BooleanOption(); option.modify(true); BooleanOption other = new BooleanOption(); option.copyFrom(other); assertThat(option.isNull(), is(true)); option.modify(true); option.copyFrom(null); assertThat(option.isNull(), is(true)); }
/** * obtain value. */ @Test public void get() { BooleanOption option = new BooleanOption(); option.modify(false); assertThat(option.get(), is(false)); assertThat(option.isNull(), is(false)); }
/** * test for booleans. * @throws Exception if failed */ @Test public void boolean_values() throws Exception { trueFormat = "true"; falseFormat = "false"; CsvParser parser = create("true,false,"); BooleanOption option = new BooleanOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(true)); parser.fill(option); assertThat(option.get(), is(false)); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * test for parsing {@code boolean} value. * @throws Exception if failed */ @Test public void fillBoolean() throws Exception { BooleanOption value = new BooleanOption(); create("boolean"); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.get(), is(true)); parser.fill(value); assertThat(value.get(), is(false)); parser.endRecord(); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.get(), is(false)); parser.fill(value); assertThat(value.get(), is(true)); parser.endRecord(); assertThat(parser.next(), is(true)); parser.fill(value); assertThat(value.isNull(), is(true)); parser.fill(value); assertThat(value.isNull(), is(false)); parser.endRecord(); }