Skip to content
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
45 changes: 45 additions & 0 deletions src/main/java/io/tiledb/java/api/Datatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,55 @@ public enum Datatype {
TILEDB_STRING_UCS4,
TILEDB_ANY;

/** @return Returns the TileDB Datatype size in Bytes * */
public int getNativeSize() throws TileDBError {
return tiledb.tiledb_datatype_size(this.toSwigEnum()).intValue();
}

/** @return True if the TileDB Datatype is a scalar, false otherwise (ex. String) * */
public boolean isStringType() {
switch (this) {
case TILEDB_STRING_ASCII:
case TILEDB_STRING_UTF8:
case TILEDB_STRING_UTF16:
case TILEDB_STRING_UTF32:
case TILEDB_STRING_UCS2:
case TILEDB_STRING_UCS4:
return true;
default:
return false;
}
}

/** @return True if the TileDB Datatype is an integer, false otherwise * */
public boolean isIntegerType() {
switch (this) {
case TILEDB_CHAR:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't char be considered a string? We typically show the char as the string character and not as the ascii integer value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We encode char as bytes and not strings as the representation is not consistent across platforms (Char in Java is 16 bits with different semantics).

case TILEDB_INT8:
case TILEDB_UINT8:
case TILEDB_INT16:
case TILEDB_UINT16:
case TILEDB_INT32:
case TILEDB_UINT32:
case TILEDB_INT64:
case TILEDB_UINT64:
return true;
default:
return false;
}
}

/** @return True if the TileDB Datatype is a float or double, false otherwise * */
public boolean isRealType() {
switch (this) {
case TILEDB_FLOAT32:
case TILEDB_FLOAT64:
return true;
default:
return false;
}
}

protected tiledb_datatype_t toSwigEnum() throws TileDBError {
switch (this) {
case TILEDB_INT32:
Expand Down
83 changes: 72 additions & 11 deletions src/test/java/io/tiledb/java/api/DatatypeTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package io.tiledb.java.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

public class DatatypeTest {

private final Datatype[] dtypes = {
Datatype.TILEDB_ANY,
Datatype.TILEDB_FLOAT32,
Datatype.TILEDB_FLOAT64,
private final Datatype[] stringDtypes = {
Datatype.TILEDB_STRING_ASCII,
Datatype.TILEDB_STRING_UTF8,
Datatype.TILEDB_STRING_UTF16,
Datatype.TILEDB_STRING_UTF32,
Datatype.TILEDB_STRING_UCS2,
Datatype.TILEDB_STRING_UCS4
};

private final Datatype[] realDtypes = {
Datatype.TILEDB_FLOAT32, Datatype.TILEDB_FLOAT64,
};

private final Datatype[] integerDtypes = {
Datatype.TILEDB_CHAR,
Datatype.TILEDB_INT8,
Datatype.TILEDB_INT8,
Datatype.TILEDB_UINT8,
Datatype.TILEDB_INT16,
Expand All @@ -17,19 +33,64 @@ public class DatatypeTest {
Datatype.TILEDB_UINT32,
Datatype.TILEDB_INT64,
Datatype.TILEDB_UINT64,
Datatype.TILEDB_STRING_ASCII,
Datatype.TILEDB_STRING_UTF8,
Datatype.TILEDB_STRING_UTF16,
Datatype.TILEDB_STRING_UTF32,
Datatype.TILEDB_STRING_UCS2,
Datatype.TILEDB_STRING_UCS4
};

private final Datatype[] otherDtypes = {
Datatype.TILEDB_ANY,
};

private List<Datatype> allDatatypes() {
ArrayList<Datatype> dtypes = new ArrayList<>();
Collections.addAll(dtypes, stringDtypes);
Collections.addAll(dtypes, integerDtypes);
Collections.addAll(dtypes, realDtypes);
Collections.addAll(dtypes, otherDtypes);
return dtypes;
}

@Test
public void testDatatypeJNIConversion() throws Exception {
// roundtrip to and from swig enum representation
for (Datatype dtype : dtypes) {
for (Datatype dtype : allDatatypes()) {
Assert.assertEquals(dtype, Datatype.fromSwigEnum(dtype.toSwigEnum()));
}
}

private void checkIfType(Datatype[] trueDtypes) {}

@Test
public void testIsStringDtypes() {
List<Datatype> stringDtypesList = Arrays.asList(stringDtypes);
for (Datatype dtype : allDatatypes()) {
if (stringDtypesList.contains(dtype)) {
Assert.assertTrue(dtype.isStringType());
} else {
Assert.assertFalse(dtype.isStringType());
}
}
}

@Test
public void testIsIntegerDtypes() {
List<Datatype> integerDtypesList = Arrays.asList(integerDtypes);
for (Datatype dtype : allDatatypes()) {
if (integerDtypesList.contains(dtype)) {
Assert.assertTrue(dtype.isIntegerType());
} else {
Assert.assertFalse(dtype.isIntegerType());
}
}
}

@Test
public void testIsRealDtypes() {
List<Datatype> realDtypesList = Arrays.asList(realDtypes);
for (Datatype dtype : allDatatypes()) {
if (realDtypesList.contains(dtype)) {
Assert.assertTrue(dtype.isRealType());
} else {
Assert.assertFalse(dtype.isRealType());
}
}
}
}