From 4b9361ca3466d09a97459535b58f3b9bb07e6e2e Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 17 Aug 2023 12:53:34 +0300 Subject: [PATCH 1/2] generics in scalars --- .../java/io/cloudquery/scalar/Binary.java | 33 +++++--------- .../main/java/io/cloudquery/scalar/Bool.java | 40 +++++++---------- .../java/io/cloudquery/scalar/DateDay.java | 37 ++++++---------- .../java/io/cloudquery/scalar/DateMilli.java | 44 +++++++------------ .../java/io/cloudquery/scalar/Duration.java | 20 ++++----- .../io/cloudquery/scalar/LargeBinary.java | 14 +++--- .../java/io/cloudquery/scalar/Scalar.java | 4 +- .../java/io/cloudquery/scalar/Timestamp.java | 20 ++++----- .../main/java/io/cloudquery/scalar/UUID.java | 25 ++++++----- 9 files changed, 95 insertions(+), 142 deletions(-) diff --git a/lib/src/main/java/io/cloudquery/scalar/Binary.java b/lib/src/main/java/io/cloudquery/scalar/Binary.java index 76844ae..285b882 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Binary.java +++ b/lib/src/main/java/io/cloudquery/scalar/Binary.java @@ -5,9 +5,8 @@ import java.util.Arrays; -public class Binary implements Scalar { +public class Binary implements Scalar { protected byte[] value; - protected boolean valid; public Binary() { } @@ -18,7 +17,7 @@ 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; @@ -26,7 +25,7 @@ public String toString() { @Override public boolean isValid() { - return this.valid; + return this.value != null; } @Override @@ -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 (!scalar.isValid()) { - this.valid = false; this.value = null; return; } @@ -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; } @@ -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; } } \ No newline at end of file diff --git a/lib/src/main/java/io/cloudquery/scalar/Bool.java b/lib/src/main/java/io/cloudquery/scalar/Bool.java index 27b2955..7e962e0 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Bool.java +++ b/lib/src/main/java/io/cloudquery/scalar/Bool.java @@ -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 { + protected Boolean value; public Bool() { } @@ -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 @@ -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 (!scalar.isValid()) { - this.valid = false; - this.value = false; + this.value = null; return; } @@ -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; } @@ -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; } } diff --git a/lib/src/main/java/io/cloudquery/scalar/DateDay.java b/lib/src/main/java/io/cloudquery/scalar/DateDay.java index cfd3d7f..49d10e3 100644 --- a/lib/src/main/java/io/cloudquery/scalar/DateDay.java +++ b/lib/src/main/java/io/cloudquery/scalar/DateDay.java @@ -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 { + protected Integer value; public DateDay() { } @@ -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 @@ -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 (!scalar.isValid()) { - this.valid = false; this.value = 0; return; } if (scalar instanceof DateDay date) { - this.valid = date.valid; this.value = date.value; return; } @@ -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; } @@ -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; } } diff --git a/lib/src/main/java/io/cloudquery/scalar/DateMilli.java b/lib/src/main/java/io/cloudquery/scalar/DateMilli.java index eb6c9fa..8c4d6b0 100644 --- a/lib/src/main/java/io/cloudquery/scalar/DateMilli.java +++ b/lib/src/main/java/io/cloudquery/scalar/DateMilli.java @@ -3,9 +3,8 @@ import org.apache.arrow.vector.types.DateUnit; import org.apache.arrow.vector.types.pojo.ArrowType; -public class DateMilli implements Scalar { - protected long value; - protected boolean valid; +public class DateMilli implements Scalar { + protected Long value; public DateMilli() { } @@ -16,15 +15,15 @@ public DateMilli(Object value) throws ValidationException { @Override public String toString() { - if (this.valid) { - return Long.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 @@ -35,20 +34,17 @@ public ArrowType dataType() { @Override public void set(Object value) throws ValidationException { if (value == null) { - this.valid = false; - this.value = 0; + this.value = null; return; } if (value instanceof Scalar scalar) { if (!scalar.isValid()) { - this.valid = false; - this.value = 0; + this.value = null; return; } if (scalar instanceof DateMilli date) { - this.valid = date.valid; this.value = date.value; return; } @@ -58,19 +54,16 @@ public void set(Object value) throws ValidationException { } if (value instanceof Long b) { - this.valid = true; this.value = b; return; } if (value instanceof Integer b) { - this.valid = true; - this.value = b; + this.value = Long.valueOf(b); return; } if (value instanceof CharSequence sequence) { - this.valid = true; this.value = Long.parseLong(sequence.toString()); return; } @@ -79,23 +72,18 @@ public void set(Object value) throws ValidationException { } @Override - public Object get() { - if (this.valid) { - return this.value; - } - return null; + public Long get() { + return this.value; } @Override public boolean equals(Object other) { - if (other == null) { - return false; - } - - if (!(other instanceof DateMilli o)) { - return false; + if (other instanceof DateMilli 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; } } diff --git a/lib/src/main/java/io/cloudquery/scalar/Duration.java b/lib/src/main/java/io/cloudquery/scalar/Duration.java index 3691428..c72091c 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Duration.java +++ b/lib/src/main/java/io/cloudquery/scalar/Duration.java @@ -3,7 +3,7 @@ import org.apache.arrow.vector.types.TimeUnit; import org.apache.arrow.vector.types.pojo.ArrowType; -public class Duration implements Scalar { +public class Duration implements Scalar { protected java.time.Duration value; // TODO: add more units support later @@ -80,20 +80,18 @@ public void set(Object value) throws ValidationException { } @Override - public Object get() { - return this.value; // null or proper value + public java.time.Duration get() { + return this.value; } @Override public boolean equals(Object other) { - if (other == null) { - return false; - } - - if (!(other instanceof Duration o)) { - return false; + if (other instanceof Duration o) { + if (this.value == null) { + return o.value == null; + } + return this.value.equals(o.value); } - - return this.value == o.value || this.value.equals(o.value); + return false; } } diff --git a/lib/src/main/java/io/cloudquery/scalar/LargeBinary.java b/lib/src/main/java/io/cloudquery/scalar/LargeBinary.java index ade2dea..9e4f5bf 100644 --- a/lib/src/main/java/io/cloudquery/scalar/LargeBinary.java +++ b/lib/src/main/java/io/cloudquery/scalar/LargeBinary.java @@ -21,14 +21,12 @@ public ArrowType dataType() { @Override public boolean equals(Object other) { - if (other == null) { - return false; + if (other instanceof LargeBinary o) { + if (this.value == null) { + return o.value == null; + } + return Arrays.equals(this.value, o.value); } - - if (!(other instanceof LargeBinary o)) { - return false; - } - - return (this.valid == o.valid) && Arrays.equals(this.value, o.value); + return false; } } diff --git a/lib/src/main/java/io/cloudquery/scalar/Scalar.java b/lib/src/main/java/io/cloudquery/scalar/Scalar.java index b6857fd..f18934a 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Scalar.java +++ b/lib/src/main/java/io/cloudquery/scalar/Scalar.java @@ -2,7 +2,7 @@ import org.apache.arrow.vector.types.pojo.ArrowType; -public interface Scalar { +public interface Scalar { String toString(); boolean isValid(); @@ -11,7 +11,7 @@ public interface Scalar { void set(Object value) throws ValidationException; - Object get(); + T get(); boolean equals(Object other); diff --git a/lib/src/main/java/io/cloudquery/scalar/Timestamp.java b/lib/src/main/java/io/cloudquery/scalar/Timestamp.java index 3377bae..53287f7 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Timestamp.java +++ b/lib/src/main/java/io/cloudquery/scalar/Timestamp.java @@ -5,7 +5,7 @@ import java.time.*; -public class Timestamp implements Scalar { +public class Timestamp implements Scalar { public static final ZoneId zoneID = ZoneOffset.UTC; // TODO: add more units support later @@ -94,20 +94,18 @@ public void set(Object value) throws ValidationException { } @Override - public Object get() { - return this.value; // null or proper value + public ZonedDateTime get() { + return this.value; } @Override public boolean equals(Object other) { - if (other == null) { - return false; - } - - if (!(other instanceof Timestamp o)) { - return false; + if (other instanceof Timestamp o) { + if (this.value == null) { + return o.value == null; + } + return this.value.equals(o.value); } - - return this.value == o.value || this.value.equals(o.value); + return false; } } diff --git a/lib/src/main/java/io/cloudquery/scalar/UUID.java b/lib/src/main/java/io/cloudquery/scalar/UUID.java index 8ea8ce8..31621ef 100644 --- a/lib/src/main/java/io/cloudquery/scalar/UUID.java +++ b/lib/src/main/java/io/cloudquery/scalar/UUID.java @@ -6,7 +6,7 @@ import java.nio.ByteBuffer; import java.util.Objects; -public class UUID implements Scalar { +public class UUID implements Scalar { private static final int BYTE_WIDTH = 16; private static final FixedSizeBinary dt = new FixedSizeBinary(BYTE_WIDTH); @@ -19,6 +19,14 @@ public UUID(Object value) throws ValidationException { this.set(value); } + @Override + public String toString() { + if (this.value != null) { + return this.value.toString(); + } + return NULL_VALUE_STRING; + } + @Override public boolean isValid() { return this.value != null; @@ -76,14 +84,17 @@ public void set(Object value) throws ValidationException { } @Override - public Object get() { + public java.util.UUID get() { return this.value; } @Override public final boolean equals(Object other) { if (other instanceof UUID o) { - return this.value == o.value || Objects.equals(this.value, o.value); + if (this.value == null) { + return o.value == null; + } + return this.value.equals(o.value); } return false; } @@ -92,12 +103,4 @@ public final boolean equals(Object other) { public final int hashCode() { return Objects.hash(value); } - - @Override - public String toString() { - if (this.value != null) { - return this.value.toString(); - } - return NULL_VALUE_STRING; - } } From f8d6850a79507eb76cec0701f2db5617490d27bc Mon Sep 17 00:00:00 2001 From: candiduslynx Date: Thu, 17 Aug 2023 13:15:45 +0300 Subject: [PATCH 2/2] fix warnings --- lib/src/main/java/io/cloudquery/scalar/Binary.java | 2 +- lib/src/main/java/io/cloudquery/scalar/Bool.java | 2 +- lib/src/main/java/io/cloudquery/scalar/DateDay.java | 2 +- lib/src/main/java/io/cloudquery/scalar/DateMilli.java | 2 +- lib/src/main/java/io/cloudquery/scalar/Duration.java | 2 +- lib/src/main/java/io/cloudquery/scalar/Timestamp.java | 2 +- lib/src/main/java/io/cloudquery/scalar/UUID.java | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/src/main/java/io/cloudquery/scalar/Binary.java b/lib/src/main/java/io/cloudquery/scalar/Binary.java index 285b882..b70f5d0 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Binary.java +++ b/lib/src/main/java/io/cloudquery/scalar/Binary.java @@ -40,7 +40,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = null; return; diff --git a/lib/src/main/java/io/cloudquery/scalar/Bool.java b/lib/src/main/java/io/cloudquery/scalar/Bool.java index 7e962e0..97698be 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Bool.java +++ b/lib/src/main/java/io/cloudquery/scalar/Bool.java @@ -37,7 +37,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = null; return; diff --git a/lib/src/main/java/io/cloudquery/scalar/DateDay.java b/lib/src/main/java/io/cloudquery/scalar/DateDay.java index 49d10e3..cc756c0 100644 --- a/lib/src/main/java/io/cloudquery/scalar/DateDay.java +++ b/lib/src/main/java/io/cloudquery/scalar/DateDay.java @@ -38,7 +38,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = 0; return; diff --git a/lib/src/main/java/io/cloudquery/scalar/DateMilli.java b/lib/src/main/java/io/cloudquery/scalar/DateMilli.java index 8c4d6b0..15a699f 100644 --- a/lib/src/main/java/io/cloudquery/scalar/DateMilli.java +++ b/lib/src/main/java/io/cloudquery/scalar/DateMilli.java @@ -38,7 +38,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = null; return; diff --git a/lib/src/main/java/io/cloudquery/scalar/Duration.java b/lib/src/main/java/io/cloudquery/scalar/Duration.java index c72091c..64b3fb0 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Duration.java +++ b/lib/src/main/java/io/cloudquery/scalar/Duration.java @@ -41,7 +41,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = null; return; diff --git a/lib/src/main/java/io/cloudquery/scalar/Timestamp.java b/lib/src/main/java/io/cloudquery/scalar/Timestamp.java index 53287f7..04c6743 100644 --- a/lib/src/main/java/io/cloudquery/scalar/Timestamp.java +++ b/lib/src/main/java/io/cloudquery/scalar/Timestamp.java @@ -45,7 +45,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = null; return; diff --git a/lib/src/main/java/io/cloudquery/scalar/UUID.java b/lib/src/main/java/io/cloudquery/scalar/UUID.java index 31621ef..53d17b9 100644 --- a/lib/src/main/java/io/cloudquery/scalar/UUID.java +++ b/lib/src/main/java/io/cloudquery/scalar/UUID.java @@ -44,7 +44,7 @@ public void set(Object value) throws ValidationException { return; } - if (value instanceof Scalar scalar) { + if (value instanceof Scalar scalar) { if (!scalar.isValid()) { this.value = null; return;