Skip to content

Commit bc7d6e3

Browse files
authored
feat: Generics in scalars (#56)
1 parent 2ea0b28 commit bc7d6e3

File tree

9 files changed

+102
-149
lines changed

9 files changed

+102
-149
lines changed

Diff for: lib/src/main/java/io/cloudquery/scalar/Binary.java

+12-23
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
import java.util.Arrays;
77

8-
public class Binary implements Scalar {
8+
public class Binary implements Scalar<byte[]> {
99
protected byte[] value;
10-
protected boolean valid;
1110

1211
public Binary() {
1312
}
@@ -18,15 +17,15 @@ public Binary(Object value) throws ValidationException {
1817

1918
@Override
2019
public String toString() {
21-
if (this.valid) {
20+
if (this.value != null) {
2221
return Base64.encodeBase64String(this.value);
2322
}
2423
return NULL_VALUE_STRING;
2524
}
2625

2726
@Override
2827
public boolean isValid() {
29-
return this.valid;
28+
return this.value != null;
3029
}
3130

3231
@Override
@@ -37,14 +36,12 @@ public ArrowType dataType() {
3736
@Override
3837
public void set(Object value) throws ValidationException {
3938
if (value == null) {
40-
this.valid = false;
4139
this.value = null;
4240
return;
4341
}
4442

45-
if (value instanceof Scalar scalar) {
43+
if (value instanceof Scalar<?> scalar) {
4644
if (!scalar.isValid()) {
47-
this.valid = false;
4845
this.value = null;
4946
return;
5047
}
@@ -54,19 +51,16 @@ public void set(Object value) throws ValidationException {
5451
}
5552

5653
if (value instanceof byte[] bytes) {
57-
this.valid = true;
5854
this.value = bytes;
5955
return;
6056
}
6157

6258
if (value instanceof CharSequence sequence) {
63-
this.valid = true;
6459
this.value = Base64.decodeBase64(sequence.toString());
6560
return;
6661
}
6762

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

7771
@Override
78-
public Object get() {
79-
if (this.valid) {
80-
return this.value;
81-
}
82-
return null;
72+
public byte[] get() {
73+
return this.value;
8374
}
8475

8576
@Override
8677
public boolean equals(Object other) {
87-
if (other == null) {
88-
return false;
89-
}
90-
91-
if (!(other instanceof Binary o)) {
92-
return false;
78+
if (other instanceof Binary o) {
79+
if (this.value == null) {
80+
return o.value == null;
81+
}
82+
return Arrays.equals(this.value, o.value);
9383
}
94-
95-
return (this.valid == o.valid) && Arrays.equals(this.value, o.value);
84+
return false;
9685
}
9786
}

Diff for: lib/src/main/java/io/cloudquery/scalar/Bool.java

+16-26
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

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

5-
public class Bool implements Scalar {
6-
protected boolean value;
7-
protected boolean valid;
5+
public class Bool implements Scalar<Boolean> {
6+
protected Boolean value;
87

98
public Bool() {
109
}
@@ -15,15 +14,15 @@ public Bool(Object value) throws ValidationException {
1514

1615
@Override
1716
public String toString() {
18-
if (this.valid) {
19-
return Boolean.toString(this.value);
17+
if (this.value != null) {
18+
return this.value.toString();
2019
}
2120
return NULL_VALUE_STRING;
2221
}
2322

2423
@Override
2524
public boolean isValid() {
26-
return this.valid;
25+
return this.value != null;
2726
}
2827

2928
@Override
@@ -34,15 +33,13 @@ public ArrowType dataType() {
3433
@Override
3534
public void set(Object value) throws ValidationException {
3635
if (value == null) {
37-
this.valid = false;
38-
this.value = false;
36+
this.value = null;
3937
return;
4038
}
4139

42-
if (value instanceof Scalar scalar) {
40+
if (value instanceof Scalar<?> scalar) {
4341
if (!scalar.isValid()) {
44-
this.valid = false;
45-
this.value = false;
42+
this.value = null;
4643
return;
4744
}
4845

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

5350
if (value instanceof Boolean b) {
54-
this.valid = true;
5551
this.value = b;
5652
return;
5753
}
5854

5955
if (value instanceof CharSequence sequence) {
60-
this.valid = true;
6156
this.value = Boolean.parseBoolean(sequence.toString());
6257
return;
6358
}
@@ -66,23 +61,18 @@ public void set(Object value) throws ValidationException {
6661
}
6762

6863
@Override
69-
public Object get() {
70-
if (this.valid) {
71-
return this.value;
72-
}
73-
return null;
64+
public Boolean get() {
65+
return this.value;
7466
}
7567

7668
@Override
7769
public boolean equals(Object other) {
78-
if (other == null) {
79-
return false;
80-
}
81-
82-
if (!(other instanceof Bool o)) {
83-
return false;
70+
if (other instanceof Bool o) {
71+
if (this.value == null) {
72+
return o.value == null;
73+
}
74+
return this.value.equals(o.value);
8475
}
85-
86-
return (this.valid == o.valid) && (this.value == o.value);
76+
return false;
8777
}
8878
}

Diff for: lib/src/main/java/io/cloudquery/scalar/DateDay.java

+14-25
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import org.apache.arrow.vector.types.DateUnit;
44
import org.apache.arrow.vector.types.pojo.ArrowType;
55

6-
public class DateDay implements Scalar {
7-
protected int value;
8-
protected boolean valid;
6+
public class DateDay implements Scalar<Integer> {
7+
protected Integer value;
98

109
public DateDay() {
1110
}
@@ -16,15 +15,15 @@ public DateDay(Object value) throws ValidationException {
1615

1716
@Override
1817
public String toString() {
19-
if (this.valid) {
20-
return Integer.toString(this.value);
18+
if (this.value != null) {
19+
return this.value.toString();
2120
}
2221
return NULL_VALUE_STRING;
2322
}
2423

2524
@Override
2625
public boolean isValid() {
27-
return this.valid;
26+
return this.value != null;
2827
}
2928

3029
@Override
@@ -35,20 +34,17 @@ public ArrowType dataType() {
3534
@Override
3635
public void set(Object value) throws ValidationException {
3736
if (value == null) {
38-
this.valid = false;
3937
this.value = 0;
4038
return;
4139
}
4240

43-
if (value instanceof Scalar scalar) {
41+
if (value instanceof Scalar<?> scalar) {
4442
if (!scalar.isValid()) {
45-
this.valid = false;
4643
this.value = 0;
4744
return;
4845
}
4946

5047
if (scalar instanceof DateDay date) {
51-
this.valid = date.valid;
5248
this.value = date.value;
5349
return;
5450
}
@@ -58,13 +54,11 @@ public void set(Object value) throws ValidationException {
5854
}
5955

6056
if (value instanceof Integer b) {
61-
this.valid = true;
6257
this.value = b;
6358
return;
6459
}
6560

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

7569
@Override
76-
public Object get() {
77-
if (this.valid) {
78-
return this.value;
79-
}
80-
return null;
70+
public Integer get() {
71+
return this.value;
8172
}
8273

8374
@Override
8475
public boolean equals(Object other) {
85-
if (other == null) {
86-
return false;
87-
}
88-
89-
if (!(other instanceof DateDay o)) {
90-
return false;
76+
if (other instanceof DateDay o) {
77+
if (this.value == null) {
78+
return o.value == null;
79+
}
80+
return this.value.equals(o.value);
9181
}
92-
93-
return (this.valid == o.valid) && (this.value == o.value);
82+
return false;
9483
}
9584
}

0 commit comments

Comments
 (0)