Skip to content

feat: Generics in scalars #56

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 2 commits into from
Aug 17, 2023
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
35 changes: 12 additions & 23 deletions lib/src/main/java/io/cloudquery/scalar/Binary.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import java.util.Arrays;

public class Binary implements Scalar {
public class Binary implements Scalar<byte[]> {
protected byte[] value;
protected boolean valid;

public Binary() {
}
Expand All @@ -18,15 +17,15 @@ public Binary(Object value) throws ValidationException {

@Override
public String toString() {
if (this.valid) {
if (this.value != null) {
return Base64.encodeBase64String(this.value);
}
return NULL_VALUE_STRING;
}

@Override
public boolean isValid() {
return this.valid;
return this.value != null;
}

@Override
Expand All @@ -37,14 +36,12 @@ public ArrowType dataType() {
@Override
public void set(Object value) throws ValidationException {
if (value == null) {
this.valid = false;
this.value = null;
return;
}

if (value instanceof Scalar scalar) {
if (value instanceof Scalar<?> scalar) {
if (!scalar.isValid()) {
this.valid = false;
this.value = null;
return;
}
Expand All @@ -54,19 +51,16 @@ public void set(Object value) throws ValidationException {
}

if (value instanceof byte[] bytes) {
this.valid = true;
this.value = bytes;
return;
}

if (value instanceof CharSequence sequence) {
this.valid = true;
this.value = Base64.decodeBase64(sequence.toString());
return;
}

if (value instanceof char[] chars) {
this.valid = true;
this.value = Base64.decodeBase64(new String(chars));
return;
}
Expand All @@ -75,23 +69,18 @@ public void set(Object value) throws ValidationException {
}

@Override
public Object get() {
if (this.valid) {
return this.value;
}
return null;
public byte[] get() {
return this.value;
}

@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}

if (!(other instanceof Binary o)) {
return false;
if (other instanceof Binary o) {
if (this.value == null) {
return o.value == null;
}
return Arrays.equals(this.value, o.value);
}

return (this.valid == o.valid) && Arrays.equals(this.value, o.value);
return false;
}
}
42 changes: 16 additions & 26 deletions lib/src/main/java/io/cloudquery/scalar/Bool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import org.apache.arrow.vector.types.pojo.ArrowType;

public class Bool implements Scalar {
protected boolean value;
protected boolean valid;
public class Bool implements Scalar<Boolean> {
protected Boolean value;

public Bool() {
}
Expand All @@ -15,15 +14,15 @@ public Bool(Object value) throws ValidationException {

@Override
public String toString() {
if (this.valid) {
return Boolean.toString(this.value);
if (this.value != null) {
return this.value.toString();
}
return NULL_VALUE_STRING;
}

@Override
public boolean isValid() {
return this.valid;
return this.value != null;
}

@Override
Expand All @@ -34,15 +33,13 @@ public ArrowType dataType() {
@Override
public void set(Object value) throws ValidationException {
if (value == null) {
this.valid = false;
this.value = false;
this.value = null;
return;
}

if (value instanceof Scalar scalar) {
if (value instanceof Scalar<?> scalar) {
if (!scalar.isValid()) {
this.valid = false;
this.value = false;
this.value = null;
return;
}

Expand All @@ -51,13 +48,11 @@ public void set(Object value) throws ValidationException {
}

if (value instanceof Boolean b) {
this.valid = true;
this.value = b;
return;
}

if (value instanceof CharSequence sequence) {
this.valid = true;
this.value = Boolean.parseBoolean(sequence.toString());
return;
}
Expand All @@ -66,23 +61,18 @@ public void set(Object value) throws ValidationException {
}

@Override
public Object get() {
if (this.valid) {
return this.value;
}
return null;
public Boolean get() {
return this.value;
}

@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}

if (!(other instanceof Bool o)) {
return false;
if (other instanceof Bool o) {
if (this.value == null) {
return o.value == null;
}
return this.value.equals(o.value);
}

return (this.valid == o.valid) && (this.value == o.value);
return false;
}
}
39 changes: 14 additions & 25 deletions lib/src/main/java/io/cloudquery/scalar/DateDay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import org.apache.arrow.vector.types.DateUnit;
import org.apache.arrow.vector.types.pojo.ArrowType;

public class DateDay implements Scalar {
protected int value;
protected boolean valid;
public class DateDay implements Scalar<Integer> {
protected Integer value;

public DateDay() {
}
Expand All @@ -16,15 +15,15 @@ public DateDay(Object value) throws ValidationException {

@Override
public String toString() {
if (this.valid) {
return Integer.toString(this.value);
if (this.value != null) {
return this.value.toString();
}
return NULL_VALUE_STRING;
}

@Override
public boolean isValid() {
return this.valid;
return this.value != null;
}

@Override
Expand All @@ -35,20 +34,17 @@ public ArrowType dataType() {
@Override
public void set(Object value) throws ValidationException {
if (value == null) {
this.valid = false;
this.value = 0;
return;
}

if (value instanceof Scalar scalar) {
if (value instanceof Scalar<?> scalar) {
if (!scalar.isValid()) {
this.valid = false;
this.value = 0;
return;
}

if (scalar instanceof DateDay date) {
this.valid = date.valid;
this.value = date.value;
return;
}
Expand All @@ -58,13 +54,11 @@ public void set(Object value) throws ValidationException {
}

if (value instanceof Integer b) {
this.valid = true;
this.value = b;
return;
}

if (value instanceof CharSequence sequence) {
this.valid = true;
this.value = Integer.parseInt(sequence.toString());
return;
}
Expand All @@ -73,23 +67,18 @@ public void set(Object value) throws ValidationException {
}

@Override
public Object get() {
if (this.valid) {
return this.value;
}
return null;
public Integer get() {
return this.value;
}

@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}

if (!(other instanceof DateDay o)) {
return false;
if (other instanceof DateDay o) {
if (this.value == null) {
return o.value == null;
}
return this.value.equals(o.value);
}

return (this.valid == o.valid) && (this.value == o.value);
return false;
}
}
Loading