@Override public void write(DataOutput out) throws IOException { if (isNull()) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeInt(entity.getElapsedDays()); } }
@Override public int hashCode() { final int prime = 31; if (isNull()) { return 1; } int result = 1; result = prime * result + entity.hashCode(); return result; }
/** * Returns whether both this object and the specified value represents an equivalent value or not. * @param other the target value (nullable) * @return {@code true} if this object has the specified value, otherwise {@code false} */ public boolean has(Date other) { if (isNull()) { return other == null; } return entity.equals(other); }
@Override public String toString() { if (isNull()) { return String.valueOf((Object) null); } else { return get().toString(); } }
@Override public DateWritable getPrimitiveWritableObject(Object o) { DateOption object = (DateOption) o; if (object == null || object.isNull()) { return null; } Date value = object.get(); return new DateWritable(value.getElapsedDays() - EPOCH_OFFSET); } }
@Override public java.sql.Date getPrimitiveJavaObject(Object o) { DateOption object = (DateOption) o; if (object == null || object.isNull()) { return null; } Date value = object.get(); // FIXME for optimization @SuppressWarnings("deprecation") java.sql.Date result = new java.sql.Date(value.getYear() - 1900, value.getMonth() - 1, value.getDay()); return result; }
@Override public void emit(DateOption option) throws IOException { addCellDelimiter(); if (option.isNull() == false) { CharSequence string = dateFormat.format(option.get().getElapsedDays()); if (escapeDate || isEscapeTarget()) { appendEscaped(lineBuffer, string); } else { lineBuffer.append(string); } } }
/** * test for initial state. */ @Test public void init() { DateOption option = new DateOption(); assertThat(option.isNull(), is(true)); }
/** * test for Writable w/ null. */ @Test public void writableOption() { DateOption option = new DateOption(); DateOption restored = restore(option); assertThat(restored.isNull(), is(true)); }
/** * test for copyFrom w/ null. */ @Test public void copyNull() { DateOption option = new DateOption(); option.modify(date(100)); DateOption other = new DateOption(); option.copyFrom(other); assertThat(option.isNull(), is(true)); option.modify(date(200)); assertThat(option.isNull(), is(false)); option.copyFrom(null); assertThat(option.isNull(), is(true)); }
/** * test for or w/ elapsed days. */ @Test public void intOr() { DateOption option = new DateOption(); assertThat(option.or(100), is(100)); assertThat(option.isNull(), is(true)); option.modify(200); assertThat(option.or(300), is(200)); }
/** * test for set elapsed days. */ @Test public void setInt() { DateOption option = new DateOption(); option.modify(500); assertThat(option.isNull(), is(false)); assertThat(option.get().getElapsedDays(), is(500)); }
/** * test for date type. */ @Test public void string() { DateOption option = new DateOption(); option.modify(date(999)); assertThat(option.isNull(), is(false)); assertThat(option.get(), is(date(999))); }
/** * test for or w/ date type. */ @Test public void stringOr() { DateOption option = new DateOption(); assertThat(option.or(date(200)), is(date(200))); assertThat(option.isNull(), is(true)); option.modify(date(300)); assertThat(option.or(date(400)), is(date(300))); }
/** * test for date values. * @throws Exception if failed */ @Test public void date_values_direct() throws Exception { dateFormat = "yyyyMMdd"; CsvParser parser = create( "20110331," + "19710401,"); DateOption option = new DateOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(new Date(2011, 3, 31))); parser.fill(option); assertThat(option.get(), is(new Date(1971, 4, 1))); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * test for date values. * @throws Exception if failed */ @Test public void date_values() throws Exception { dateFormat = "yyyy/MM/dd"; CsvParser parser = create( "2011/03/31," + "1971/4/1," + " 1971/4/1 ,"); DateOption option = new DateOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(new Date(2011, 3, 31))); parser.fill(option); assertThat(option.get(), is(new Date(1971, 4, 1))); parser.fill(option); assertThat(option.get(), is(new Date(1971, 4, 1))); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }
assertThat(value.isNull(), is(true)); parser.fill(value); assertThat(value.get(), is(date(1, 1, 1)));