Skip to content

Converting Time to LocalTime in TimeColumn's appendObj() #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions core/src/main/java/tech/tablesaw/api/TimeColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.nio.ByteBuffer;
import java.sql.Time;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
Expand Down Expand Up @@ -176,11 +177,15 @@ public TimeColumn appendObj(Object obj) {
if (obj == null) {
return appendMissing();
}
if (!(obj instanceof LocalTime)) {
throw new IllegalArgumentException(
"Cannot append " + obj.getClass().getName() + " to TimeColumn");
if (obj instanceof LocalTime) {
return append((LocalTime) obj);
}
if (obj instanceof Time) {
Time time = (Time) obj;
return append(time.toLocalTime());
}
return append((LocalTime) obj);
throw new IllegalArgumentException(
"Cannot append " + obj.getClass().getName() + " to TimeColumn");
}

@Override
Expand Down
38 changes: 33 additions & 5 deletions core/src/test/java/tech/tablesaw/api/TimeColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@

package tech.tablesaw.api;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static tech.tablesaw.columns.times.PackedLocalTime.getMinuteOfDay;
import static tech.tablesaw.columns.times.PackedLocalTime.getSecondOfDay;
import static tech.tablesaw.columns.times.PackedLocalTime.of;

import java.nio.ByteBuffer;
import java.sql.Time;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
Expand Down Expand Up @@ -407,6 +404,37 @@ public void testNull() {
assertNull(col.get(0));
}

@Test
public void testAppendObjNull() {
TimeColumn returned = column1.appendObj(null);
assertEquals(column1, returned);
assertEquals(1, returned.size());
assertTrue(returned.isMissing(0));
}

@Test
public void testAppendObjLocalTime() {
LocalTime localTime = LocalTime.of(9,10,42);
TimeColumn returned = column1.appendObj(localTime);
assertEquals(column1, returned);
assertEquals(1, returned.size());
assertEquals(LocalTime.of(9,10,42), returned.get(0));
}

@Test
public void testAppendObjTime() {
Time time = Time.valueOf("09:10:42");
TimeColumn returned = column1.appendObj(time);
assertEquals(column1, returned);
assertEquals(1, returned.size());
assertEquals(LocalTime.of(9,10,42), returned.get(0));
}

@Test
public void testAppendObjIllegal() {
assertThrows(IllegalArgumentException.class, () -> column1.appendObj(new Object()));
}

private void assertMinAndMaxEquals(int expected, IntColumn numberColumn) {
assertEquals(expected, (int) numberColumn.min());
assertEquals(expected, (int) numberColumn.max());
Expand Down